alanchen1115 commited on
Commit
ca680bf
·
verified ·
1 Parent(s): a412fae

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +10 -17
main.py CHANGED
@@ -146,26 +146,19 @@ def analyze_image_with_text(image_path: str, user_text: str) -> str:
146
  return "圖片路徑無效,無法進行分析。"
147
 
148
  img_user = PIL.Image.open(image_path)
149
-
150
- # 建立一個新的聊天模型實例,以確保狀態獨立
151
- # chat_model = ChatGoogleGenerativeAI(
152
- # model="gemini-2.0-flash-exp",
153
- # temperature=0.5,
154
- # top_p=0.5,
155
- # top_k=16
156
- # )
157
  chat_model = client.chats.create(model="gemini-2.0-flash-exp", temperature=0.5, top_p=0.5,top_k=16)
158
 
159
- response = chat_model.invoke([
160
- {"role": "user", "content": [
161
- {"type": "image", "source_type": "url", "url": f"file://{image_path}"},
162
- {"type": "text", "text": user_text}
163
- ]}
164
- ])
 
 
165
 
166
- return response.content
167
- except Exception as e:
168
- return f"圖片分析失敗: {e}"
169
 
170
 
171
  # ==========================
 
146
  return "圖片路徑無效,無法進行分析。"
147
 
148
  img_user = PIL.Image.open(image_path)
149
+
 
 
 
 
 
 
 
150
  chat_model = client.chats.create(model="gemini-2.0-flash-exp", temperature=0.5, top_p=0.5,top_k=16)
151
 
152
+ response = chat_model.send_message([img_usr, user_text])
153
+ if (response.text != None):
154
+ out = response.text
155
+ else:
156
+ out = "Gemini沒答案!請換個說法!"
157
+ except:
158
+ # 處理錯誤
159
+ out = "Gemini執行出錯!請換個說法!"
160
 
161
+ return out
 
 
162
 
163
 
164
  # ==========================