Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,8 +4,6 @@ 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_gen
|
| 7 |
-
from google import genai
|
| 8 |
-
from google.genai import types
|
| 9 |
|
| 10 |
import base64
|
| 11 |
from collections import defaultdict
|
|
@@ -13,14 +11,9 @@ from linebot import LineBotApi, WebhookHandler
|
|
| 13 |
from linebot.exceptions import InvalidSignatureError
|
| 14 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage, ImageMessage
|
| 15 |
import PIL.Image
|
| 16 |
-
import tempfile
|
| 17 |
import httpx
|
| 18 |
|
| 19 |
-
from
|
| 20 |
-
import requests
|
| 21 |
-
import tempfile
|
| 22 |
-
from datetime import datetime
|
| 23 |
-
|
| 24 |
|
| 25 |
# 設定 Google AI API 金鑰
|
| 26 |
genai_gen.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
|
@@ -80,44 +73,6 @@ async def webhook(
|
|
| 80 |
raise HTTPException(status_code=400, detail="Invalid signature")
|
| 81 |
return "ok"
|
| 82 |
|
| 83 |
-
#==========================
|
| 84 |
-
# 使用者請求生成圖片
|
| 85 |
-
#==========================
|
| 86 |
-
def upload_image_to_imgur(client, image_binary, album=None, name="gemini-image", title="gemini Generated Image"):
|
| 87 |
-
# 將 binary 資料轉為 PIL Image
|
| 88 |
-
image = PIL.Image.open(io.BytesIO(image_binary))
|
| 89 |
-
|
| 90 |
-
# 建立暫存檔案來上傳 (因為 ImgurClient 需要檔案路徑)
|
| 91 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as tmp:
|
| 92 |
-
image.save(tmp.name, format='PNG')
|
| 93 |
-
|
| 94 |
-
# 準備上傳資訊
|
| 95 |
-
config = {
|
| 96 |
-
'album': album,
|
| 97 |
-
'name': name,
|
| 98 |
-
'title': title,
|
| 99 |
-
'description': f'Generated by gemini - {datetime.now()}'
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
# 使用 client 進行圖片上傳
|
| 103 |
-
uploaded_image = client.upload_from_path(tmp.name, config=config, anon=False)
|
| 104 |
-
|
| 105 |
-
# 回傳圖片網址
|
| 106 |
-
return uploaded_image['link']
|
| 107 |
-
|
| 108 |
-
# 使用 Gemini 生成圖片
|
| 109 |
-
client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
|
| 110 |
-
def generate_image_with_gemini(prompt):
|
| 111 |
-
response = client.models.generate_content(model="gemini-2.0-flash-exp-image-generation",contents=prompt,config=types.GenerateContentConfig(response_modalities=['Text', 'Image']))
|
| 112 |
-
|
| 113 |
-
for part in response.candidates[0].content.parts:
|
| 114 |
-
if part.text is not None:
|
| 115 |
-
print(part.text)
|
| 116 |
-
elif part.inline_data is not None:
|
| 117 |
-
return part.inline_data.data
|
| 118 |
-
return
|
| 119 |
-
|
| 120 |
-
|
| 121 |
#==========================
|
| 122 |
# 使用者上傳圖片
|
| 123 |
#==========================
|
|
|
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
| 6 |
import google.generativeai as genai_gen
|
|
|
|
|
|
|
| 7 |
|
| 8 |
import base64
|
| 9 |
from collections import defaultdict
|
|
|
|
| 11 |
from linebot.exceptions import InvalidSignatureError
|
| 12 |
from linebot.models import MessageEvent, TextMessage, TextSendMessage, ImageSendMessage, AudioMessage, ImageMessage
|
| 13 |
import PIL.Image
|
|
|
|
| 14 |
import httpx
|
| 15 |
|
| 16 |
+
from Image_generation import generate_image_with_gemini, upload_image_to_imgur
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# 設定 Google AI API 金鑰
|
| 19 |
genai_gen.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
|
|
|
| 73 |
raise HTTPException(status_code=400, detail="Invalid signature")
|
| 74 |
return "ok"
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
#==========================
|
| 77 |
# 使用者上傳圖片
|
| 78 |
#==========================
|