Spaces:
Paused
Paused
Download app.py from BTrain/Shnell: direct link, hf CLI and curl.
- Browser
- Download file 8.02 kB
-
https://huggingface.co/spaces/BTrain/Shnell/resolve/refs%2Fpr%2F1/app.py
- Command line
-
hf download hf://spaces/BTrain/Shnell@refs/pr/1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/BTrain/Shnell/resolve/refs%2Fpr%2F1/app.py
8.02 kB
| import gradio as gr | |
| import numpy as np | |
| import random | |
| import spaces | |
| import torch | |
| from diffusers import DiffusionPipeline | |
| dtype = torch.bfloat16 | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device) | |
| MAX_SEED = np.iinfo(np.int32).max | |
| WIDTH = 700 | |
| HEIGHT = 900 | |
| def infer(prompt, progress=gr.Progress(track_tqdm=True)): | |
| seed = random.randint(0, MAX_SEED) | |
| generator = torch.Generator().manual_seed(seed) | |
| image = pipe( | |
| prompt=prompt, | |
| width=WIDTH, | |
| height=HEIGHT, | |
| num_inference_steps=4, | |
| generator=generator, | |
| guidance_scale=0.0, | |
| ).images[0] | |
| out_path = f"/tmp/generated_{seed}.png" | |
| image.save(out_path) | |
| return image, gr.update(value=out_path, visible=True) | |
| css = """ | |
| .gradio-container { | |
| max-width: 700px !important; | |
| padding: 2px !important; | |
| padding-top: 0 !important; | |
| } | |
| #col-container { | |
| margin: 0 auto; | |
| margin-top: 0 !important; | |
| max-width: 700px; | |
| width: 100%; | |
| gap: 0 !important; | |
| } | |
| #sticky-controls { | |
| position: sticky; | |
| top: 0; | |
| z-index: 100; | |
| background: var(--body-background-fill); | |
| padding-top: 2px; | |
| padding-bottom: 2px; | |
| margin-bottom: 0 !important; | |
| flex-wrap: nowrap !important; | |
| flex-direction: row !important; | |
| gap: 8px !important; | |
| } | |
| #generate-btn, #save-btn { | |
| flex: 1 1 30% !important; | |
| min-width: 0 !important; | |
| } | |
| #generate-btn, #save-btn { | |
| font-size: 15px !important; | |
| } | |
| #combo-btn, #autoloop-btn { | |
| flex: 0 0 52px !important; | |
| min-width: 52px !important; | |
| max-width: 52px !important; | |
| padding: 0 !important; | |
| font-size: 18px !important; | |
| } | |
| #autoloop-btn { | |
| background-color: #e74c3c !important; | |
| border-color: #e74c3c !important; | |
| color: white !important; | |
| } | |
| #sticky-controls button { | |
| width: 100% !important; | |
| height: 44px !important; | |
| min-height: 44px !important; | |
| transition: filter 0.05s ease !important; | |
| } | |
| #sticky-controls button:active { | |
| filter: brightness(0.65) !important; | |
| } | |
| #result-hidden { | |
| display: none !important; | |
| } | |
| #display-wrap { | |
| width: 100vw; | |
| max-width: 100vw; | |
| margin-left: calc(50% - 50vw); | |
| margin-right: calc(50% - 50vw); | |
| aspect-ratio: 700 / 900; | |
| background: #111; | |
| overflow: hidden; | |
| } | |
| #display-img { | |
| width: 100%; | |
| height: 100%; | |
| object-fit: cover; | |
| display: block; | |
| } | |
| @media (min-width: 700px) { | |
| #display-wrap { | |
| width: 700px; | |
| max-width: 700px; | |
| margin-left: auto; | |
| margin-right: auto; | |
| } | |
| } | |
| #interval-row { | |
| margin-top: 16px; | |
| padding: 0 8px; | |
| } | |
| #interval-panel { | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| gap: 16px; | |
| width: 100%; | |
| padding: 10px 0; | |
| } | |
| #interval-minus, #interval-plus { | |
| flex: 0 0 56px !important; | |
| max-width: 56px !important; | |
| height: 56px !important; | |
| min-height: 56px !important; | |
| font-size: 26px !important; | |
| font-weight: 700 !important; | |
| border-radius: 50% !important; | |
| } | |
| #interval-display { | |
| flex: 0 0 140px; | |
| text-align: center; | |
| font-size: 20px; | |
| font-weight: 600; | |
| } | |
| #interval-label { | |
| text-align: center; | |
| font-size: 13px; | |
| opacity: 0.7; | |
| margin-top: 2px; | |
| } | |
| """ | |
| with gr.Blocks(css=css) as demo: | |
| with gr.Column(elem_id="col-container"): | |
| with gr.Row(elem_id="sticky-controls"): | |
| autoloop_button = gr.Button("\u27F3", elem_id="autoloop-btn") | |
| run_button = gr.Button("Generate", variant="primary", elem_id="generate-btn") | |
| download_button = gr.DownloadButton("Save", visible=False, elem_id="save-btn") | |
| combo_button = gr.Button("\u26A1", elem_id="combo-btn") | |
| result = gr.Image(show_label=False, elem_id="result-hidden") | |
| gr.HTML('<div id="display-wrap"><img id="display-img" src=""></div>') | |
| prompt = gr.Textbox( | |
| label=None, | |
| show_label=False, | |
| placeholder="Describe the image...", | |
| lines=2, | |
| ) | |
| with gr.Column(elem_id="interval-row"): | |
| gr.HTML('<div id="interval-label">Auto-loop interval</div>') | |
| with gr.Row(elem_id="interval-panel"): | |
| interval_minus = gr.Button("\u2212", elem_id="interval-minus") | |
| interval_display = gr.HTML('<div id="interval-display">10 sec</div>') | |
| interval_plus = gr.Button("+", elem_id="interval-plus") | |
| gr.on( | |
| triggers=[run_button.click, prompt.submit], | |
| fn=infer, | |
| inputs=[prompt], | |
| outputs=[result, download_button], | |
| concurrency_limit=1, | |
| ) | |
| result.change( | |
| fn=None, | |
| inputs=None, | |
| outputs=None, | |
| js=""" | |
| () => { | |
| const hiddenImg = document.querySelector('#result-hidden img'); | |
| const displayImg = document.querySelector('#display-img'); | |
| if (hiddenImg && displayImg && hiddenImg.src) { | |
| displayImg.src = hiddenImg.src; | |
| } | |
| } | |
| """, | |
| ) | |
| combo_button.click( | |
| fn=None, | |
| inputs=None, | |
| outputs=None, | |
| js=""" | |
| () => { | |
| const saveBtn = document.querySelector('#save-btn'); | |
| if (saveBtn) { saveBtn.click(); } | |
| setTimeout(() => { | |
| const genBtn = document.querySelector('#generate-btn'); | |
| if (genBtn) { genBtn.click(); } | |
| }, 1000); | |
| } | |
| """, | |
| ) | |
| interval_minus.click( | |
| fn=None, | |
| inputs=None, | |
| outputs=None, | |
| js=""" | |
| () => { | |
| if (!window.__fluxLoop) { window.__fluxLoop = { running: false, timer: null, interval: 10 }; } | |
| window.__fluxLoop.interval = Math.max(1, window.__fluxLoop.interval - 1); | |
| const disp = document.querySelector('#interval-display'); | |
| if (disp) { disp.textContent = window.__fluxLoop.interval + ' sec'; } | |
| } | |
| """, | |
| ) | |
| interval_plus.click( | |
| fn=None, | |
| inputs=None, | |
| outputs=None, | |
| js=""" | |
| () => { | |
| if (!window.__fluxLoop) { window.__fluxLoop = { running: false, timer: null, interval: 10 }; } | |
| window.__fluxLoop.interval = Math.min(60, window.__fluxLoop.interval + 1); | |
| const disp = document.querySelector('#interval-display'); | |
| if (disp) { disp.textContent = window.__fluxLoop.interval + ' sec'; } | |
| } | |
| """, | |
| ) | |
| autoloop_button.click( | |
| fn=None, | |
| inputs=None, | |
| outputs=None, | |
| js=""" | |
| () => { | |
| if (!window.__fluxLoop) { window.__fluxLoop = { running: false, timer: null, interval: 10 }; } | |
| const state = window.__fluxLoop; | |
| const btn = document.querySelector('#autoloop-btn'); | |
| function setColor(running) { | |
| if (btn) { | |
| btn.style.setProperty('background-color', running ? '#2ecc71' : '#e74c3c', 'important'); | |
| btn.style.setProperty('border-color', running ? '#2ecc71' : '#e74c3c', 'important'); | |
| } | |
| } | |
| function cycle() { | |
| if (!state.running) return; | |
| const genBtn = document.querySelector('#generate-btn'); | |
| if (genBtn) { genBtn.click(); } | |
| state.timer = setTimeout(() => { | |
| if (!state.running) return; | |
| const saveBtn = document.querySelector('#save-btn'); | |
| if (saveBtn) { saveBtn.click(); } | |
| state.timer = setTimeout(() => { cycle(); }, 1000); | |
| }, (state.interval || 10) * 1000); | |
| } | |
| if (state.running) { | |
| state.running = false; | |
| clearTimeout(state.timer); | |
| setColor(false); | |
| } else { | |
| state.running = true; | |
| setColor(true); | |
| cycle(); | |
| } | |
| } | |
| """, | |
| ) | |
| demo.queue(max_size=10).launch() |