Update main.py
Browse files
main.py
CHANGED
@@ -12,6 +12,9 @@ import PIL.Image
|
|
12 |
import tempfile
|
13 |
import httpx
|
14 |
|
|
|
|
|
|
|
15 |
# 設定 Google AI API 金鑰
|
16 |
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
17 |
|
@@ -25,6 +28,12 @@ model = genai.GenerativeModel('gemini-2.0-flash-exp', system_instruction="主要
|
|
25 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
26 |
line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
# 設定是否正在與使用者交談
|
29 |
working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true"
|
30 |
|
@@ -67,6 +76,30 @@ async def webhook(
|
|
67 |
#==========================
|
68 |
# 使用者請求生成圖片
|
69 |
#==========================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def upload_image_to_imgur_with_token(image_binary, access_token):
|
71 |
"""
|
72 |
使用 Imgur OAuth2 Access Token 上傳圖片(直接接收 binary,不需存檔)
|
@@ -158,25 +191,15 @@ def handle_image_message(event):
|
|
158 |
if user_text and user_text.startswith("請幫我生成圖片"):
|
159 |
prompt = user_text.replace("請幫我生成圖片", "").strip()
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
if response.parts and hasattr(response.parts[0], "inline_data"):
|
165 |
-
image_data = response.parts[0].inline_data.data
|
166 |
-
access_token = os.environ.get("IMGUR_ACCESS_TOKEN")
|
167 |
-
image_url = upload_image_to_imgur_with_token(image_data,access_token)
|
168 |
-
print("Imgur 回傳 URL:", image_url)
|
169 |
|
170 |
-
if
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
TextSendMessage(text="這是我為你生成的圖片喔~ ✨"),
|
175 |
-
ImageSendMessage(original_content_url=image_url, preview_image_url=image_url)
|
176 |
-
]
|
177 |
-
)
|
178 |
else:
|
179 |
-
|
180 |
return
|
181 |
|
182 |
if event.message.type == "image":
|
|
|
12 |
import tempfile
|
13 |
import httpx
|
14 |
|
15 |
+
from imgurpython import ImgurClient
|
16 |
+
|
17 |
+
|
18 |
# 設定 Google AI API 金鑰
|
19 |
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
20 |
|
|
|
28 |
line_bot_api = LineBotApi(os.environ["CHANNEL_ACCESS_TOKEN"])
|
29 |
line_handler = WebhookHandler(os.environ["CHANNEL_SECRET"])
|
30 |
|
31 |
+
# 設定 Imgur 的 API
|
32 |
+
client_id = os.environ.get("IMGUR_CLIENT_ID")
|
33 |
+
client_secret = os.environ.get("IMGUR_CLIENT_SECRET")
|
34 |
+
access_token = os.environ.get("IMGUR_ACCESS_TOKEN")
|
35 |
+
refresh_token = os.environ.get("IMGUR_REFRESH_TOKEN")
|
36 |
+
|
37 |
# 設定是否正在與使用者交談
|
38 |
working_status = os.getenv("DEFALUT_TALKING", default = "true").lower() == "true"
|
39 |
|
|
|
76 |
#==========================
|
77 |
# 使用者請求生成圖片
|
78 |
#==========================
|
79 |
+
def upload_image_to_imgur(client, image_binary, album=None, name="gemini-image", title="Gemini Generated Image"):
|
80 |
+
config = {
|
81 |
+
'album': album,
|
82 |
+
'name': name,
|
83 |
+
'title': title,
|
84 |
+
'description': f'Generated by Gemini - {datetime.now()}'
|
85 |
+
}
|
86 |
+
|
87 |
+
# 使用暫存檔案上傳,因為 imgurpython 需要檔案路徑
|
88 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as tmp:
|
89 |
+
tmp.write(image_binary)
|
90 |
+
tmp.flush()
|
91 |
+
image = client.upload_from_path(tmp.name, config=config, anon=False)
|
92 |
+
|
93 |
+
return image['link']
|
94 |
+
|
95 |
+
# 使用 Gemini 生成圖片
|
96 |
+
def generate_image_with_gemini(prompt):
|
97 |
+
image_model = genai.GenerativeModel("gemini-2.0-flash-exp-image-generation")
|
98 |
+
response = image_model.generate_content(prompt)
|
99 |
+
if response.parts and hasattr(response.parts[0], "inline_data"):
|
100 |
+
return response.parts[0].inline_data.data
|
101 |
+
return None
|
102 |
+
|
103 |
def upload_image_to_imgur_with_token(image_binary, access_token):
|
104 |
"""
|
105 |
使用 Imgur OAuth2 Access Token 上傳圖片(直接接收 binary,不需存檔)
|
|
|
191 |
if user_text and user_text.startswith("請幫我生成圖片"):
|
192 |
prompt = user_text.replace("請幫我生成圖片", "").strip()
|
193 |
|
194 |
+
client = ImgurClient(client_id, client_secret, access_token, refresh_token)
|
195 |
+
image_binary = generate_image_with_gemini(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
+
if image_binary:
|
198 |
+
album = "nvsYwgq" # 你的相簿ID
|
199 |
+
image_url = upload_image_to_imgur(client, image_binary, album)
|
200 |
+
print(f"圖片網址: {image_url}")
|
|
|
|
|
|
|
|
|
201 |
else:
|
202 |
+
print("Gemini 圖片生成失敗")
|
203 |
return
|
204 |
|
205 |
if event.message.type == "image":
|