Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,39 @@
|
|
1 |
import os
|
2 |
import threading
|
3 |
-
from
|
|
|
4 |
from linebot import LineBotApi, WebhookHandler
|
5 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage
|
6 |
from model import classify_esi # นำเข้าโมเดลจาก model.py
|
7 |
|
8 |
-
#
|
9 |
LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
|
10 |
LINE_CHANNEL_SECRET = os.getenv("LINE_CHANNEL_SECRET")
|
11 |
|
12 |
-
# ✅ ตั้งค่า Flask
|
13 |
-
app =
|
14 |
line_bot_api = LineBotApi(LINE_ACCESS_TOKEN)
|
15 |
handler = WebhookHandler(LINE_CHANNEL_SECRET)
|
16 |
|
17 |
-
|
|
|
18 |
def home():
|
19 |
-
return "DeepSeek AI ESI Classifier is running on Hugging Face Space!"
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
signature
|
24 |
-
body
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
32 |
@handler.add(MessageEvent, message=TextMessage)
|
33 |
def handle_message(event):
|
34 |
user_message = event.message.text
|
@@ -39,12 +44,13 @@ def handle_message(event):
|
|
39 |
elif esi_level == 3:
|
40 |
response_text = f"🩺 คุณควรพบแพทย์เร็วที่สุดเพื่อตรวจสอบเพิ่มเติม (ESI {esi_level})"
|
41 |
elif esi_level in [4, 5]:
|
42 |
-
response_text = f"💊 อาการของคุณสามารถรอพบแพทย์ที่ OPD
|
43 |
else:
|
44 |
response_text = "❌ ไม่สามารถประเมินได้ กรุณาลองอธิบายอาการให้ละเอียดขึ้น"
|
45 |
|
46 |
threading.Thread(target=lambda: line_bot_api.reply_message(event.reply_token, TextSendMessage(text=response_text))).start()
|
47 |
|
48 |
-
# ✅
|
49 |
if __name__ == "__main__":
|
50 |
-
|
|
|
|
1 |
import os
|
2 |
import threading
|
3 |
+
from fastapi import FastAPI, Request
|
4 |
+
from pydantic import BaseModel
|
5 |
from linebot import LineBotApi, WebhookHandler
|
6 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage
|
7 |
from model import classify_esi # นำเข้าโมเดลจาก model.py
|
8 |
|
9 |
+
# โหลด Environment Variables
|
10 |
LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
|
11 |
LINE_CHANNEL_SECRET = os.getenv("LINE_CHANNEL_SECRET")
|
12 |
|
13 |
+
# ✅ ตั้งค่า FastAPI แทน Flask
|
14 |
+
app = FastAPI()
|
15 |
line_bot_api = LineBotApi(LINE_ACCESS_TOKEN)
|
16 |
handler = WebhookHandler(LINE_CHANNEL_SECRET)
|
17 |
|
18 |
+
# ✅ Route หน้าแรก
|
19 |
+
@app.get("/")
|
20 |
def home():
|
21 |
+
return {"message": "DeepSeek AI ESI Classifier is running on Hugging Face Space!"}
|
22 |
+
|
23 |
+
# ✅ Webhook สำหรับรับข้อความจาก LINE
|
24 |
+
class LineRequest(BaseModel):
|
25 |
+
signature: str
|
26 |
+
body: str
|
27 |
+
|
28 |
+
@app.post("/webhook")
|
29 |
+
async def webhook(request: LineRequest):
|
30 |
+
if not request.signature:
|
31 |
+
return {"error": "Missing Signature"}
|
32 |
+
|
33 |
+
threading.Thread(target=handler.handle, args=(request.body, request.signature)).start()
|
34 |
+
return {"status": "OK"}
|
35 |
+
|
36 |
+
# ✅ ฟังก์ชันตอบกลับ LINE
|
37 |
@handler.add(MessageEvent, message=TextMessage)
|
38 |
def handle_message(event):
|
39 |
user_message = event.message.text
|
|
|
44 |
elif esi_level == 3:
|
45 |
response_text = f"🩺 คุณควรพบแพทย์เร็วที่สุดเพื่อตรวจสอบเพิ่มเติม (ESI {esi_level})"
|
46 |
elif esi_level in [4, 5]:
|
47 |
+
response_text = f"💊 อาการของคุณสามารถรอพบแพทย์ที่ OPD ในวันพรุ่งนี้เช้าได้ (ESI {esi_level})"
|
48 |
else:
|
49 |
response_text = "❌ ไม่สามารถประเมินได้ กรุณาลองอธิบายอาการให้ละเอียดขึ้น"
|
50 |
|
51 |
threading.Thread(target=lambda: line_bot_api.reply_message(event.reply_token, TextSendMessage(text=response_text))).start()
|
52 |
|
53 |
+
# ✅ รันแอปที่ Port 7860 (ค่าเริ่มต้นของ Hugging Face Space)
|
54 |
if __name__ == "__main__":
|
55 |
+
import uvicorn
|
56 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|