Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import threading
|
3 |
+
from flask import Flask, request, jsonify
|
4 |
+
from linebot import LineBotApi, WebhookHandler
|
5 |
+
from linebot.models import MessageEvent, TextMessage, TextSendMessage
|
6 |
+
from model import classify_esi # นำเข้าโมเดลจาก model.py
|
7 |
+
|
8 |
+
# ✅ โหลด Environment Variables
|
9 |
+
LINE_ACCESS_TOKEN = os.getenv("LINE_ACCESS_TOKEN")
|
10 |
+
LINE_CHANNEL_SECRET = os.getenv("LINE_CHANNEL_SECRET")
|
11 |
+
|
12 |
+
# ✅ ตั้งค่า Flask App
|
13 |
+
app = Flask(__name__)
|
14 |
+
line_bot_api = LineBotApi(LINE_ACCESS_TOKEN)
|
15 |
+
handler = WebhookHandler(LINE_CHANNEL_SECRET)
|
16 |
+
|
17 |
+
@app.route("/")
|
18 |
+
def home():
|
19 |
+
return "DeepSeek AI ESI Classifier is running!"
|
20 |
+
|
21 |
+
@app.route("/webhook", methods=["POST"])
|
22 |
+
def webhook():
|
23 |
+
signature = request.headers.get("X-Line-Signature", "No Signature")
|
24 |
+
body = request.get_data(as_text=True)
|
25 |
+
|
26 |
+
if not signature:
|
27 |
+
return "Missing Signature", 400
|
28 |
+
|
29 |
+
threading.Thread(target=handler.handle, args=(body, signature)).start()
|
30 |
+
return "OK", 200
|
31 |
+
|
32 |
+
@handler.add(MessageEvent, message=TextMessage)
|
33 |
+
def handle_message(event):
|
34 |
+
user_message = event.message.text
|
35 |
+
esi_level = classify_esi(user_message)
|
36 |
+
|
37 |
+
if esi_level in [1, 2]:
|
38 |
+
response_text = f"🚨 อาการของคุณรุนแรง ควรรีบไปห้องฉุกเฉินทันที! (ESI {esi_level})"
|
39 |
+
elif esi_level == 3:
|
40 |
+
response_text = f"🩺 คุณควรพบแพทย์เร็วที่สุดเพื่อตรวจสอบเพิ่มเติม (ESI {esi_level})"
|
41 |
+
elif esi_level in [4, 5]:
|
42 |
+
response_text = f"💊 อาการของคุณสามารถรอพบแพทย์ที่ OPD ได้ (ESI {esi_level})"
|
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 |
+
app.run(host="0.0.0.0", port=7860)
|