Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,75 @@
|
|
1 |
import os
|
2 |
import threading
|
3 |
-
|
4 |
-
from
|
5 |
-
from linebot import LineBotApi
|
6 |
-
from linebot.
|
7 |
-
from
|
|
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
|
11 |
LINE_CHANNEL_SECRET = os.getenv("LINE_CHANNEL_SECRET")
|
12 |
|
13 |
-
#
|
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
|
21 |
-
return {"message": "
|
22 |
-
|
23 |
-
# ✅ Webhook สำหรับรับข้อความจาก LINE
|
24 |
-
class LineRequest(BaseModel):
|
25 |
-
signature: str
|
26 |
-
body: str
|
27 |
|
28 |
@app.post("/webhook")
|
29 |
-
async def webhook(request:
|
30 |
-
if
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
return {"status": "OK"}
|
35 |
|
36 |
-
#
|
37 |
@handler.add(MessageEvent, message=TextMessage)
|
38 |
def handle_message(event):
|
39 |
user_message = event.message.text
|
40 |
-
esi_level = classify_esi(user_message)
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
if esi_level in [1, 2]:
|
43 |
-
|
44 |
elif esi_level == 3:
|
45 |
-
|
46 |
elif esi_level in [4, 5]:
|
47 |
-
|
48 |
else:
|
49 |
-
|
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)
|
|
|
|
1 |
import os
|
2 |
import threading
|
3 |
+
import uvicorn
|
4 |
+
from fastapi import FastAPI, Request, HTTPException, Header
|
5 |
+
from linebot.v3.messaging import LineBotApi
|
6 |
+
from linebot.v3.webhook import WebhookHandler
|
7 |
+
from linebot.v3.exceptions import InvalidSignatureError
|
8 |
+
from linebot.v3.webhooks import MessageEvent, TextMessage
|
9 |
+
from linebot.v3.messaging import TextSendMessage
|
10 |
+
from model import classify_esi # นำเข้าโมเดลสำหรับจัดอันดับ ESI
|
11 |
|
12 |
+
# Load environment variables
|
13 |
LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
|
14 |
LINE_CHANNEL_SECRET = os.getenv("LINE_CHANNEL_SECRET")
|
15 |
|
16 |
+
# Initialize FastAPI
|
17 |
app = FastAPI()
|
18 |
+
|
19 |
+
# Initialize LINE SDK
|
20 |
line_bot_api = LineBotApi(LINE_ACCESS_TOKEN)
|
21 |
handler = WebhookHandler(LINE_CHANNEL_SECRET)
|
22 |
|
|
|
23 |
@app.get("/")
|
24 |
+
async def root():
|
25 |
+
return {"message": "Triage Bot is running for the ER of San Pa Tong Hospital!"}
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
@app.post("/webhook")
|
28 |
+
async def webhook(request: Request, x_line_signature: str = Header(None)):
|
29 |
+
if x_line_signature is None:
|
30 |
+
raise HTTPException(status_code=400, detail="Missing X-Line-Signature")
|
31 |
+
|
32 |
+
body = await request.body()
|
33 |
+
body_text = body.decode("utf-8")
|
34 |
+
|
35 |
+
try:
|
36 |
+
handler.handle(body_text, x_line_signature)
|
37 |
+
except InvalidSignatureError:
|
38 |
+
raise HTTPException(status_code=400, detail="Invalid signature")
|
39 |
+
|
40 |
return {"status": "OK"}
|
41 |
|
42 |
+
# Event handler for messages
|
43 |
@handler.add(MessageEvent, message=TextMessage)
|
44 |
def handle_message(event):
|
45 |
user_message = event.message.text
|
46 |
+
esi_level = classify_esi(user_message) # ใช้โมเดล AI วิเคราะห์ ESI
|
47 |
|
48 |
+
# แปลงระดับ ESI เป็นคำแนะนำ
|
49 |
+
response_text = get_triage_response(esi_level)
|
50 |
+
|
51 |
+
# ใช้ threading เพื่อให้ bot ตอบกลับทันที
|
52 |
+
threading.Thread(target=reply_message, args=(event.reply_token, response_text)).start()
|
53 |
+
|
54 |
+
def reply_message(reply_token, message):
|
55 |
+
try:
|
56 |
+
line_bot_api.reply_message(reply_token, TextSendMessage(text=message))
|
57 |
+
except Exception as e:
|
58 |
+
print(f"Error sending reply: {e}")
|
59 |
+
|
60 |
+
def get_triage_response(esi_level):
|
61 |
+
"""
|
62 |
+
รับค่า ESI (1-5) และแปลงเป็นข้อความแนะนำให้ผู้ป่วย
|
63 |
+
"""
|
64 |
if esi_level in [1, 2]:
|
65 |
+
return "🚑 อาการของคุณรุนแรง ควรรีบไปห้องฉุกเฉินทันที!"
|
66 |
elif esi_level == 3:
|
67 |
+
return "🏥 คุณควรพบแพทย์เร็วที่สุดเพื่อตรวจสอบเพิ่มเติม"
|
68 |
elif esi_level in [4, 5]:
|
69 |
+
return "😊 อาการของคุณสามารถรอพบแพทย์ที่ OPD ในวันพรุ่งนี้เช้าได้"
|
70 |
else:
|
71 |
+
return "❓ ไม่สามารถประเมินได้ กรุณาลองอธิบายอาการให้ละเอียดขึ้น"
|
72 |
|
|
|
|
|
|
|
73 |
if __name__ == "__main__":
|
|
|
74 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
75 |
+
|