Update main.py
Browse files
main.py
CHANGED
@@ -5,7 +5,6 @@ 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
|
@@ -15,7 +14,8 @@ import tempfile
|
|
15 |
import httpx
|
16 |
|
17 |
from imgurpython import ImgurClient
|
18 |
-
|
|
|
19 |
|
20 |
# 設定 Google AI API 金鑰
|
21 |
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
@@ -113,6 +113,17 @@ def generate_image_with_gemini(prompt):
|
|
113 |
|
114 |
return None
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
#==========================
|
117 |
# 使用者上傳圖片
|
118 |
#==========================
|
@@ -185,7 +196,7 @@ def handle_image_message(event):
|
|
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 =
|
189 |
|
190 |
if image_binary:
|
191 |
album = "nvsYwgq"
|
|
|
5 |
from fastapi import FastAPI, Request, Header, BackgroundTasks, HTTPException, status
|
6 |
import google.generativeai as genai
|
7 |
import base64
|
|
|
8 |
from collections import defaultdict
|
9 |
from linebot import LineBotApi, WebhookHandler
|
10 |
from linebot.exceptions import InvalidSignatureError
|
|
|
14 |
import httpx
|
15 |
|
16 |
from imgurpython import ImgurClient
|
17 |
+
import requests
|
18 |
+
import tempfile
|
19 |
|
20 |
# 設定 Google AI API 金鑰
|
21 |
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
|
|
113 |
|
114 |
return None
|
115 |
|
116 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
|
117 |
+
headers = {"Authorization": f"Bearer {os.environ['HF_TOKEN']}"}
|
118 |
+
|
119 |
+
def generate_image_hf(prompt):
|
120 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
|
121 |
+
if response.status_code == 200:
|
122 |
+
return response.content # 圖片binary資料
|
123 |
+
else:
|
124 |
+
print("圖片生成失敗:", response.text)
|
125 |
+
return None
|
126 |
+
|
127 |
#==========================
|
128 |
# 使用者上傳圖片
|
129 |
#==========================
|
|
|
196 |
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="圖片生成中~ 請稍候.....✨"))
|
197 |
|
198 |
client = ImgurClient(client_id, client_secret, access_token, refresh_token)
|
199 |
+
image_binary = generate_image_hf(prompt)
|
200 |
|
201 |
if image_binary:
|
202 |
album = "nvsYwgq"
|