Evaaaaa06 commited on
Commit
fac1691
Β·
verified Β·
1 Parent(s): f26ebc6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -16
main.py CHANGED
@@ -1,10 +1,11 @@
1
  import json, os
 
2
  import gradio as gr
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
5
  import google.generativeai as genai
6
- import mimetypes
7
  import base64
 
8
  from collections import defaultdict
9
  from linebot import LineBotApi, WebhookHandler
10
  from linebot.exceptions import InvalidSignatureError
@@ -77,11 +78,6 @@ async def webhook(
77
  #==========================
78
  # δ½Ώη”¨θ€…θ«‹ζ±‚η”Ÿζˆεœ–η‰‡
79
  #==========================
80
- import io
81
- from PIL import Image
82
- import tempfile
83
- from datetime import datetime
84
-
85
  def upload_image_to_imgur(client, image_binary, album=None, name="gemini-image", title="Gemini Generated Image"):
86
  # ε°‡ binary 資料轉為 PIL Image
87
  image = Image.open(io.BytesIO(image_binary))
@@ -185,24 +181,36 @@ def handle_image_message(event):
185
  if user_text and user_text.startswith("θ«‹εΉ«ζˆ‘η”Ÿζˆεœ–η‰‡"):
186
  prompt = user_text.replace("θ«‹εΉ«ζˆ‘η”Ÿζˆεœ–η‰‡", "").strip()
187
 
 
 
 
188
  client = ImgurClient(client_id, client_secret, access_token, refresh_token)
189
  image_binary = generate_image_with_gemini(prompt)
190
- line_bot_api.reply_message(event.reply_token, TextSendMessage(text="εœ–η‰‡η”ŸζˆδΈ­ο½ž 請稍候約10-20η§’βœ¨"))
191
 
192
  if image_binary:
193
- album = "nvsYwgq" # δ½ ηš„η›Έη°ΏID
194
  image_url = upload_image_to_imgur(client, image_binary, album)
 
195
  if image_url:
196
- # 使用 push θ¨Šζ―οΌŒιΏε…ε›žθ¦†ιŽζœŸ
197
  line_bot_api.push_message(
198
- event.source.user_id,
199
- [TextSendMessage(text="✨ ι€™ζ˜―ζˆ‘η‚Ίδ½ η”Ÿζˆηš„εœ–η‰‡ε–”ο½ž"),ImageSendMessage(original_content_url=image_url,preview_image_url=image_url)]
 
 
 
200
  )
201
- else:
202
- line_bot_api.push_message(
203
- event.source.user_id,
204
- TextSendMessage(text="⚠️ εœ–η‰‡δΈŠε‚³ε€±ζ•—οΌŒθ«‹η¨εΎŒε†θ©¦~")
205
- )
 
 
 
 
 
 
206
 
207
 
208
  if event.message.type == "image":
 
1
  import json, os
2
+ import io
3
  import gradio as gr
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
6
  import google.generativeai as genai
 
7
  import base64
8
+ from datetime import datetime
9
  from collections import defaultdict
10
  from linebot import LineBotApi, WebhookHandler
11
  from linebot.exceptions import InvalidSignatureError
 
78
  #==========================
79
  # δ½Ώη”¨θ€…θ«‹ζ±‚η”Ÿζˆεœ–η‰‡
80
  #==========================
 
 
 
 
 
81
  def upload_image_to_imgur(client, image_binary, album=None, name="gemini-image", title="Gemini Generated Image"):
82
  # ε°‡ binary 資料轉為 PIL Image
83
  image = Image.open(io.BytesIO(image_binary))
 
181
  if user_text and user_text.startswith("θ«‹εΉ«ζˆ‘η”Ÿζˆεœ–η‰‡"):
182
  prompt = user_text.replace("θ«‹εΉ«ζˆ‘η”Ÿζˆεœ–η‰‡", "").strip()
183
 
184
+ # ε…ˆη«‹ε³ε›žθ¦†ιΏε…token過期
185
+ line_bot_api.reply_message(event.reply_token, TextSendMessage(text="εœ–η‰‡η”ŸζˆδΈ­ο½ž 請稍候.....✨"))
186
+
187
  client = ImgurClient(client_id, client_secret, access_token, refresh_token)
188
  image_binary = generate_image_with_gemini(prompt)
 
189
 
190
  if image_binary:
191
+ album = "nvsYwgq"
192
  image_url = upload_image_to_imgur(client, image_binary, album)
193
+
194
  if image_url:
195
+ # 使用 push message η™Όι€εœ–η‰‡οΌŒιΏε… reply token θΆ…ζ™‚
196
  line_bot_api.push_message(
197
+ event.source.user_id,
198
+ [
199
+ TextSendMessage(text="✨ ι€™ζ˜―ζˆ‘η‚Ίδ½ η”Ÿζˆηš„εœ–η‰‡ε–”ο½ž"),
200
+ ImageSendMessage(original_content_url=image_url, preview_image_url=image_url)
201
+ ]
202
  )
203
+ else:
204
+ line_bot_api.push_message(
205
+ event.source.user_id,
206
+ TextSendMessage(text="⚠️ εœ–η‰‡δΈŠε‚³ε€±ζ•—οΌŒθ«‹η¨εΎŒε†θ©¦~")
207
+ )
208
+ else:
209
+ line_bot_api.push_message(
210
+ event.source.user_id,
211
+ TextSendMessage(text="⚠️ εœ–η‰‡η”Ÿζˆε€±ζ•—οΌŒθ«‹η¨εΎŒε†θ©¦~")
212
+ )
213
+ return
214
 
215
 
216
  if event.message.type == "image":