huchiahsi commited on
Commit
60d6fec
·
verified ·
1 Parent(s): e442330

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +48 -58
main.py CHANGED
@@ -1,16 +1,17 @@
1
- # 東吳大學資料系2025年LINEBOT
2
-
3
- from flask import Flask, request, abort, send_from_directory
4
 
 
 
 
 
5
  import markdown
 
 
6
  from bs4 import BeautifulSoup
 
7
 
8
- from linebot.v3 import (
9
- WebhookHandler
10
- )
11
- from linebot.v3.exceptions import (
12
- InvalidSignatureError
13
- )
14
  from linebot.v3.messaging import (
15
  Configuration,
16
  ApiClient,
@@ -26,77 +27,69 @@ from linebot.v3.webhooks import (
26
  ImageMessageContent
27
  )
28
 
29
- import os
30
- import requests
31
- import logging
32
- import tempfile
33
- import google.generativeai as genai
34
-
35
- # HF_TOKEN = os.environ.get('HF_TOKEN')
36
- # headers = {"Authorization": f"Bearer {HF_TOKEN}"}
37
-
38
- GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
39
  genai.configure(api_key=GOOGLE_API_KEY)
40
- model = genai.GenerativeModel('gemini-2.0-flash')
41
 
 
42
  static_tmp_path = "/tmp"
43
  os.makedirs(static_tmp_path, exist_ok=True)
 
 
 
 
 
 
44
 
45
- # API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
 
 
 
 
 
 
46
  def query(payload):
47
- # response = requests.post(API_URL, headers=headers, json=payload)
48
  response = model.generate_content(payload)
49
  return response.text
50
 
51
- app = Flask(__name__)
52
-
53
- ### 將/tmp資料夾中的圖片建立成URL
54
-
55
  @app.route("/images/<filename>")
56
  def serve_image(filename):
57
- return send_from_directory("/tmp", filename)
58
-
59
- base_url = os.getenv("SPACE_HOST")
60
- ###
61
-
62
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
63
- app.logger.setLevel(logging.INFO)
64
 
65
- channel_secret = os.environ.get('YOUR_CHANNEL_SECRET')
66
- channel_access_token = os.environ.get('YOUR_CHANNEL_ACCESS_TOKEN')
67
-
68
- configuration = Configuration(access_token=channel_access_token)
69
- handler = WebhookHandler(channel_secret)
70
 
 
71
  @app.route("/")
72
  def home():
73
  return {"message": "Line Webhook Server"}
74
 
75
- @app.route("/", methods=['POST'])
76
  def callback():
77
- # get X-Line-Signature header value
78
- signature = request.headers['X-Line-Signature']
79
-
80
- # get request body as text
81
  body = request.get_data(as_text=True)
82
- app.logger.info("Request body: " + body)
83
 
84
- # handle webhook body
85
  try:
86
  handler.handle(body, signature)
87
  except InvalidSignatureError:
88
- app.logger.info("Invalid signature. Please check your channel access token/channel secret.")
89
  abort(400)
90
 
91
- return 'OK'
92
 
 
93
  @handler.add(MessageEvent, message=TextMessageContent)
94
- def handle_message(event):
95
  with ApiClient(configuration) as api_client:
96
  line_bot_api = MessagingApi(api_client)
97
  response = query(event.message.text)
98
  html_msg = markdown.markdown(response)
99
- soup = BeautifulSoup(html_msg, 'html.parser')
 
100
  line_bot_api.reply_message_with_http_info(
101
  ReplyMessageRequest(
102
  reply_token=event.reply_token,
@@ -104,19 +97,21 @@ def handle_message(event):
104
  )
105
  )
106
 
 
107
  @handler.add(MessageEvent, message=ImageMessageContent)
108
- def handle_content_message(event):
109
- ext = 'jpg'
110
  with ApiClient(configuration) as api_client:
111
  blob_api = MessagingApiBlob(api_client)
112
  content = blob_api.get_message_content(message_id=event.message.id)
113
 
114
- with tempfile.NamedTemporaryFile(dir=static_tmp_path, suffix='.' + ext, delete=False) as tf:
115
  tf.write(content)
116
  filename = os.path.basename(tf.name)
117
 
118
  image_url = f"https://{base_url}/images/{filename}"
119
- app.logger.info(image_url)
 
120
  with ApiClient(configuration) as api_client:
121
  line_bot_api = MessagingApi(api_client)
122
  line_bot_api.reply_message(
@@ -130,8 +125,3 @@ def handle_content_message(event):
130
  ]
131
  )
