Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -91,29 +91,13 @@ def analyze_with_gemini(image_path, user_text):
|
|
| 91 |
分析用戶問題和圖片,並返回 Gemini 的回應
|
| 92 |
"""
|
| 93 |
try:
|
| 94 |
-
#
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
-
# # 打開本地圖片文件並將其轉換為 Base64 編碼
|
| 99 |
-
# with open(image_path, "rb") as image_file:
|
| 100 |
-
# image_binary = image_file.read()
|
| 101 |
-
|
| 102 |
-
# # 將二進制數據編碼為 Base64
|
| 103 |
-
# image_base64 = base64.b64encode(image_binary).decode('utf-8')
|
| 104 |
organ = PIL.Image.open(image_path)
|
| 105 |
-
response = model.
|
| 106 |
|
| 107 |
-
# # 構建 Gemini 的請求
|
| 108 |
-
# prompt = f"""
|
| 109 |
-
# 用戶的問題是:{user_text}
|
| 110 |
-
# 圖片已上傳,Base64 編碼如下:
|
| 111 |
-
# {image_base64}
|
| 112 |
-
# 請根據圖片和問題進行詳細分析。
|
| 113 |
-
# """
|
| 114 |
-
# # 使用 Gemini API 請求
|
| 115 |
-
# response = model.generate_content([{'mime_type':'image/png', 'data': image_base64}, prompt])
|
| 116 |
-
|
| 117 |
# 提取回應內容
|
| 118 |
return response.parts[0].text
|
| 119 |
|
|
@@ -181,7 +165,7 @@ def handle_image_message(event):
|
|
| 181 |
prompt = event.message.text
|
| 182 |
store_user_message(user_id, "text", prompt)
|
| 183 |
# 使用 Gemini 模型生成文字
|
| 184 |
-
completion = model.
|
| 185 |
# 檢查生成結果是否為空
|
| 186 |
if (completion.parts[0].text != None):
|
| 187 |
# 取得生成結果
|
|
|
|
| 91 |
分析用戶問題和圖片,並返回 Gemini 的回應
|
| 92 |
"""
|
| 93 |
try:
|
| 94 |
+
# 確保圖片存在
|
| 95 |
+
if not os.path.exists(image_path):
|
| 96 |
+
raise FileNotFoundError(f"圖片路徑無效:{image_path}")
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
organ = PIL.Image.open(image_path)
|
| 99 |
+
response = model.start_chat([user_text, organ])
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# 提取回應內容
|
| 102 |
return response.parts[0].text
|
| 103 |
|
|
|
|
| 165 |
prompt = event.message.text
|
| 166 |
store_user_message(user_id, "text", prompt)
|
| 167 |
# 使用 Gemini 模型生成文字
|
| 168 |
+
completion = model.start_chat(prompt, generation_config=generation_config)
|
| 169 |
# 檢查生成結果是否為空
|
| 170 |
if (completion.parts[0].text != None):
|
| 171 |
# 取得生成結果
|