Update main.py
Browse files
main.py
CHANGED
@@ -2,7 +2,7 @@ import json, os
|
|
2 |
import gradio as gr
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
5 |
-
|
6 |
import base64
|
7 |
from collections import defaultdict
|
8 |
from linebot import LineBotApi, WebhookHandler
|
@@ -10,22 +10,15 @@ from linebot.exceptions import InvalidSignatureError
|
|
10 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage, ImageMessage
|
11 |
import PIL.Image
|
12 |
|
13 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
14 |
-
from langchain.memory import ConversationBufferMemory
|
15 |
-
from langchain.chains import ConversationChain
|
16 |
-
|
17 |
# 設定 Google AI API 金鑰
|
18 |
-
|
19 |
|
20 |
# 設定生成文字的參數
|
21 |
-
generation_config =
|
22 |
|
23 |
# 使用 Gemini-1.5-flash 模型
|
24 |
model = genai.GenerativeModel('gemini-2.0-flash-exp', system_instruction="請用繁體中文回答。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題", generation_config=generation_config)
|
25 |
-
|
26 |
-
# 2. 初始化 ConversationChain 和 Memory
|
27 |
-
memory = ConversationBufferMemory()
|
28 |
-
conversation = ConversationChain(llm=model, memory=memory)
|
29 |
|
30 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
31 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
@@ -104,8 +97,6 @@ def analyze_with_gemini(image_path, user_text):
|
|
104 |
|
105 |
organ = PIL.Image.open(image_path)
|
106 |
response = chat.send_message([user_text, organ])
|
107 |
-
#chain = get_or_create_chain(user_id)
|
108 |
-
#completion = chain.run(prompt)
|
109 |
|
110 |
# 提取回應內容
|
111 |
return response.text
|
@@ -122,14 +113,7 @@ def get_previous_message(user_id):
|
|
122 |
# 返回最後一則訊息
|
123 |
return user_message_history[user_id][-1]
|
124 |
return None
|
125 |
-
|
126 |
-
# 分開不同ID的歷史
|
127 |
-
chat_memories = {}
|
128 |
-
def get_or_create_chain(user_id):
|
129 |
-
if user_id not in chat_memories:
|
130 |
-
memory = ConversationBufferMemory()
|
131 |
-
chat_memories[user_id] = ConversationChain(llm=model, memory=memory)
|
132 |
-
return chat_memories[user_id]
|
133 |
|
134 |
@line_handler.add(MessageEvent, message=(ImageMessage,TextMessage))
|
135 |
def handle_image_message(event):
|
@@ -159,7 +143,6 @@ def handle_image_message(event):
|
|
159 |
# 結合圖片與文字分析
|
160 |
out = analyze_with_gemini(image_path, user_text)
|
161 |
|
162 |
-
|
163 |
else:
|
164 |
global working_status
|
165 |
# 檢查事件類型和訊息類型
|
@@ -184,9 +167,7 @@ def handle_image_message(event):
|
|
184 |
store_user_message(user_id, "text", prompt)
|
185 |
|
186 |
# 使用 Gemini 模型生成文字
|
187 |
-
|
188 |
-
chain = get_or_create_chain(user_id)
|
189 |
-
completion = chain.run(prompt) # 使用帶對話歷史的方法
|
190 |
# 檢查生成結果是否為空
|
191 |
if (completion.text != None):
|
192 |
# 取得生成結果
|
|
|
2 |
import gradio as gr
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
5 |
+
import google.generativeai as genai
|
6 |
import base64
|
7 |
from collections import defaultdict
|
8 |
from linebot import LineBotApi, WebhookHandler
|
|
|
10 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage, ImageMessage
|
11 |
import PIL.Image
|
12 |
|
|
|
|
|
|
|
|
|
13 |
# 設定 Google AI API 金鑰
|
14 |
+
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
15 |
|
16 |
# 設定生成文字的參數
|
17 |
+
generation_config = genai.types.GenerationConfig(max_output_tokens=2048, temperature=0.2, top_p=0.5, top_k=16)
|
18 |
|
19 |
# 使用 Gemini-1.5-flash 模型
|
20 |
model = genai.GenerativeModel('gemini-2.0-flash-exp', system_instruction="請用繁體中文回答。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題", generation_config=generation_config)
|
21 |
+
|
|
|
|
|
|
|
22 |
|
23 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
24 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
|
|
97 |
|
98 |
organ = PIL.Image.open(image_path)
|
99 |
response = chat.send_message([user_text, organ])
|
|
|
|
|
100 |
|
101 |
# 提取回應內容
|
102 |
return response.text
|
|
|
113 |
# 返回最後一則訊息
|
114 |
return user_message_history[user_id][-1]
|
115 |
return None
|
116 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
@line_handler.add(MessageEvent, message=(ImageMessage,TextMessage))
|
119 |
def handle_image_message(event):
|
|
|
143 |
# 結合圖片與文字分析
|
144 |
out = analyze_with_gemini(image_path, user_text)
|
145 |
|
|
|
146 |
else:
|
147 |
global working_status
|
148 |
# 檢查事件類型和訊息類型
|
|
|
167 |
store_user_message(user_id, "text", prompt)
|
168 |
|
169 |
# 使用 Gemini 模型生成文字
|
170 |
+
completion = chat.send_message(prompt)
|
|
|
|
|
171 |
# 檢查生成結果是否為空
|
172 |
if (completion.text != None):
|
173 |
# 取得生成結果
|