Spaces:
Runtime error
Runtime error
File size: 1,152 Bytes
4e580b4 c334ba3 4e580b4 c334ba3 4e580b4 c334ba3 4e580b4 |
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 |
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) |