Update main.py
Browse files
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"
|
| 194 |
image_url = upload_image_to_imgur(client, image_binary, album)
|
|
|
|
| 195 |
if image_url:
|
| 196 |
-
# δ½Ώη¨ push
|
| 197 |
line_bot_api.push_message(
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
| 200 |
)
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 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":
|