Spaces:
Runtime error
Runtime error
生圖換成google
Browse files- requirements.txt +2 -1
- test.py +37 -23
requirements.txt
CHANGED
|
@@ -5,4 +5,5 @@ markdown
|
|
| 5 |
beautifulsoup4
|
| 6 |
openai
|
| 7 |
# google-generativeai
|
| 8 |
-
google-genai
|
|
|
|
|
|
| 5 |
beautifulsoup4
|
| 6 |
openai
|
| 7 |
# google-generativeai
|
| 8 |
+
google-genai
|
| 9 |
+
Pillow
|
test.py
CHANGED
|
@@ -3,8 +3,12 @@ import base64
|
|
| 3 |
import logging
|
| 4 |
import os
|
| 5 |
import tempfile
|
|
|
|
| 6 |
|
| 7 |
from google import genai
|
|
|
|
|
|
|
|
|
|
| 8 |
import markdown
|
| 9 |
from bs4 import BeautifulSoup
|
| 10 |
from flask import Flask, abort, request, send_from_directory
|
|
@@ -93,36 +97,47 @@ def handle_text_message(event):
|
|
| 93 |
if user_input.startswith("AI "):
|
| 94 |
prompt = user_input[3:].strip()
|
| 95 |
try:
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 102 |
)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
)
|
| 115 |
-
|
| 116 |
-
)
|
| 117 |
-
)
|
| 118 |
except Exception as e:
|
| 119 |
-
app.logger.error(f"
|
| 120 |
with ApiClient(configuration) as api_client:
|
| 121 |
line_bot_api = MessagingApi(api_client)
|
| 122 |
line_bot_api.reply_message(
|
| 123 |
ReplyMessageRequest(
|
| 124 |
reply_token=event.reply_token,
|
| 125 |
-
messages=[TextMessage(text="
|
| 126 |
)
|
| 127 |
)
|
| 128 |
else:
|
|
@@ -139,7 +154,6 @@ def handle_text_message(event):
|
|
| 139 |
)
|
| 140 |
)
|
| 141 |
|
| 142 |
-
|
| 143 |
# === 處理圖片訊息 ===
|
| 144 |
@handler.add(MessageEvent, message=ImageMessageContent)
|
| 145 |
def handle_image_message(event):
|
|
|
|
| 3 |
import logging
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
+
from io import BytesIO
|
| 7 |
|
| 8 |
from google import genai
|
| 9 |
+
from google.genai import types
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
import markdown
|
| 13 |
from bs4 import BeautifulSoup
|
| 14 |
from flask import Flask, abort, request, send_from_directory
|
|
|
|
| 97 |
if user_input.startswith("AI "):
|
| 98 |
prompt = user_input[3:].strip()
|
| 99 |
try:
|
| 100 |
+
# 使用 Gemini 生成圖片
|
| 101 |
+
response = google_client.models.generate_content(
|
| 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 |
+
# 處理回應中的圖片
|
| 110 |
+
for part in response.candidates[0].content.parts:
|
| 111 |
+
if part.inline_data is not None:
|
| 112 |
+
image = Image.open(BytesIO(part.inline_data.data))
|
| 113 |
+
filename = "output.png"
|
| 114 |
+
image_path = os.path.join(static_tmp_path, filename)
|
| 115 |
+
image.save(image_path)
|
| 116 |
+
|
| 117 |
+
# 建立圖片的公開 URL
|
| 118 |
+
image_url = f"https://{base_url}/images/{filename}"
|
| 119 |
+
|
| 120 |
+
with ApiClient(configuration) as api_client:
|
| 121 |
+
line_bot_api = MessagingApi(api_client)
|
| 122 |
+
line_bot_api.reply_message(
|
| 123 |
+
ReplyMessageRequest(
|
| 124 |
+
reply_token=event.reply_token,
|
| 125 |
+
messages=[
|
| 126 |
+
ImageMessage(
|
| 127 |
+
original_content_url=image_url,
|
| 128 |
+
preview_image_url=image_url,
|
| 129 |
+
)
|
| 130 |
+
],
|
| 131 |
)
|
| 132 |
+
)
|
|
|
|
|
|
|
| 133 |
except Exception as e:
|
| 134 |
+
app.logger.error(f"Gemini API error: {e}")
|
| 135 |
with ApiClient(configuration) as api_client:
|
| 136 |
line_bot_api = MessagingApi(api_client)
|
| 137 |
line_bot_api.reply_message(
|
| 138 |
ReplyMessageRequest(
|
| 139 |
reply_token=event.reply_token,
|
| 140 |
+
messages=[TextMessage(text="抱歉,生成圖片時發生錯誤。")],
|
| 141 |
)
|
| 142 |
)
|
| 143 |
else:
|
|
|
|
| 154 |
)
|
| 155 |
)
|
| 156 |
|
|
|
|
| 157 |
# === 處理圖片訊息 ===
|
| 158 |
@handler.add(MessageEvent, message=ImageMessageContent)
|
| 159 |
def handle_image_message(event):
|