Download app1.py from digiplay/2026: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/digiplay/2026/resolve/main/app1.py
- Command line
-
hf download hf://spaces/digiplay/2026/app1.py
-
curl -L -o app1.py https://huggingface.co/spaces/digiplay/2026/resolve/main/app1.py
1.53 kB
| import os | |
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| # 1. 從環境變數讀取 Token (這是 2026 年最安全的標準作法) | |
| # 在 Space 設定頁面的 "Secrets" 新增一個變數叫 HF_TOKEN | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| # 初始化客戶端,它會自動對接最新的 router.huggingface.co | |
| client = InferenceClient(api_key=HF_TOKEN) | |
| def generate_image(prompt): | |
| if not prompt: | |
| return None | |
| try: | |
| # 使用目前最強的開源生圖模型 FLUX.1-schnell | |
| # 路由會自動導向新的 Inference Providers 端點 | |
| image = client.text_to_image( | |
| prompt, | |
| model="digiplay/2K" | |
| ) | |
| return image | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| return None | |
| # 建立簡潔的介面 | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 🚀 2026 AI 快速生圖工具") | |
| gr.Markdown("這是一個使用最新 `router.huggingface.co` 端點的範例。") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_text = gr.Textbox( | |
| label="描述你想看到的畫面", | |
| placeholder="例如:台北街頭的賽博龐克風格, 霓虹燈, 雨夜...", | |
| lines=3 | |
| ) | |
| run_btn = gr.Button("立即生成 ✨", variant="primary") | |
| with gr.Column(): | |
| output_img = gr.Image(label="生成結果", type="pil") | |
| run_btn.click(fn=generate_image, inputs=input_text, outputs=output_img) | |
| demo.launch() | |