Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- new.py +43 -0
- requirements.txt +3 -0
new.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from fastapi import FastAPI
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from huggingface_hub import InferenceClient
|
5 |
+
|
6 |
+
# Initialize FastAPI app
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Get the API key securely from the environment variables
|
10 |
+
API_KEY = os.getenv("HF_API_KEY") # The key name matches the one you set in Secrets
|
11 |
+
MODEL_NAME = "meta-llama/Llama-3.1-8B-Instruct"
|
12 |
+
|
13 |
+
# Initialize Hugging Face Inference Client
|
14 |
+
client = InferenceClient(api_key=API_KEY)
|
15 |
+
|
16 |
+
# Define input data model
|
17 |
+
class ChatInput(BaseModel):
|
18 |
+
role: str
|
19 |
+
content: str
|
20 |
+
|
21 |
+
@app.post("/chat")
|
22 |
+
async def chat(input_data: ChatInput):
|
23 |
+
try:
|
24 |
+
# Prepare input messages for the model
|
25 |
+
messages = [
|
26 |
+
{
|
27 |
+
"role": input_data.role,
|
28 |
+
"content": input_data.content
|
29 |
+
}
|
30 |
+
]
|
31 |
+
|
32 |
+
# Get completion from the Hugging Face model
|
33 |
+
completion = client.chat.completions.create(
|
34 |
+
model=MODEL_NAME,
|
35 |
+
messages=messages,
|
36 |
+
max_tokens=500
|
37 |
+
)
|
38 |
+
# Extract and return the response
|
39 |
+
return {
|
40 |
+
"response": completion.choices[0].message
|
41 |
+
}
|
42 |
+
except Exception as e:
|
43 |
+
return {"error": str(e)}
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# fastapi
|
2 |
+
uvicorn
|
3 |
+
huggingface-hub
|