Spaces:
Sleeping
Sleeping
base64
Browse files
main.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
# ===東吳大學資料系 2025 年 LINEBOT ===
|
|
|
|
| 2 |
|
| 3 |
import io
|
| 4 |
from PIL import Image
|
|
@@ -156,6 +157,13 @@ def handle_image_message(event):
|
|
| 156 |
with ApiClient(configuration) as api_client:
|
| 157 |
blob_api = MessagingApiBlob(api_client)
|
| 158 |
content = blob_api.get_message_content(message_id=event.message.id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
# 使用 Pillow 讀取圖像
|
| 161 |
image = Image.open(io.BytesIO(content))
|
|
@@ -173,7 +181,6 @@ def handle_image_message(event):
|
|
| 173 |
app.logger.info(f"Image URL: {image_url}")
|
| 174 |
|
| 175 |
# === 以下是處理解釋圖片部分 === #
|
| 176 |
-
'''
|
| 177 |
response = client.responses.create(
|
| 178 |
model="gpt-4o-mini",
|
| 179 |
input=[{
|
|
@@ -182,13 +189,14 @@ def handle_image_message(event):
|
|
| 182 |
{"type": "input_text", "text": "describe the image in traditional chinese"},
|
| 183 |
{
|
| 184 |
"type": "input_image",
|
| 185 |
-
"image_url":
|
| 186 |
},
|
| 187 |
],
|
| 188 |
}],
|
| 189 |
)
|
| 190 |
app.logger.info(response.output_text)
|
| 191 |
-
|
|
|
|
| 192 |
|
| 193 |
with ApiClient(configuration) as api_client:
|
| 194 |
line_bot_api = MessagingApi(api_client)
|
|
|
|
| 1 |
# ===東吳大學資料系 2025 年 LINEBOT ===
|
| 2 |
+
import base64
|
| 3 |
|
| 4 |
import io
|
| 5 |
from PIL import Image
|
|
|
|
| 157 |
with ApiClient(configuration) as api_client:
|
| 158 |
blob_api = MessagingApiBlob(api_client)
|
| 159 |
content = blob_api.get_message_content(message_id=event.message.id)
|
| 160 |
+
image_bytes = content.data
|
| 161 |
+
|
| 162 |
+
# Step 2:轉成 base64 字串
|
| 163 |
+
base64_string = base64.b64encode(image_bytes).decode("utf-8")
|
| 164 |
+
|
| 165 |
+
# Step 3:組成 OpenAI 的 data URI 格式
|
| 166 |
+
data_uri = f"data:image/png;base64,{base64_string}"
|
| 167 |
|
| 168 |
# 使用 Pillow 讀取圖像
|
| 169 |
image = Image.open(io.BytesIO(content))
|
|
|
|
| 181 |
app.logger.info(f"Image URL: {image_url}")
|
| 182 |
|
| 183 |
# === 以下是處理解釋圖片部分 === #
|
|
|
|
| 184 |
response = client.responses.create(
|
| 185 |
model="gpt-4o-mini",
|
| 186 |
input=[{
|
|
|
|
| 189 |
{"type": "input_text", "text": "describe the image in traditional chinese"},
|
| 190 |
{
|
| 191 |
"type": "input_image",
|
| 192 |
+
"image_url": data_uri,
|
| 193 |
},
|
| 194 |
],
|
| 195 |
}],
|
| 196 |
)
|
| 197 |
app.logger.info(response.output_text)
|
| 198 |
+
|
| 199 |
+
# === 以下是回傳圖片部分 === #
|
| 200 |
|
| 201 |
with ApiClient(configuration) as api_client:
|
| 202 |
line_bot_api = MessagingApi(api_client)
|