Peiiiiiiiiru commited on
Commit
8cd6ba0
·
verified ·
1 Parent(s): e796722

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -15
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
- # 定義 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 方便測試
 
 
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)