132
  )
133
-
134
-
135
- @app.route('/static/<path:path>')
136
- def send_static_content(path):
137
- return send_from_directory('static', path)
 
1
+ # 東吳大學資料系 2025 LINEBOT
 
 
2
 
3
+ import os
4
+ import tempfile
5
+ import logging
6
+ import requests
7
  import markdown
8
+
9
+ from flask import Flask, request, abort, send_from_directory
10
  from bs4 import BeautifulSoup
11
+ import google.generativeai as genai
12
 
13
+ from linebot.v3 import WebhookHandler
14
+ from linebot.v3.exceptions import InvalidSignatureError
 
 
 
 
15
  from linebot.v3.messaging import (
16
  Configuration,
17
  ApiClient,
 
27
  ImageMessageContent
28
  )
29
 
30
+ # === 初始化 Google Gemini ===
31
+ GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
 
 
 
 
 
 
 
 
32
  genai.configure(api_key=GOOGLE_API_KEY)
33
+ model = genai.GenerativeModel("gemini-2.0-flash")
34
 
35
+ # === 初始設定 ===
36
  static_tmp_path = "/tmp"
37
  os.makedirs(static_tmp_path, exist_ok=True)
38
+ base_url = os.getenv("SPACE_HOST") # e.g., "your-space-name.hf.space"
39
+
40
+ # === Flask 應用初始化 ===
41
+ app = Flask(__name__)
42
+ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
43
+ app.logger.setLevel(logging.INFO)
44
 
45
+ channel_secret = os.environ.get("YOUR_CHANNEL_SECRET")
46
+ channel_access_token = os.environ.get("YOUR_CHANNEL_ACCESS_TOKEN")
47
+
48
+ configuration = Configuration(access_token=channel_access_token)
49
+ handler = WebhookHandler(channel_secret)
50
+
51
+ # === AI Query 包裝 ===
52
  def query(payload):
 
53
  response = model.generate_content(payload)
54
  return response.text
55
 
56
+ # === 靜態圖檔路由 ===
 
 
 
57
  @app.route("/images/<filename>")
58
  def serve_image(filename):
59
+ return send_from_directory(static_tmp_path, filename)
 
 
 
 
 
 
60
 
61
+ @app.route("/static/<path:path>")
62
+ def send_static_content(path):
63
+ return send_from_directory("static", path)
 
 
64
 
65
+ # === LINE Webhook 接收端點 ===
66
  @app.route("/")
67
  def home():
68
  return {"message": "Line Webhook Server"}
69
 
70
+ @app.route("/", methods=["POST"])
71
  def callback():
72
+ signature = request.headers.get("X-Line-Signature")
 
 
 
73
  body = request.get_data(as_text=True)
74
+ app.logger.info(f"Request body: {body}")
75
 
 
76
  try:
77
  handler.handle(body, signature)
78
  except InvalidSignatureError:
79
+ app.logger.warning("Invalid signature. Please check channel credentials.")
80
  abort(400)
81
 
82
+ return "OK"
83
 
84
+ # === 處理文字訊息 ===
85
  @handler.add(MessageEvent, message=TextMessageContent)
86
+ def handle_text_message(event):
87
  with ApiClient(configuration) as api_client:
88
  line_bot_api = MessagingApi(api_client)
89
  response = query(event.message.text)
90
  html_msg = markdown.markdown(response)
91
+ soup = BeautifulSoup(html_msg, "html.parser")
92
+
93
  line_bot_api.reply_message_with_http_info(
94
  ReplyMessageRequest(
95
  reply_token=event.reply_token,
 
97
  )
98
  )
99
 
100
+ # === 處理圖片訊息 ===
101
  @handler.add(MessageEvent, message=ImageMessageContent)
102
+ def handle_image_message(event):
103
+ ext = "jpg"
104
  with ApiClient(configuration) as api_client:
105
  blob_api = MessagingApiBlob(api_client)
106
  content = blob_api.get_message_content(message_id=event.message.id)
107
 
108
+ with tempfile.NamedTemporaryFile(dir=static_tmp_path, suffix=f".{ext}", delete=False) as tf:
109
  tf.write(content)
110
  filename = os.path.basename(tf.name)
111
 
112
  image_url = f"https://{base_url}/images/{filename}"
113
+ app.logger.info(f"Image URL: {image_url}")
114
+
115
  with ApiClient(configuration) as api_client:
116
  line_bot_api = MessagingApi(api_client)
117
  line_bot_api.reply_message(
 
125
  ]
126
  )
127
  )