kkulchatbot / app.py
Sirawitch's picture
Update app.py
86dc7b3 verified
raw
history blame
1.33 kB
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional
from huggingface_hub import InferenceClient
import uvicorn
app = FastAPI()
client = InferenceClient("scb10x/llama-3-typhoon-v1.5-8b-instruct")
class Query(BaseModel):
queryResult: Optional[dict] = None
queryText: Optional[str] = None
@app.get("/")
async def root():
return {"message": "Welcome to the API"}
@app.post("/webhook")
async def webhook(query: Query):
try:
user_query = query.queryResult.get('queryText') if query.queryResult else query.queryText
if not user_query:
raise HTTPException(status_code=400, detail="No query text provided")
messages = [
{"role": "system", "content": "You are a friendly Chatbot."},
{"role": "user", "content": user_query}
]
response = client.chat_completion(
messages,
max_tokens=512,
temperature=0.7,
top_p=0.95,
)
model_reply = response.choices[0].message.content.strip()
return {"fulfillmentText": model_reply}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)