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