Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,33 @@
|
|
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 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
return image
|
22 |
-
except Exception as e:
|
23 |
-
return None
|
24 |
|
25 |
-
# 建立 Gradio
|
26 |
-
|
27 |
fn=generate_image,
|
28 |
-
inputs=
|
29 |
-
outputs=
|
30 |
-
title="
|
31 |
-
description="
|
32 |
)
|
33 |
|
34 |
-
#
|
35 |
-
iface.launch()
|
|
|
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 |
+
# 定義 Gradio 介面功能
|
17 |
def generate_image(prompt):
|
18 |
+
image_data = query({"inputs": prompt})
|
19 |
+
if isinstance(image_data, bytes):
|
20 |
+
return image_data # 返回生成的圖片
|
21 |
+
else:
|
22 |
+
return "無法生成圖片,請檢查輸入或模型狀態。"
|
|
|
|
|
|
|
23 |
|
24 |
+
# 建立 Gradio App
|
25 |
+
interface = gr.Interface(
|
26 |
fn=generate_image,
|
27 |
+
inputs="text",
|
28 |
+
outputs="image",
|
29 |
+
title="浮世繪生成器",
|
30 |
+
description="輸入文字提示,生成日本浮世繪風格的藝術圖片。",
|
31 |
)
|
32 |
|
33 |
+
interface.launch(share=True) # 設定 share=True 方便測試
|
|