Spaces:
Sleeping
Sleeping
Update app.py
#2
by
wsy07
- opened
app.py
CHANGED
|
@@ -1,19 +1,35 @@
|
|
|
|
|
| 1 |
from flask import Flask, request, abort
|
| 2 |
from linebot import LineBotApi, WebhookHandler
|
| 3 |
from linebot.exceptions import InvalidSignatureError
|
| 4 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
line_bot_api = LineBotApi(os.environ.get("LINE_CHANNEL_ACCESS_TOKEN"))
|
| 12 |
handler = WebhookHandler(os.environ.get("LINE_CHANNEL_SECRET"))
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.route("/", methods=["POST"])
|
| 15 |
def callback():
|
| 16 |
-
# 接收 X-Line-Signature
|
| 17 |
signature = request.headers.get("X-Line-Signature")
|
| 18 |
body = request.get_data(as_text=True)
|
| 19 |
|
|
@@ -21,16 +37,11 @@ def callback():
|
|
| 21 |
handler.handle(body, signature)
|
| 22 |
except InvalidSignatureError:
|
| 23 |
abort(400)
|
| 24 |
-
|
| 25 |
return "OK"
|
| 26 |
|
| 27 |
-
#
|
| 28 |
@handler.add(MessageEvent, message=TextMessage)
|
| 29 |
def handle_message(event):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
line_bot_api.reply_message(event.reply_token, TextSendMessage(text=
|
| 33 |
-
|
| 34 |
-
# 本地測試用(部署在 Huggingface 時不需要)
|
| 35 |
-
if __name__ == "__main__":
|
| 36 |
-
app.run()
|
|
|
|
| 1 |
+
import os
|
| 2 |
from flask import Flask, request, abort
|
| 3 |
from linebot import LineBotApi, WebhookHandler
|
| 4 |
from linebot.exceptions import InvalidSignatureError
|
| 5 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage
|
|
|
|
| 6 |
|
| 7 |
+
from google import generativeai as genai
|
| 8 |
+
import markdown
|
| 9 |
+
from bs4 import BeautifulSoup
|
| 10 |
+
|
| 11 |
+
# 初始化 Flask
|
| 12 |
app = Flask(__name__)
|
| 13 |
|
| 14 |
+
# 設定 LINE Bot 憑證(從 Huggingface Secrets 讀取)
|
| 15 |
line_bot_api = LineBotApi(os.environ.get("LINE_CHANNEL_ACCESS_TOKEN"))
|
| 16 |
handler = WebhookHandler(os.environ.get("LINE_CHANNEL_SECRET"))
|
| 17 |
|
| 18 |
+
# 初始化 Gemini Flash(使用免費模型)
|
| 19 |
+
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
| 20 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 21 |
+
|
| 22 |
+
def query(prompt: str) -> str:
|
| 23 |
+
try:
|
| 24 |
+
response = model.generate_content(prompt)
|
| 25 |
+
html = markdown.markdown(response.text)
|
| 26 |
+
return BeautifulSoup(html, "html.parser").get_text()
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"⚠️ Gemini 發生錯誤:{e}"
|
| 29 |
+
|
| 30 |
+
# LINE webhook 入口
|
| 31 |
@app.route("/", methods=["POST"])
|
| 32 |
def callback():
|
|
|
|
| 33 |
signature = request.headers.get("X-Line-Signature")
|
| 34 |
body = request.get_data(as_text=True)
|
| 35 |
|
|
|
|
| 37 |
handler.handle(body, signature)
|
| 38 |
except InvalidSignatureError:
|
| 39 |
abort(400)
|
|
|
|
| 40 |
return "OK"
|
| 41 |
|
| 42 |
+
# 處理文字訊息
|
| 43 |
@handler.add(MessageEvent, message=TextMessage)
|
| 44 |
def handle_message(event):
|
| 45 |
+
user_msg = event.message.text.strip()
|
| 46 |
+
bot_reply = query(user_msg)
|
| 47 |
+
line_bot_api.reply_message(event.reply_token, TextSendMessage(text=bot_reply))
|
|
|
|
|
|
|
|
|
|
|
|