Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -74,57 +74,6 @@ async def webhook(
|
|
| 74 |
raise HTTPException(status_code=400, detail="Invalid signature")
|
| 75 |
return "ok"
|
| 76 |
|
| 77 |
-
#==========================
|
| 78 |
-
# 使用者上傳圖片
|
| 79 |
-
#==========================
|
| 80 |
-
def get_image_url(message_id):
|
| 81 |
-
try:
|
| 82 |
-
message_content = line_bot_api.get_message_content(message_id)
|
| 83 |
-
file_path = f"/tmp/{message_id}.png"
|
| 84 |
-
with open(file_path, "wb") as f:
|
| 85 |
-
for chunk in message_content.iter_content():
|
| 86 |
-
f.write(chunk)
|
| 87 |
-
return file_path
|
| 88 |
-
except Exception as e:
|
| 89 |
-
print(f"Error getting image: {e}")
|
| 90 |
-
return None
|
| 91 |
-
|
| 92 |
-
# 使用字典模擬用戶訊息歷史存儲
|
| 93 |
-
user_message_history = defaultdict(list)
|
| 94 |
-
def store_user_message(user_id, message_type, message_content):
|
| 95 |
-
"""
|
| 96 |
-
儲存用戶的訊息
|
| 97 |
-
"""
|
| 98 |
-
user_message_history[user_id].append({
|
| 99 |
-
"type": message_type,
|
| 100 |
-
"content": message_content})
|
| 101 |
-
|
| 102 |
-
def analyze_with_gemini(image_path, user_text):
|
| 103 |
-
"""
|
| 104 |
-
分析用戶問題和圖片,並返回 Gemini 的回應
|
| 105 |
-
"""
|
| 106 |
-
try:
|
| 107 |
-
# 確保圖片存在
|
| 108 |
-
if not os.path.exists(image_path):
|
| 109 |
-
raise FileNotFoundError(f"圖片路徑無效:{image_path}")
|
| 110 |
-
|
| 111 |
-
organ = PIL.Image.open(image_path)
|
| 112 |
-
response = chat.send_message([user_text, organ])
|
| 113 |
-
|
| 114 |
-
# 提取回應內容
|
| 115 |
-
return response.text
|
| 116 |
-
|
| 117 |
-
except Exception as e:
|
| 118 |
-
return f"發生錯誤: {e}"
|
| 119 |
-
|
| 120 |
-
def get_previous_message(user_id):
|
| 121 |
-
"""
|
| 122 |
-
獲取用戶的上一則訊息
|
| 123 |
-
"""
|
| 124 |
-
if user_id in user_message_history and len(user_message_history[user_id]) > 0:
|
| 125 |
-
# 返回最後一則訊息
|
| 126 |
-
return user_message_history[user_id][-1]
|
| 127 |
-
return None
|
| 128 |
|
| 129 |
#==========================
|
| 130 |
# 主程式(圖片與文字)
|
|
|
|
| 74 |
raise HTTPException(status_code=400, detail="Invalid signature")
|
| 75 |
return "ok"
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
#==========================
|
| 79 |
# 主程式(圖片與文字)
|