Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
-
#
|
| 2 |
|
| 3 |
import os
|
| 4 |
import tempfile
|
| 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,6 +29,8 @@ from linebot.v3.webhooks import (
|
|
| 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)
|
|
@@ -146,13 +149,15 @@ def handle_image_message(event):
|
|
| 146 |
|
| 147 |
# === 以下是處理圖片回傳部分 === #
|
| 148 |
|
| 149 |
-
ext = "jpg"
|
| 150 |
with ApiClient(configuration) as api_client:
|
| 151 |
blob_api = MessagingApiBlob(api_client)
|
| 152 |
content = blob_api.get_message_content(message_id=event.message.id)
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
| 156 |
filename = os.path.basename(tf.name)
|
| 157 |
|
| 158 |
image_url2 = f"https://{base_url}/images/{filename}"
|
|
|
|
| 1 |
+
# ===東吳大學資料系 2025 年 LINEBOT ===
|
| 2 |
|
| 3 |
import os
|
| 4 |
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 |
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)
|
|
|
|
| 149 |
|
| 150 |
# === 以下是處理圖片回傳部分 === #
|
| 151 |
|
|
|
|
| 152 |
with ApiClient(configuration) as api_client:
|
| 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 |
+
# tf.write(content)
|
| 160 |
+
image.save(tf, format="JPEG")
|
| 161 |
filename = os.path.basename(tf.name)
|
| 162 |
|
| 163 |
image_url2 = f"https://{base_url}/images/{filename}"
|