Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,35 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
|
|
|
|
3 |
import os
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
8 |
-
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
if response.status_code != 200:
|
14 |
-
print(f"Request failed with status code {response.status_code}")
|
15 |
-
print(f"Error details: {response.text}")
|
16 |
-
return f"Error: {response.status_code}, {response.text}"
|
17 |
-
|
18 |
-
return response.content # 返回圖片的二進位數據
|
19 |
|
20 |
-
def
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
description = dinner_hints.get(hint, "delicious food")
|
31 |
-
|
32 |
-
# 調用 API 生成圖像
|
33 |
-
output = query({"inputs": description})
|
34 |
-
|
35 |
-
if isinstance(output, str) and output.startswith("Error:"):
|
36 |
-
return output # 返回錯誤訊息
|
37 |
-
|
38 |
-
# 保存生成的圖像到文件
|
39 |
-
with open("generated_dinner.png", "wb") as f:
|
40 |
-
f.write(output)
|
41 |
-
|
42 |
-
return "generated_dinner.png"
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
fn=
|
47 |
-
inputs=gr.Textbox(
|
48 |
-
outputs="
|
49 |
-
title="
|
50 |
-
description="
|
51 |
)
|
52 |
|
53 |
-
#
|
54 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
import io
|
4 |
+
from PIL import Image
|
5 |
+
from dotenv import load_dotenv
|
6 |
import os
|
7 |
|
8 |
+
# 載入環境變數
|
9 |
+
load_dotenv()
|
|
|
|
|
10 |
|
11 |
+
# Hugging Face API 設定
|
12 |
+
API_URL = "https://api-inference.huggingface.co/models/KappaNeuro/ukiyo-e-art"
|
13 |
+
headers = {"Authorization": "Bearer hf_MySpaceToken"}
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
def generate_image(prompt):
|
16 |
+
try:
|
17 |
+
# 呼叫 Hugging Face API
|
18 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
|
19 |
+
# 轉換回應為圖像
|
20 |
+
image = Image.open(io.BytesIO(response.content))
|
21 |
+
return image
|
22 |
+
except Exception as e:
|
23 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# 建立 Gradio 介面
|
26 |
+
iface = gr.Interface(
|
27 |
+
fn=generate_image,
|
28 |
+
inputs=gr.Textbox(label="請輸入圖像描述"),
|
29 |
+
outputs=gr.Image(label="生成的圖像"),
|
30 |
+
title="浮世繪風格圖像生成器",
|
31 |
+
description="輸入描述文字,生成浮世繪風格的圖像"
|
32 |
)
|
33 |
|
34 |
+
# 啟動應用
|
35 |
+
iface.launch()
|