tsengiii commited on
Commit
998d23e
·
verified ·
1 Parent(s): 60edab6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -1,9 +1,11 @@
1
  import gradio as gr
2
  import requests
 
3
 
4
- # 使用你的 Hugging Face API token
5
  API_URL = "https://api-inference.huggingface.co/models/KappaNeuro/ukiyo-e-art"
6
- headers = {"Authorization": "Bearer hf_your_valid_token_here"} # 'hf_your_valid_token_here' 替换为实际的 API token
 
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
- # 创建 Gradio 界面
26
  interface = gr.Interface(
27
  fn=inference,
28
- inputs=gr.Textbox(lines=2, placeholder="输入生成艺术的描述..."),
29
- outputs="text", # 如果模型返回的是图片,可以改为 `outputs="image"`
30
  title="Ukiyo-e Art 模型",
31
- description="这是一个调用 Hugging Face 上 Ukiyo-e Art 模型的应用"
32
  )
33
 
34
- # 启动 Gradio 应用
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()