Update main.py
Browse files
main.py
CHANGED
|
@@ -66,16 +66,12 @@ async def webhook(
|
|
| 66 |
#==========================
|
| 67 |
# 使用者請求生成圖片
|
| 68 |
#==========================
|
| 69 |
-
def upload_image_to_imgur_with_token(
|
| 70 |
"""
|
| 71 |
-
使用 Imgur OAuth2 Access Token
|
| 72 |
"""
|
| 73 |
try:
|
| 74 |
-
with open(image_path, "rb") as img_file:
|
| 75 |
-
image_binary = img_file.read()
|
| 76 |
-
|
| 77 |
headers = {"Authorization": f"Bearer {access_token}"}
|
| 78 |
-
|
| 79 |
data = {
|
| 80 |
"image": base64.b64encode(image_binary).decode("utf-8"),
|
| 81 |
"type": "base64",
|
|
@@ -90,8 +86,7 @@ def upload_image_to_imgur_with_token(image_path, access_token):
|
|
| 90 |
except Exception as e:
|
| 91 |
print("圖片上傳例外:", e)
|
| 92 |
return None
|
| 93 |
-
|
| 94 |
-
|
| 95 |
#==========================
|
| 96 |
# 使用者上傳圖片
|
| 97 |
#==========================
|
|
@@ -123,6 +118,7 @@ def analyze_with_gemini(image_path, user_text):
|
|
| 123 |
"""
|
| 124 |
try:
|
| 125 |
# 確保圖片存在
|
|
|
|
| 126 |
if not os.path.exists(image_path):
|
| 127 |
raise FileNotFoundError(f"圖片路徑無效:{image_path}")
|
| 128 |
|
|
@@ -157,7 +153,7 @@ def handle_image_message(event):
|
|
| 157 |
chat_sessions[user_id] = chat
|
| 158 |
|
| 159 |
user_text = event.message.text if event.message.type == "text" else None
|
| 160 |
-
|
| 161 |
if user_text and user_text.startswith("請幫我生成圖片"):
|
| 162 |
prompt = user_text.replace("請幫我生成圖片", "").strip()
|
| 163 |
|
|
@@ -168,6 +164,7 @@ def handle_image_message(event):
|
|
| 168 |
image_data = response.parts[0].inline_data.data
|
| 169 |
access_token = os.environ.get("IMGUR_ACCESS_TOKEN")
|
| 170 |
image_url = upload_image_to_imgur_with_token(image_data,access_token)
|
|
|
|
| 171 |
|
| 172 |
if image_url:
|
| 173 |
line_bot_api.reply_message(
|
|
|
|
| 66 |
#==========================
|
| 67 |
# 使用者請求生成圖片
|
| 68 |
#==========================
|
| 69 |
+
def upload_image_to_imgur_with_token(image_binary, access_token):
|
| 70 |
"""
|
| 71 |
+
使用 Imgur OAuth2 Access Token 上傳圖片(直接接收 binary,不需存檔)
|
| 72 |
"""
|
| 73 |
try:
|
|
|
|
|
|
|
|
|
|
| 74 |
headers = {"Authorization": f"Bearer {access_token}"}
|
|
|
|
| 75 |
data = {
|
| 76 |
"image": base64.b64encode(image_binary).decode("utf-8"),
|
| 77 |
"type": "base64",
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
print("圖片上傳例外:", e)
|
| 88 |
return None
|
| 89 |
+
|
|
|
|
| 90 |
#==========================
|
| 91 |
# 使用者上傳圖片
|
| 92 |
#==========================
|
|
|
|
| 118 |
"""
|
| 119 |
try:
|
| 120 |
# 確保圖片存在
|
| 121 |
+
print("上傳圖片至 Imgur...")
|
| 122 |
if not os.path.exists(image_path):
|
| 123 |
raise FileNotFoundError(f"圖片路徑無效:{image_path}")
|
| 124 |
|
|
|
|
| 153 |
chat_sessions[user_id] = chat
|
| 154 |
|
| 155 |
user_text = event.message.text if event.message.type == "text" else None
|
| 156 |
+
image_url = None
|
| 157 |
if user_text and user_text.startswith("請幫我生成圖片"):
|
| 158 |
prompt = user_text.replace("請幫我生成圖片", "").strip()
|
| 159 |
|
|
|
|
| 164 |
image_data = response.parts[0].inline_data.data
|
| 165 |
access_token = os.environ.get("IMGUR_ACCESS_TOKEN")
|
| 166 |
image_url = upload_image_to_imgur_with_token(image_data,access_token)
|
| 167 |
+
print("Imgur 回傳 URL:", image_url)
|
| 168 |
|
| 169 |
if image_url:
|
| 170 |
line_bot_api.reply_message(
|