kkulchatbot / app.py
Sirawitch's picture
Update app.py
4e580b4 verified
raw
history blame
1.15 kB
from fastapi import FastAPI, Request
from huggingface_hub import InferenceClient
import uvicorn
app = FastAPI()
client = InferenceClient("scb10x/llama-3-typhoon-v1.5-8b-instruct")
@app.post("/webhook")
async def webhook(request: Request):
# รับข้อมูลจาก Dialogflow
data = await request.json()
query_text = data['queryResult']['queryText']
# สร้าง messages สำหรับ Huggingface API
messages = [
{"role": "system", "content": "You are a friendly Chatbot."},
{"role": "user", "content": query_text}
]
# เรียกใช้ Huggingface API
response = client.chat_completion(
messages,
max_tokens=512,
temperature=0.7,
top_p=0.95,
)
# ดึงข้อความตอบกลับ
answer = response.choices[0].message.content
# สร้างการตอบกลับสำหรับ Dialogflow
dialogflow_response = {
"fulfillmentText": answer,
}
return dialogflow_response
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)