Spaces:
Sleeping
Sleeping
Peiiiiiiiiru
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,46 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
|
|
3 |
|
4 |
-
# Hugging Face API
|
5 |
API_URL = "https://api-inference.huggingface.co/models/KappaNeuro/ukiyo-e-art"
|
6 |
headers = {"Authorization": "Bearer hf_MySpaceToken"}
|
7 |
|
8 |
-
#
|
9 |
def query(payload):
|
|
|
10 |
response = requests.post(API_URL, headers=headers, json=payload)
|
11 |
-
if response.status_code == 200:
|
12 |
-
return response.content # 圖片回應為二進位格式
|
13 |
-
else:
|
14 |
-
return f"Error {response.status_code}: {response.text}"
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def generate_image(prompt):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
# 建立 Gradio
|
25 |
interface = gr.Interface(
|
26 |
fn=generate_image,
|
27 |
inputs="text",
|
28 |
outputs="image",
|
29 |
title="浮世繪生成器",
|
30 |
-
description="
|
31 |
)
|
32 |
|
33 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
|
6 |
+
# Hugging Face API 資訊
|
7 |
API_URL = "https://api-inference.huggingface.co/models/KappaNeuro/ukiyo-e-art"
|
8 |
headers = {"Authorization": "Bearer hf_MySpaceToken"}
|
9 |
|
10 |
+
# 呼叫模型的函數,並增加日誌以排查錯誤
|
11 |
def query(payload):
|
12 |
+
print("呼叫模型中...")
|
13 |
response = requests.post(API_URL, headers=headers, json=payload)
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
if response.status_code != 200:
|
16 |
+
print(f"Error {response.status_code}: {response.text}")
|
17 |
+
return None # 回傳 None 表示呼叫失敗
|
18 |
+
|
19 |
+
print("模型呼叫成功,正在解析回應...")
|
20 |
+
return response.content
|
21 |
+
|
22 |
+
# 定義 Gradio 的生成函數
|
23 |
def generate_image(prompt):
|
24 |
+
result = query({"inputs": prompt})
|
25 |
+
|
26 |
+
if result is None:
|
27 |
+
return "模型回應錯誤,請檢查 API Token 或模型狀態。"
|
28 |
+
|
29 |
+
try:
|
30 |
+
image = Image.open(BytesIO(result)) # 將回傳的二進位資料轉為圖片
|
31 |
+
return image
|
32 |
+
except Exception as e:
|
33 |
+
print(f"解析圖片失敗:{e}")
|
34 |
+
return "無法生成圖片,請稍後再試。"
|
35 |
|
36 |
+
# 建立 Gradio 介面
|
37 |
interface = gr.Interface(
|
38 |
fn=generate_image,
|
39 |
inputs="text",
|
40 |
outputs="image",
|
41 |
title="浮世繪生成器",
|
42 |
+
description="輸入一句話,生成浮世繪風格的藝術圖片。",
|
43 |
)
|
44 |
|
45 |
+
# 啟動應用程式,設定 share=True 以方便測試
|
46 |
+
interface.launch(share=True)
|