Update main.py
Browse files
main.py
CHANGED
@@ -20,14 +20,16 @@ from Uploading_images_file import get_image_url, store_user_message, analyze_wit
|
|
20 |
#==========================
|
21 |
|
22 |
# 設定 Google AI API 金鑰
|
23 |
-
google_api = os.environ["GOOGLE_API_KEY"]
|
24 |
-
genai_gen.configure(api_key=google_api)
|
|
|
25 |
|
26 |
# 設定生成文字的參數
|
27 |
-
generation_config = genai_gen.types.GenerationConfig(max_output_tokens=1000, temperature=0.2, top_p=0.5, top_k=16)
|
|
|
28 |
|
29 |
# 使用 gemini-2.0-flash-exp 模型
|
30 |
-
model = genai_gen.GenerativeModel('gemini-2.0-flash', system_instruction="主要用繁體中文回答,但如果用戶使用詢問英文問題,就用英文回應。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題", generation_config=generation_config)
|
31 |
|
32 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
33 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
@@ -109,7 +111,7 @@ def handle_image_message(event):
|
|
109 |
image_binary = image_generator.generate_image_with_gemini(prompt)
|
110 |
|
111 |
if image_binary:
|
112 |
-
image_url = 'https://alanchen1115-linebot.hf.space/'+image_generator.upload_image_to_tmp(image_binary)
|
113 |
|
114 |
if image_url:
|
115 |
# 使用 push message 發送圖片,避免 reply token 超時
|
@@ -137,7 +139,13 @@ def handle_image_message(event):
|
|
137 |
# ========
|
138 |
elif event.message.type == "text" and previous_message["type"] != "image":
|
139 |
try:
|
140 |
-
response = chat.send_message(user_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
if (response.text != None):
|
142 |
out = response.text
|
143 |
else:
|
@@ -154,8 +162,15 @@ def handle_image_message(event):
|
|
154 |
if not os.path.exists(image_path):
|
155 |
raise FileNotFoundError(f"圖片路徑無效:{image_path}")
|
156 |
previous_img = PIL.Image.open(image_path)
|
157 |
-
completion = chat.send_message([user_text, previous_img])
|
158 |
-
out = completion.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
except Exception as e:
|
160 |
out = f"發生錯誤: {e}"
|
161 |
|
|
|
20 |
#==========================
|
21 |
|
22 |
# 設定 Google AI API 金鑰
|
23 |
+
# google_api = os.environ["GOOGLE_API_KEY"]
|
24 |
+
# genai_gen.configure(api_key=google_api)
|
25 |
+
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
|
26 |
|
27 |
# 設定生成文字的參數
|
28 |
+
# generation_config = genai_gen.types.GenerationConfig(max_output_tokens=1000, temperature=0.2, top_p=0.5, top_k=16)
|
29 |
+
generation_config = genai.types.GenerateContentConfig(max_output_tokens=256, temperature=0.5, top_p=0.5, top_k=16)
|
30 |
|
31 |
# 使用 gemini-2.0-flash-exp 模型
|
32 |
+
# model = genai_gen.GenerativeModel('gemini-2.0-flash', system_instruction="主要用繁體中文回答,但如果用戶使用詢問英文問題,就用英文回應。你現在是個專業助理,職稱為OPEN小助理,個性活潑、樂觀,願意回答所有問題", generation_config=generation_config)
|
33 |
|
34 |
# 設定 Line Bot 的 API 金鑰和秘密金鑰
|
35 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
|
|
111 |
image_binary = image_generator.generate_image_with_gemini(prompt)
|
112 |
|
113 |
if image_binary:
|
114 |
+
image_url = 'https://alanchen1115-linebot.hf.space/'+image_generator.upload_image_to_tmp(image_binary) ### 要改 https
|
115 |
|
116 |
if image_url:
|
117 |
# 使用 push message 發送圖片,避免 reply token 超時
|
|
|
139 |
# ========
|
140 |
elif event.message.type == "text" and previous_message["type"] != "image":
|
141 |
try:
|
142 |
+
# response = chat.send_message(user_text)
|
143 |
+
user_id = event.source.user_id
|
144 |
+
chat = chat_sessions.get(user_id) or client.chats.create(model="gemini-2.0-flash", config=generation_config)
|
145 |
+
chat_sessions[user_id] = chat
|
146 |
+
# 取得使用者輸入的文字
|
147 |
+
user_input = event.message.text
|
148 |
+
response = chat.send_message(user_input)
|
149 |
if (response.text != None):
|
150 |
out = response.text
|
151 |
else:
|
|
|
162 |
if not os.path.exists(image_path):
|
163 |
raise FileNotFoundError(f"圖片路徑無效:{image_path}")
|
164 |
previous_img = PIL.Image.open(image_path)
|
165 |
+
# completion = chat.send_message([user_text, previous_img])
|
166 |
+
# out = completion.text
|
167 |
+
user_id = event.source.user_id
|
168 |
+
chat = chat_sessions.get(user_id) or client.chats.create(model="gemini-2.0-flash", config=generation_config)
|
169 |
+
chat_sessions[user_id] = chat
|
170 |
+
# 取得使用者輸入的文字
|
171 |
+
user_input = event.message.text
|
172 |
+
response = chat.send_message([previous_img, user_input])
|
173 |
+
|
174 |
except Exception as e:
|
175 |
out = f"發生錯誤: {e}"
|
176 |
|