Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
3 |
|
4 |
-
#
|
5 |
API_URL = "https://api-inference.huggingface.co/models/KappaNeuro/ukiyo-e-art"
|
6 |
-
|
|
|
7 |
|
8 |
def query(payload):
|
9 |
response = requests.post(API_URL, headers=headers, json=payload)
|
@@ -12,24 +14,24 @@ def query(payload):
|
|
12 |
return response.json()
|
13 |
|
14 |
def inference(text):
|
15 |
-
#
|
16 |
output = query({"inputs": text})
|
17 |
|
18 |
-
#
|
19 |
if isinstance(output, dict) and "error" in output:
|
20 |
return f"Error: {output['error']}"
|
21 |
|
22 |
-
#
|
23 |
-
return output #
|
24 |
|
25 |
-
#
|
26 |
interface = gr.Interface(
|
27 |
fn=inference,
|
28 |
-
inputs=gr.Textbox(lines=2, placeholder="
|
29 |
-
outputs="text", #
|
30 |
title="Ukiyo-e Art 模型",
|
31 |
-
description="
|
32 |
)
|
33 |
|
34 |
-
#
|
35 |
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
import os # 用於讀取環境變數
|
4 |
|
5 |
+
# 從環境變數讀取 Hugging Face API token
|
6 |
API_URL = "https://api-inference.huggingface.co/models/KappaNeuro/ukiyo-e-art"
|
7 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN") # 從環境變數讀取 API token
|
8 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
9 |
|
10 |
def query(payload):
|
11 |
response = requests.post(API_URL, headers=headers, json=payload)
|
|
|
14 |
return response.json()
|
15 |
|
16 |
def inference(text):
|
17 |
+
# 呼叫模型並獲取結果
|
18 |
output = query({"inputs": text})
|
19 |
|
20 |
+
# 如果返回結果包含錯誤信息,顯示錯誤
|
21 |
if isinstance(output, dict) and "error" in output:
|
22 |
return f"Error: {output['error']}"
|
23 |
|
24 |
+
# 返回模型的輸出結果
|
25 |
+
return output # 根據實際返回的格式處理
|
26 |
|
27 |
+
# 創建 Gradio 介面
|
28 |
interface = gr.Interface(
|
29 |
fn=inference,
|
30 |
+
inputs=gr.Textbox(lines=2, placeholder="輸入生成藝術的描述..."),
|
31 |
+
outputs="text", # 如果模型輸出的是圖片,可以改為 `outputs="image"`
|
32 |
title="Ukiyo-e Art 模型",
|
33 |
+
description="這是一個呼叫 Hugging Face 上 Ukiyo-e Art 模型的應用"
|
34 |
)
|
35 |
|
36 |
+
# 啟動 Gradio 應用
|
37 |
interface.launch()
|