Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,20 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException
|
2 |
-
from fastapi.responses import JSONResponse
|
3 |
from pydantic import BaseModel
|
4 |
from typing import Optional
|
5 |
-
from
|
6 |
-
import
|
7 |
-
from fastapi.middleware.cors import CORSMiddleware
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
allow_credentials=True,
|
16 |
-
allow_methods=["*"], # หรือระบุเจาะจง ["GET", "POST", "OPTIONS"]
|
17 |
-
allow_headers=["*"],
|
18 |
-
)
|
19 |
|
20 |
-
# Hugging Face client
|
21 |
-
client = InferenceClient("scb10x/llama-3-typhoon-v1.5-8b-instruct")
|
22 |
-
|
23 |
-
# Pydantic model for query
|
24 |
class Query(BaseModel):
|
25 |
queryResult: Optional[dict] = None
|
26 |
queryText: Optional[str] = None
|
27 |
|
28 |
-
# Root route
|
29 |
-
@app.get("/")
|
30 |
-
async def root():
|
31 |
-
return {"message": "Welcome to the API"}
|
32 |
-
|
33 |
-
@app.options("/webhook")
|
34 |
-
async def webhook_options():
|
35 |
-
return {"message": "OK"}
|
36 |
-
|
37 |
-
@app.get("/test")
|
38 |
-
async def test():
|
39 |
-
return {"message": "Test successful"}
|
40 |
-
|
41 |
-
# Webhook route
|
42 |
@app.post("/webhook")
|
43 |
async def webhook(query: Query):
|
44 |
try:
|
@@ -47,24 +23,16 @@ async def webhook(query: Query):
|
|
47 |
if not user_query:
|
48 |
raise HTTPException(status_code=400, detail="No query text provided")
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
]
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
max_tokens=512,
|
58 |
-
temperature=0.7,
|
59 |
-
top_p=0.95,
|
60 |
-
)
|
61 |
|
62 |
-
|
|
|
63 |
|
64 |
-
return {"fulfillmentText":
|
65 |
except Exception as e:
|
66 |
-
raise HTTPException(status_code=500, detail=str(e))
|
67 |
-
|
68 |
-
# Run the app
|
69 |
-
if __name__ == "__main__":
|
70 |
-
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 transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
+
import torch
|
|
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
9 |
+
# โหลดโมเดลและ tokenizer
|
10 |
+
model_name = "scb10x/llama-3-typhoon-v1.5-8b-instruct"
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
|
|
|
|
|
|
|
|
13 |
|
|
|
|
|
|
|
|
|
14 |
class Query(BaseModel):
|
15 |
queryResult: Optional[dict] = None
|
16 |
queryText: Optional[str] = None
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
@app.post("/webhook")
|
19 |
async def webhook(query: Query):
|
20 |
try:
|
|
|
23 |
if not user_query:
|
24 |
raise HTTPException(status_code=400, detail="No query text provided")
|
25 |
|
26 |
+
# สร้าง prompt และ generate ข้อความ
|
27 |
+
prompt = f"Human: {user_query}\nAI:"
|
28 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
|
|
29 |
|
30 |
+
output = model.generate(input_ids, max_new_tokens=100, temperature=0.7)
|
31 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
# แยกส่วนที่เป็นคำตอบของ AI
|
34 |
+
ai_response = response.split("AI:")[-1].strip()
|
35 |
|
36 |
+
return {"fulfillmentText": ai_response}
|
37 |
except Exception as e:
|
38 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|