Spaces:
Running
Running
logging
Browse files- Dockerfile +1 -1
- with_logs.py +94 -0
Dockerfile
CHANGED
|
@@ -14,4 +14,4 @@ USER appuser
|
|
| 14 |
|
| 15 |
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD curl -f http://0.0.0.0:7860/ || exit 1
|
| 16 |
|
| 17 |
-
CMD ["gunicorn","-b", "0.0.0.0:7860", "
|
|
|
|
| 14 |
|
| 15 |
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD curl -f http://0.0.0.0:7860/ || exit 1
|
| 16 |
|
| 17 |
+
CMD ["gunicorn","-b", "0.0.0.0:7860", "with_logs:app"]
|
with_logs.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
東吳大學資料系 2025 LINEBOT
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import logging
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
from flask import Flask, abort, request
|
| 9 |
+
from bs4 import BeautifulSoup
|
| 10 |
+
import markdown
|
| 11 |
+
|
| 12 |
+
from google import genai
|
| 13 |
+
from google.genai import types # 加入system prompot所需的types模組
|
| 14 |
+
|
| 15 |
+
from linebot.v3 import WebhookHandler
|
| 16 |
+
from linebot.v3.exceptions import InvalidSignatureError
|
| 17 |
+
from linebot.v3.messaging import (
|
| 18 |
+
ApiClient,
|
| 19 |
+
Configuration,
|
| 20 |
+
MessagingApi,
|
| 21 |
+
ReplyMessageRequest,
|
| 22 |
+
TextMessage,
|
| 23 |
+
)
|
| 24 |
+
from linebot.v3.webhooks import MessageEvent, TextMessageContent
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Initialize Google Gemini
|
| 28 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 29 |
+
client = genai.Client(api_key=GOOGLE_API_KEY)
|
| 30 |
+
chat = client.chats.create(model="gemini-2.0-flash")
|
| 31 |
+
|
| 32 |
+
# Initialize Flask app
|
| 33 |
+
app = Flask(__name__)
|
| 34 |
+
logging.basicConfig(
|
| 35 |
+
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
|
| 36 |
+
)
|
| 37 |
+
app.logger.setLevel(logging.INFO)
|
| 38 |
+
|
| 39 |
+
channel_secret = os.getenv("YOUR_CHANNEL_SECRET")
|
| 40 |
+
channel_access_token = os.getenv("YOUR_CHANNEL_ACCESS_TOKEN")
|
| 41 |
+
configuration = Configuration(access_token=channel_access_token)
|
| 42 |
+
handler = WebhookHandler(channel_secret)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def query(payload: str) -> str:
|
| 46 |
+
"""Send a prompt to Gemini and return the response text."""
|
| 47 |
+
response = chat.send_message(
|
| 48 |
+
config=types.GenerateContentConfig(
|
| 49 |
+
system_instruction="你是一個中文的AI助手,請用繁體中文回答"),
|
| 50 |
+
message=payload
|
| 51 |
+
)
|
| 52 |
+
return response.text
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@app.route("/", methods=["GET"])
|
| 56 |
+
def home():
|
| 57 |
+
"""Health check endpoint."""
|
| 58 |
+
return {"message": "Line Webhook Server"}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@app.route("/", methods=["POST"])
|
| 62 |
+
def callback():
|
| 63 |
+
"""Handle incoming webhook from LINE."""
|
| 64 |
+
signature = request.headers.get("X-Line-Signature")
|
| 65 |
+
body = request.get_data(as_text=True)
|
| 66 |
+
app.logger.info("Request body: %s", body)
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
handler.handle(body, signature)
|
| 70 |
+
except InvalidSignatureError:
|
| 71 |
+
app.logger.warning(
|
| 72 |
+
"Invalid signature. Please check channel credentials."
|
| 73 |
+
)
|
| 74 |
+
abort(400)
|
| 75 |
+
|
| 76 |
+
return "OK"
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
@handler.add(MessageEvent, message=TextMessageContent)
|
| 80 |
+
def handle_text_message(event):
|
| 81 |
+
"""Handle incoming text message event."""
|
| 82 |
+
user_input = event.message.text.strip()
|
| 83 |
+
response_text = query(user_input)
|
| 84 |
+
html_msg = markdown.markdown(response_text)
|
| 85 |
+
soup = BeautifulSoup(html_msg, "html.parser")
|
| 86 |
+
|
| 87 |
+
with ApiClient(configuration) as api_client:
|
| 88 |
+
line_bot_api = MessagingApi(api_client)
|
| 89 |
+
line_bot_api.reply_message_with_http_info(
|
| 90 |
+
ReplyMessageRequest(
|
| 91 |
+
reply_token=event.reply_token,
|
| 92 |
+
messages=[TextMessage(text=soup.get_text())],
|
| 93 |
+
)
|
| 94 |
+
)
|