Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -5,7 +5,6 @@ import tempfile
|
|
| 5 |
import logging
|
| 6 |
import requests
|
| 7 |
import markdown
|
| 8 |
-
import io
|
| 9 |
|
| 10 |
from flask import Flask, request, abort, send_from_directory
|
| 11 |
from bs4 import BeautifulSoup
|
|
@@ -29,8 +28,6 @@ from linebot.v3.webhooks import (
|
|
| 29 |
ImageMessageContent
|
| 30 |
)
|
| 31 |
|
| 32 |
-
from PIL import Image
|
| 33 |
-
|
| 34 |
# === 初始化 Google Gemini ===
|
| 35 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 36 |
genai.configure(api_key=GOOGLE_API_KEY)
|
|
@@ -153,17 +150,14 @@ def handle_image_message(event):
|
|
| 153 |
blob_api = MessagingApiBlob(api_client)
|
| 154 |
content = blob_api.get_message_content(message_id=event.message.id)
|
| 155 |
|
| 156 |
-
image = Image.open(io.BytesIO(content))
|
| 157 |
-
|
| 158 |
with tempfile.NamedTemporaryFile(dir=static_tmp_path, suffix=".jpg", delete=False) as tf:
|
| 159 |
-
|
| 160 |
-
image.save(tf, format="JPEG")
|
| 161 |
filename = os.path.basename(tf.name)
|
| 162 |
|
| 163 |
-
|
| 164 |
# image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
|
| 165 |
|
| 166 |
-
app.logger.info(f"Image URL: {
|
| 167 |
|
| 168 |
# === 以下是處理解釋圖片部分 === #
|
| 169 |
|
|
@@ -172,10 +166,10 @@ def handle_image_message(event):
|
|
| 172 |
input=[{
|
| 173 |
"role": "user",
|
| 174 |
"content": [
|
| 175 |
-
{"type": "input_text", "text": "
|
| 176 |
{
|
| 177 |
"type": "input_image",
|
| 178 |
-
"image_url":
|
| 179 |
},
|
| 180 |
],
|
| 181 |
}],
|
|
|
|
| 5 |
import logging
|
| 6 |
import requests
|
| 7 |
import markdown
|
|
|
|
| 8 |
|
| 9 |
from flask import Flask, request, abort, send_from_directory
|
| 10 |
from bs4 import BeautifulSoup
|
|
|
|
| 28 |
ImageMessageContent
|
| 29 |
)
|
| 30 |
|
|
|
|
|
|
|
| 31 |
# === 初始化 Google Gemini ===
|
| 32 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 33 |
genai.configure(api_key=GOOGLE_API_KEY)
|
|
|
|
| 150 |
blob_api = MessagingApiBlob(api_client)
|
| 151 |
content = blob_api.get_message_content(message_id=event.message.id)
|
| 152 |
|
|
|
|
|
|
|
| 153 |
with tempfile.NamedTemporaryFile(dir=static_tmp_path, suffix=".jpg", delete=False) as tf:
|
| 154 |
+
tf.write(content)
|
|
|
|
| 155 |
filename = os.path.basename(tf.name)
|
| 156 |
|
| 157 |
+
image_url = f"https://{base_url}/images/{filename}"
|
| 158 |
# image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
|
| 159 |
|
| 160 |
+
app.logger.info(f"Image URL: {image_url}")
|
| 161 |
|
| 162 |
# === 以下是處理解釋圖片部分 === #
|
| 163 |
|
|
|
|
| 166 |
input=[{
|
| 167 |
"role": "user",
|
| 168 |
"content": [
|
| 169 |
+
{"type": "input_text", "text": "describe the image in traditional chinese"},
|
| 170 |
{
|
| 171 |
"type": "input_image",
|
| 172 |
+
"image_url": image_url,
|
| 173 |
},
|
| 174 |
],
|
| 175 |
}],
|