Update main.py
Browse files
main.py
CHANGED
|
@@ -78,9 +78,9 @@ async def webhook(
|
|
| 78 |
#==========================
|
| 79 |
# 使用者請求生成圖片
|
| 80 |
#==========================
|
| 81 |
-
def upload_image_to_imgur(client, image_binary, album=None, name="
|
| 82 |
# 將 binary 資料轉為 PIL Image
|
| 83 |
-
image = Image.open(io.BytesIO(image_binary))
|
| 84 |
|
| 85 |
# 建立暫存檔案來上傳 (因為 ImgurClient 需要檔案路徑)
|
| 86 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as tmp:
|
|
@@ -91,7 +91,7 @@ def upload_image_to_imgur(client, image_binary, album=None, name="gemini-image",
|
|
| 91 |
'album': album,
|
| 92 |
'name': name,
|
| 93 |
'title': title,
|
| 94 |
-
'description': f'Generated by
|
| 95 |
}
|
| 96 |
|
| 97 |
# 使用 client 進行圖片上傳
|
|
@@ -113,10 +113,10 @@ def generate_image_with_gemini(prompt):
|
|
| 113 |
|
| 114 |
return None
|
| 115 |
|
| 116 |
-
|
| 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資料
|
|
|
|
| 78 |
#==========================
|
| 79 |
# 使用者請求生成圖片
|
| 80 |
#==========================
|
| 81 |
+
def upload_image_to_imgur(client, image_binary, album=None, name="Hugging Face-image", title="Hugging Face Generated Image"):
|
| 82 |
# 將 binary 資料轉為 PIL Image
|
| 83 |
+
image = PIL.Image.open(io.BytesIO(image_binary))
|
| 84 |
|
| 85 |
# 建立暫存檔案來上傳 (因為 ImgurClient 需要檔案路徑)
|
| 86 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as tmp:
|
|
|
|
| 91 |
'album': album,
|
| 92 |
'name': name,
|
| 93 |
'title': title,
|
| 94 |
+
'description': f'Generated by Hugging Face - {datetime.now()}'
|
| 95 |
}
|
| 96 |
|
| 97 |
# 使用 client 進行圖片上傳
|
|
|
|
| 113 |
|
| 114 |
return None
|
| 115 |
|
| 116 |
+
# 使用 Hugging Face 生成圖片
|
|
|
|
|
|
|
| 117 |
def generate_image_hf(prompt):
|
| 118 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
|
| 119 |
+
headers = {"Authorization": f"Bearer {os.environ['HF_TOKEN']}"}
|
| 120 |
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
|
| 121 |
if response.status_code == 200:
|
| 122 |
return response.content # 圖片binary資料
|