Spaces:
Runtime error
Runtime error
Delete Uploading_images_file.py
Browse files- Uploading_images_file.py +0 -61
Uploading_images_file.py
DELETED
|
@@ -1,61 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import io
|
| 3 |
-
import PIL.Image
|
| 4 |
-
from collections import defaultdict
|
| 5 |
-
from linebot import LineBotApi
|
| 6 |
-
|
| 7 |
-
#==========================
|
| 8 |
-
# 使用者上傳圖片
|
| 9 |
-
#==========================
|
| 10 |
-
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
| 11 |
-
def get_image_url(message_id):
|
| 12 |
-
try:
|
| 13 |
-
message_content = line_bot_api.get_message_content(message_id)
|
| 14 |
-
file_path = f"/tmp/{message_id}.png"
|
| 15 |
-
with open(file_path, "wb") as f:
|
| 16 |
-
for chunk in message_content.iter_content():
|
| 17 |
-
f.write(chunk)
|
| 18 |
-
print(f"✅ 圖片成功儲存到:{file_path}")
|
| 19 |
-
return file_path
|
| 20 |
-
except Exception as e:
|
| 21 |
-
print(f"❌ 圖片取得失敗:{e}")
|
| 22 |
-
return None
|
| 23 |
-
|
| 24 |
-
# 使用字典模擬用戶訊息歷史存儲
|
| 25 |
-
user_message_history = defaultdict(list)
|
| 26 |
-
def store_user_message(user_id, message_type, message_content):
|
| 27 |
-
"""
|
| 28 |
-
儲存用戶的訊息
|
| 29 |
-
"""
|
| 30 |
-
user_message_history[user_id].append({
|
| 31 |
-
"type": message_type,
|
| 32 |
-
"content": message_content})
|
| 33 |
-
|
| 34 |
-
def analyze_with_gemini(image_path, user_text):
|
| 35 |
-
"""
|
| 36 |
-
分析用戶問題和圖片,並返回 Gemini 的回應
|
| 37 |
-
"""
|
| 38 |
-
try:
|
| 39 |
-
# 確保圖片存在
|
| 40 |
-
if not os.path.exists(image_path):
|
| 41 |
-
raise FileNotFoundError(f"圖片路徑無效:{image_path}")
|
| 42 |
-
|
| 43 |
-
img_user = PIL.Image.open(image_path)
|
| 44 |
-
response = chat.send_message([user_text, img_user])
|
| 45 |
-
|
| 46 |
-
# 提取回應內容
|
| 47 |
-
return response.text
|
| 48 |
-
|
| 49 |
-
except Exception as e:
|
| 50 |
-
return f"發生錯誤: {e}"
|
| 51 |
-
|
| 52 |
-
def get_previous_message(user_id):
|
| 53 |
-
"""
|
| 54 |
-
獲取用戶的上一則訊息
|
| 55 |
-
"""
|
| 56 |
-
if user_id in user_message_history and len(user_message_history[user_id]) > 0:
|
| 57 |
-
# 返回最後一則訊息
|
| 58 |
-
return user_message_history[user_id][-1]
|
| 59 |
-
return {
|
| 60 |
-
"type": 'text',
|
| 61 |
-
"content": 'No message!'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|