Download app.py from AstroQuantumphycicist/FLUX-Schnell_MCP_Server: direct link, hf CLI and curl.
- Browser
- Download file 2.66 kB
-
https://huggingface.co/spaces/AstroQuantumphycicist/FLUX-Schnell_MCP_Server/resolve/main/app.py
- Command line
-
hf download hf://spaces/AstroQuantumphycicist/FLUX-Schnell_MCP_Server/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AstroQuantumphycicist/FLUX-Schnell_MCP_Server/resolve/main/app.py
2.66 kB
| import gradio as gr | |
| from gradio_client import Client | |
| from PIL import Image | |
| import io | |
| import base64 | |
| # 1. Gradio-Client für das Ziel-Space initialisieren | |
| flux_client = Client("black-forest-labs/FLUX.1-schnell") | |
| def proxy_generate( | |
| prompt: str, | |
| num_inference_steps: int = 4, | |
| guidance_scale: float = 7.5, | |
| height: int = 1024, | |
| width: int = 1024 | |
| ): | |
| """ | |
| Proxy-Funktion, die gradio_client verwendet: | |
| • prompt: Texteingabe für die Bildsynthese | |
| • num_inference_steps: Anzahl der Diffusionsschritte | |
| • guidance_scale: CFG-Skalierungsfaktor | |
| • height, width: Höhe/Breite des Ausgabebildes (Standard: 1024x1024) | |
| """ | |
| # Direkter Aufruf an /infer (das gradio_client-default Endpoint) | |
| result = flux_client.predict( | |
| prompt=prompt, | |
| seed=0, | |
| randomize_seed=True, | |
| width=width, | |
| height=height, | |
| num_inference_steps=num_inference_steps, | |
| api_name="/infer" | |
| ) | |
| # result enthält eine Base64-encodierte PNG-Grafik (als String) | |
| if not isinstance(result, str) or not result.startswith("data:image"): | |
| raise ValueError(f"Unerwartetes Response-Format von gradio_client: {result}") | |
| # Base64-String in Bytes dekodieren und als PIL-Image öffnen | |
| base64_str = result.split(",")[1] # "data:image/png;base64,<Base64-Teil>" | |
| image_bytes = base64.b64decode(base64_str) | |
| image = Image.open(io.BytesIO(image_bytes)) | |
| return image | |
| # 2. Gradio-Interface definieren | |
| with gr.Blocks() as demo: | |
| gr.Markdown( | |
| "## FLUX-Schnell Proxy\n" | |
| "Leitet Anfragen an das „FLUX.1-schnell“-Space via gradio_client weiter und zeigt das generierte Bild an.\n\n" | |
| "**Standardauflösung:** 1024 × 1024" | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| prompt = gr.Textbox(label="Prompt", placeholder="z.B. Eine futuristische Stadt in der Dämmerung …") | |
| steps = gr.Slider(minimum=1, maximum=50, value=4, step=1, label="Diffusionsschritte") | |
| scale = gr.Slider(minimum=1.0, maximum=20.0, value=7.5, step=0.5, label="Guidance Scale") | |
| height = gr.Number(value=1024, precision=0, label="Höhe (px)") | |
| width = gr.Number(value=1024, precision=0, label="Breite (px)") | |
| gen_btn = gr.Button("Bild generieren") | |
| with gr.Column(): | |
| output_img = gr.Image(label="Ergebnisbild") | |
| # 3. Event-Binding: Button klickt führt proxy_generate aus | |
| gen_btn.click( | |
| fn=proxy_generate, | |
| inputs=[prompt, steps, scale, height, width], | |
| outputs=output_img | |
| ) | |
| # 4. Gradio-Server als MCP-Server starten | |
| demo.launch(mcp_server=True) |