tsengiii commited on
Commit
d045353
·
verified ·
1 Parent(s): 9490f8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -37
app.py CHANGED
@@ -1,48 +1,19 @@
1
- import gradio as gr
2
  import requests
3
  import os
4
 
5
- # 設置 Hugging Face API URL 和 Token
6
- API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
7
- HF_API_TOKEN = os.getenv("HF_API_TOKEN") # 確認您已將此變數在 Hugging Face Spaces 的 Secret 中設定
8
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
9
 
10
  def query(payload):
11
- # 呼叫 API 並處理回應
12
  response = requests.post(API_URL, headers=headers, json=payload)
13
-
14
  if response.status_code != 200:
15
- # 打印錯誤以進行調試
16
- print(f"Request failed with status code {response.status_code}")
17
- print(f"Error details: {response.text}")
18
  return f"Error: {response.status_code}, {response.text}"
19
-
20
- return response.content
21
 
22
- def generate_landscape_art(description):
23
- # 呼叫模型,生成圖像
24
- output = query({"inputs": description})
25
-
26
- # 如果回應的內容是錯誤信息,顯示錯誤
27
- if isinstance(output, str) and output.startswith("Error:"):
28
- return output # 直接返回錯誤訊息供顯示
29
-
30
- # 保存生成的圖像到文件
31
- with open("generated_image.png", "wb") as f:
32
- f.write(output)
33
-
34
- # 返回生成的圖像文件
35
- return "generated_image.png"
36
-
37
- # 創建 Gradio 介面
38
- interface = gr.Interface(
39
- fn=generate_landscape_art,
40
- inputs=gr.Textbox(lines=2, placeholder="輸入描述來生成山水畫..."),
41
- outputs="image", # 輸出圖像
42
- title="山水畫生成器",
43
- description="根據您的描述生成山水畫,請輸入如 '古代中國風格的山水畫' 的描述"
44
- )
45
-
46
- # 啟動 Gradio 應用
47
- interface.launch()
48
 
 
 
 
 
1
  import requests
2
  import os
3
 
4
+ # 設置測試模型的 API URL
5
+ API_URL = "https://api-inference.huggingface.co/models/gpt2"
6
+ HF_API_TOKEN = os.getenv("HF_API_TOKEN") # 從環境變數讀取 API token
7
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
8
 
9
  def query(payload):
 
10
  response = requests.post(API_URL, headers=headers, json=payload)
 
11
  if response.status_code != 200:
 
 
 
12
  return f"Error: {response.status_code}, {response.text}"
13
+ return response.json()
 
14
 
15
+ # 測試簡單的文本生成
16
+ output = query({"inputs": "Hello, world!"})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # 打印結果
19
+ print(output)