Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,41 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 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 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import Optional
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
+
import uvicorn
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
client = InferenceClient("scb10x/llama-3-typhoon-v1.5-8b-instruct")
|
| 9 |
+
|
| 10 |
+
class Query(BaseModel):
|
| 11 |
+
queryResult: Optional[dict] = None
|
| 12 |
+
queryText: Optional[str] = None
|
| 13 |
+
|
| 14 |
+
@app.post("/webhook")
|
| 15 |
+
async def webhook(query: Query):
|
| 16 |
+
try:
|
| 17 |
+
user_query = query.queryResult.get('queryText') if query.queryResult else query.queryText
|
| 18 |
+
|
| 19 |
+
if not user_query:
|
| 20 |
+
raise HTTPException(status_code=400, detail="No query text provided")
|
| 21 |
+
|
| 22 |
+
messages = [
|
| 23 |
+
{"role": "system", "content": "You are a friendly Chatbot."},
|
| 24 |
+
{"role": "user", "content": user_query}
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
response = client.chat_completion(
|
| 28 |
+
messages,
|
| 29 |
+
max_tokens=512,
|
| 30 |
+
temperature=0.7,
|
| 31 |
+
top_p=0.95,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
model_reply = response.choices[0].message.content.strip()
|
| 35 |
+
|
| 36 |
+
return {"fulfillmentText": model_reply}
|
| 37 |
+
except Exception as e:
|
| 38 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|