Update main.py
Browse files
main.py
CHANGED
@@ -5,6 +5,7 @@ from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, s
|
|
5 |
import google.generativeai as genai
|
6 |
import openai
|
7 |
import base64
|
|
|
8 |
from linebot import LineBotApi, WebhookHandler
|
9 |
from linebot.exceptions import InvalidSignatureError
|
10 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage, ImageMessage
|
@@ -66,45 +67,6 @@ async def webhook(
|
|
66 |
def handle_message(event):
|
67 |
global working_status
|
68 |
|
69 |
-
# 檢查事件類型和訊息類型
|
70 |
-
if event.type != "message" or event.message.type != "text":
|
71 |
-
# 回覆錯誤訊息
|
72 |
-
line_bot_api.reply_message(
|
73 |
-
event.reply_token,
|
74 |
-
TextSendMessage(text="Event type error:[No message or the message does not contain text]")
|
75 |
-
)
|
76 |
-
|
77 |
-
# 檢查使用者是否輸入 "再見"
|
78 |
-
elif event.message.text == "再見":
|
79 |
-
# 回覆 "Bye!"
|
80 |
-
line_bot_api.reply_message(
|
81 |
-
event.reply_token,
|
82 |
-
TextSendMessage(text="Bye!")
|
83 |
-
)
|
84 |
-
return
|
85 |
-
|
86 |
-
# 檢查是否正在與使用者交談
|
87 |
-
elif working_status:
|
88 |
-
try:
|
89 |
-
# 取得使用者輸入的文字
|
90 |
-
prompt = event.message.text
|
91 |
-
# 使用 Gemini 模型生成文字
|
92 |
-
completion = model.generate_content(prompt, generation_config=generation_config)
|
93 |
-
# 檢查生成結果是否為空
|
94 |
-
if (completion.parts[0].text != None):
|
95 |
-
# 取得生成結果
|
96 |
-
out = completion.parts[0].text
|
97 |
-
else:
|
98 |
-
# 回覆 "Gemini沒答案!請換個說法!"
|
99 |
-
out = "我不太懂什麼意思也~"
|
100 |
-
except:
|
101 |
-
# 處理錯誤
|
102 |
-
out = "Gemini執行出錯!請換個說法!"
|
103 |
-
|
104 |
-
# 回覆生成結果
|
105 |
-
line_bot_api.reply_message(
|
106 |
-
event.reply_token,
|
107 |
-
TextSendMessage(text=out))
|
108 |
|
109 |
|
110 |
|
@@ -162,13 +124,31 @@ def analyze_with_openai(image_path, user_text):
|
|
162 |
)
|
163 |
|
164 |
return response["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
@line_handler.add(MessageEvent, message=ImageMessage)
|
168 |
def handle_image_message(event):
|
169 |
# 從 LINE API 獲取圖片數據
|
170 |
image_path = get_image_url(event.message.id)
|
171 |
-
|
172 |
if image_path:
|
173 |
# 處理圖片或進行進一步分析
|
174 |
print(f"圖片已保存至: {image_path}")
|
@@ -176,7 +156,7 @@ def handle_image_message(event):
|
|
176 |
# 回應用戶
|
177 |
line_bot_api.reply_message(
|
178 |
event.reply_token,
|
179 |
-
TextSendMessage(text="
|
180 |
)
|
181 |
|
182 |
else:
|
@@ -185,22 +165,62 @@ def handle_image_message(event):
|
|
185 |
TextSendMessage(text="無法下載圖片,請稍後再試!")
|
186 |
)
|
187 |
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
event.reply_token,
|
193 |
-
TextSendMessage(text="
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
-
# 將圖片與文字結合發送到 OpenAI
|
198 |
-
response_text = analyze_with_openai(image_path, user_text)
|
199 |
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
204 |
|
205 |
if __name__ == "__main__":
|
206 |
# 啟動 FastAPI 應用程式
|
|
|
5 |
import google.generativeai as genai
|
6 |
import openai
|
7 |
import base64
|
8 |
+
from collections import defaultdict
|
9 |
from linebot import LineBotApi, WebhookHandler
|
10 |
from linebot.exceptions import InvalidSignatureError
|
11 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage, ImageMessage
|
|
|
67 |
def handle_message(event):
|
68 |
global working_status
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
|
72 |
|
|
|
124 |
)
|
125 |
|
126 |
return response["choices"][0]["message"]["content"]
|
127 |
+
|
128 |
+
# 使用字典模擬用戶訊息歷史存儲
|
129 |
+
user_message_history = defaultdict(list)
|
130 |
|
131 |
+
def store_user_message(user_id, message_type, message_content):
|
132 |
+
"""
|
133 |
+
儲存用戶的訊息
|
134 |
+
"""
|
135 |
+
user_message_history[user_id].append({
|
136 |
+
"type": message_type,
|
137 |
+
"content": message_content
|
138 |
+
})
|
139 |
+
|
140 |
+
def get_previous_message(user_id):
|
141 |
+
"""
|
142 |
+
獲取用戶的上一則訊息
|
143 |
+
"""
|
144 |
+
if user_id in user_message_history and len(user_message_history[user_id]) > 1:
|
145 |
+
# 返回倒數第二則訊息(上一則)
|
146 |
+
return us
|
147 |
|
148 |
@line_handler.add(MessageEvent, message=ImageMessage)
|
149 |
def handle_image_message(event):
|
150 |
# 從 LINE API 獲取圖片數據
|
151 |
image_path = get_image_url(event.message.id)
|
|
|
152 |
if image_path:
|
153 |
# 處理圖片或進行進一步分析
|
154 |
print(f"圖片已保存至: {image_path}")
|
|
|
156 |
# 回應用戶
|
157 |
line_bot_api.reply_message(
|
158 |
event.reply_token,
|
159 |
+
TextSendMessage(text="圖片已接收成功囉,幫我輸入你想詢問的問題喔~")
|
160 |
)
|
161 |
|
162 |
else:
|
|
|
165 |
TextSendMessage(text="無法下載圖片,請稍後再試!")
|
166 |
)
|
167 |
|
168 |
+
|
169 |
+
user_id = event.source.user_id
|
170 |
+
store_user_message(user_id, "text", user_text)
|
171 |
+
previous_message = get_previous_message(user_id)
|
172 |
+
|
173 |
+
# 獲取傳送圖片後的用戶問題(文字)
|
174 |
+
if previous_message and previous_message["type"] == "image":
|
175 |
+
# 獲取用戶問題(文字)
|
176 |
+
user_text = event.message.text # 自行實現,根據需求追蹤用戶文字
|
177 |
+
# 將圖片與文字結合發送到 OpenAI
|
178 |
+
out = analyze_with_openai(image_path, user_text)
|
179 |
+
|
180 |
+
else:
|
181 |
+
# 檢查事件類型和訊息類型
|
182 |
+
if event.type != "message" or event.message.type != "text":
|
183 |
+
# 回覆錯誤訊息
|
184 |
+
line_bot_api.reply_message(
|
185 |
event.reply_token,
|
186 |
+
TextSendMessage(text="Event type error:[No message or the message does not contain text]")
|
187 |
+
)
|
188 |
+
|
189 |
+
# 檢查使用者是否輸入 "再見"
|
190 |
+
elif event.message.text == "再見":
|
191 |
+
# 回覆 "Bye!"
|
192 |
+
line_bot_api.reply_message(
|
193 |
+
event.reply_token,
|
194 |
+
TextSendMessage(text="Bye!")
|
195 |
+
)
|
196 |
+
return
|
197 |
+
|
198 |
+
# 檢查是否正在與使用者交談
|
199 |
+
elif working_status:
|
200 |
+
try:
|
201 |
+
# 取得使用者輸入的文字
|
202 |
+
prompt = event.message.text
|
203 |
+
# 使用 Gemini 模型生成文字
|
204 |
+
completion = model.generate_content(prompt, generation_config=generation_config)
|
205 |
+
# 檢查生成結果是否為空
|
206 |
+
if (completion.parts[0].text != None):
|
207 |
+
# 取得生成結果
|
208 |
+
out = completion.parts[0].text
|
209 |
+
else:
|
210 |
+
# 回覆 "Gemini沒答案!請換個說法!"
|
211 |
+
out = "我不太懂什麼意思也~"
|
212 |
+
except:
|
213 |
+
# 處理錯誤
|
214 |
+
out = "執行出錯!請換個說法!"
|
215 |
|
|
|
|
|
216 |
|
217 |
+
|
218 |
+
# 回覆生成結果
|
219 |
+
line_bot_api.reply_message(
|
220 |
+
event.reply_token,
|
221 |
+
TextSendMessage(text=out))
|
222 |
+
|
223 |
+
|
224 |
|
225 |
if __name__ == "__main__":
|
226 |
# 啟動 FastAPI 應用程式
|