Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -90,26 +90,28 @@ def analyze_with_gemini(image_path, user_text):
|
|
| 90 |
分析用戶問題和圖片,並返回 Gemini 的回應
|
| 91 |
"""
|
| 92 |
try:
|
| 93 |
-
# 確保圖片存在
|
| 94 |
-
if not os.path.exists(image_path):
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
-
with open(image_path, "rb") as image_file:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# 將二進制數據編碼為 Base64
|
| 102 |
-
image_base64 = base64.b64encode(image_binary).decode('utf-8')
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
{
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
#
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
|
| 114 |
# 提取回應內容
|
| 115 |
return response.parts[0].text
|
|
|
|
| 90 |
分析用戶問題和圖片,並返回 Gemini 的回應
|
| 91 |
"""
|
| 92 |
try:
|
| 93 |
+
# # 確保圖片存在
|
| 94 |
+
# if not os.path.exists(image_path):
|
| 95 |
+
# raise FileNotFoundError(f"圖片路徑無效:{image_path}")
|
| 96 |
|
| 97 |
+
# # 打開本地圖片文件並將其轉換為 Base64 編碼
|
| 98 |
+
# with open(image_path, "rb") as image_file:
|
| 99 |
+
# image_binary = image_file.read()
|
| 100 |
+
|
| 101 |
+
# # 將二進制數據編碼為 Base64
|
| 102 |
+
# image_base64 = base64.b64encode(image_binary).decode('utf-8')
|
| 103 |
+
organ = PIL.Image.open(image_path)
|
| 104 |
+
response = model.generate_content([user_text, organ])
|
| 105 |
+
|
| 106 |
+
# # 構建 Gemini 的請求
|
| 107 |
+
# prompt = f"""
|
| 108 |
+
# 用戶的問題是:{user_text}
|
| 109 |
+
# 圖片已上傳,Base64 編碼如下:
|
| 110 |
+
# {image_base64}
|
| 111 |
+
# 請根據圖片和問題進行詳細分析。
|
| 112 |
+
# """
|
| 113 |
+
# # 使用 Gemini API 請求
|
| 114 |
+
# response = model.generate_content([{'mime_type':'image/png', 'data': image_base64}, prompt])
|
| 115 |
|
| 116 |
# 提取回應內容
|
| 117 |
return response.parts[0].text
|