Spaces:
Running
Running
顯示完後刪除圖片
Browse files
test.py
CHANGED
|
@@ -3,16 +3,14 @@ import base64
|
|
| 3 |
import logging
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
-
from io import BytesIO
|
| 7 |
import uuid
|
| 8 |
-
|
| 9 |
-
from google import genai
|
| 10 |
-
from google.genai import types
|
| 11 |
-
from PIL import Image
|
| 12 |
|
| 13 |
import markdown
|
| 14 |
from bs4 import BeautifulSoup
|
| 15 |
from flask import Flask, abort, request, send_from_directory
|
|
|
|
|
|
|
| 16 |
from linebot.v3 import WebhookHandler
|
| 17 |
from linebot.v3.exceptions import InvalidSignatureError
|
| 18 |
from linebot.v3.messaging import (
|
|
@@ -26,6 +24,7 @@ from linebot.v3.messaging import (
|
|
| 26 |
)
|
| 27 |
from linebot.v3.webhooks import ImageMessageContent, MessageEvent, TextMessageContent
|
| 28 |
from openai import OpenAI
|
|
|
|
| 29 |
|
| 30 |
# === 初始化 Google Gemini ===
|
| 31 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
@@ -58,8 +57,8 @@ handler = WebhookHandler(channel_secret)
|
|
| 58 |
# === AI Query 包裝 ===
|
| 59 |
def query(payload):
|
| 60 |
response = google_client.models.generate_content(
|
| 61 |
-
|
| 62 |
-
|
| 63 |
)
|
| 64 |
return response.text
|
| 65 |
|
|
@@ -103,8 +102,8 @@ def handle_text_message(event):
|
|
| 103 |
model="gemini-2.0-flash-exp-image-generation",
|
| 104 |
contents=prompt,
|
| 105 |
config=types.GenerateContentConfig(
|
| 106 |
-
response_modalities=[
|
| 107 |
-
)
|
| 108 |
)
|
| 109 |
|
| 110 |
# 處理回應中的圖片
|
|
@@ -118,7 +117,7 @@ def handle_text_message(event):
|
|
| 118 |
# 建立圖片的公開 URL
|
| 119 |
image_url = f"https://{base_url}/images/{filename}"
|
| 120 |
app.logger.info(f"Image URL: {image_url}")
|
| 121 |
-
|
| 122 |
# 回傳圖片給 LINE 使用者
|
| 123 |
with ApiClient(configuration) as api_client:
|
| 124 |
line_bot_api = MessagingApi(api_client)
|
|
@@ -133,6 +132,10 @@ def handle_text_message(event):
|
|
| 133 |
],
|
| 134 |
)
|
| 135 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
except Exception as e:
|
| 137 |
app.logger.error(f"Gemini API error: {e}")
|
| 138 |
with ApiClient(configuration) as api_client:
|
|
@@ -157,6 +160,7 @@ def handle_text_message(event):
|
|
| 157 |
)
|
| 158 |
)
|
| 159 |
|
|
|
|
| 160 |
# === 處理圖片訊息 ===
|
| 161 |
@handler.add(MessageEvent, message=ImageMessageContent)
|
| 162 |
def handle_image_message(event):
|
|
@@ -221,4 +225,4 @@ def handle_image_message(event):
|
|
| 221 |
TextMessage(text=response.output_text),
|
| 222 |
],
|
| 223 |
)
|
| 224 |
-
)
|
|
|
|
| 3 |
import logging
|
| 4 |
import os
|
| 5 |
import tempfile
|
|
|
|
| 6 |
import uuid
|
| 7 |
+
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
import markdown
|
| 10 |
from bs4 import BeautifulSoup
|
| 11 |
from flask import Flask, abort, request, send_from_directory
|
| 12 |
+
from google import genai
|
| 13 |
+
from google.genai import types
|
| 14 |
from linebot.v3 import WebhookHandler
|
| 15 |
from linebot.v3.exceptions import InvalidSignatureError
|
| 16 |
from linebot.v3.messaging import (
|
|
|
|
| 24 |
)
|
| 25 |
from linebot.v3.webhooks import ImageMessageContent, MessageEvent, TextMessageContent
|
| 26 |
from openai import OpenAI
|
| 27 |
+
from PIL import Image
|
| 28 |
|
| 29 |
# === 初始化 Google Gemini ===
|
| 30 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
|
|
| 57 |
# === AI Query 包裝 ===
|
| 58 |
def query(payload):
|
| 59 |
response = google_client.models.generate_content(
|
| 60 |
+
model="gemini-2.0-flash",
|
| 61 |
+
contents=f"{text_system_prompt}:{payload}",
|
| 62 |
)
|
| 63 |
return response.text
|
| 64 |
|
|
|
|
| 102 |
model="gemini-2.0-flash-exp-image-generation",
|
| 103 |
contents=prompt,
|
| 104 |
config=types.GenerateContentConfig(
|
| 105 |
+
response_modalities=["TEXT", "IMAGE"]
|
| 106 |
+
),
|
| 107 |
)
|
| 108 |
|
| 109 |
# 處理回應中的圖片
|
|
|
|
| 117 |
# 建立圖片的公開 URL
|
| 118 |
image_url = f"https://{base_url}/images/{filename}"
|
| 119 |
app.logger.info(f"Image URL: {image_url}")
|
| 120 |
+
|
| 121 |
# 回傳圖片給 LINE 使用者
|
| 122 |
with ApiClient(configuration) as api_client:
|
| 123 |
line_bot_api = MessagingApi(api_client)
|
|
|
|
| 132 |
],
|
| 133 |
)
|
| 134 |
)
|
| 135 |
+
# 傳送圖片後刪除暫存檔案
|
| 136 |
+
if os.path.exists(image_path):
|
| 137 |
+
os.remove(image_path)
|
| 138 |
+
|
| 139 |
except Exception as e:
|
| 140 |
app.logger.error(f"Gemini API error: {e}")
|
| 141 |
with ApiClient(configuration) as api_client:
|
|
|
|
| 160 |
)
|
| 161 |
)
|
| 162 |
|
| 163 |
+
|
| 164 |
# === 處理圖片訊息 ===
|
| 165 |
@handler.add(MessageEvent, message=ImageMessageContent)
|
| 166 |
def handle_image_message(event):
|
|
|
|
| 225 |
TextMessage(text=response.output_text),
|
| 226 |
],
|
| 227 |
)
|
| 228 |
+
)
|