Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import io | |
| from PIL import Image | |
| from dotenv import load_dotenv | |
| import os | |
| # 載入環境變數 | |
| load_dotenv() | |
| # Hugging Face API 設定 | |
| API_URL = "https://api-inference.huggingface.co/models/KappaNeuro/ukiyo-e-art" | |
| headers = {"Authorization": "Bearer hf_MySpaceToken"} | |
| def generate_image(prompt): | |
| try: | |
| # 呼叫 Hugging Face API | |
| response = requests.post(API_URL, headers=headers, json={"inputs": prompt}) | |
| # 轉換回應為圖像 | |
| image = Image.open(io.BytesIO(response.content)) | |
| return image | |
| except Exception as e: | |
| return None | |
| # 建立 Gradio 介面 | |
| iface = gr.Interface( | |
| fn=generate_image, | |
| inputs=gr.Textbox(label="請輸入圖像描述"), | |
| outputs=gr.Image(label="生成的圖像"), | |
| title="浮世繪風格圖像生成器", | |
| description="輸入描述文字,生成浮世繪風格的圖像" | |
| ) | |
| # 啟動應用 | |
| iface.launch() |