Spaces:
Sleeping
Sleeping
File size: 1,573 Bytes
a5b048f a132a03 8cd6ba0 772013d a132a03 8cd6ba0 a132a03 60f7d2b a5b048f 772013d e796722 8cd6ba0 772013d 8cd6ba0 772013d 8cd6ba0 d00b797 8cd6ba0 772013d 8cd6ba0 772013d 8cd6ba0 772013d a5b048f 8cd6ba0 e796722 a5b048f e796722 8cd6ba0 a5b048f 772013d 8cd6ba0 772013d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import gradio as gr
import requests
from PIL import Image
from io import BytesIO
import json
# Hugging Face API 資訊
API_URL = "https://api-inference.huggingface.co/models/KappaNeuro/ukiyo-e-art"
headers = {"Authorization": "Bearer hf_MySpaceToken"}
# 呼叫模型的函數,並處理回應格式
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
if response.status_code != 200:
return f"Error {response.status_code}: {response.text}" # 顯示 API 回傳的錯誤訊息
# 檢查回應格式,如果是 JSON 說明模型未回傳圖片
if response.headers["Content-Type"] == "application/json":
response_data = response.json()
return f"API 回應錯誤: {response_data.get('error', '未知錯誤')}"
return response.content # 如果回傳的是圖片,回傳二進位資料
# 定義 Gradio 的生成函數
def generate_image(prompt):
result = query({"inputs": prompt})
# 如果回應內容是字串(錯誤訊息),直接回傳
if isinstance(result, str):
return result
try:
image = Image.open(BytesIO(result)) # 將二進位資料轉為圖片
return image
except Exception as e:
return f"圖片解析失敗: {e}"
# 建立 Gradio 介面
interface = gr.Interface(
fn=generate_image,
inputs="text",
outputs="image",
title="浮世繪生成器",
description="輸入一句話,生成浮世繪風格的藝術圖片。",
)
# 啟動應用程式,設定 share=True 以便測試
interface.launch(share=True)
|