Download app.py from Quantumbraid/video-model-factory: direct link, hf CLI and curl.
- Browser
- Download file 6.48 kB
-
https://huggingface.co/spaces/Quantumbraid/video-model-factory/resolve/main/app.py
- Command line
-
hf download hf://spaces/Quantumbraid/video-model-factory/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Quantumbraid/video-model-factory/resolve/main/app.py
6.48 kB
| from __future__ import annotations | |
| import traceback | |
| from pathlib import Path | |
| import gradio as gr | |
| import spaces | |
| from PIL import Image | |
| from factory_config import FACTORY_PATH | |
| from runtime import render_video | |
| from scanner import scan_hub_repo | |
| from storage import effective_factory, lora_library, register_imports, start_prepare_background, status_text | |
| start_prepare_background() | |
| def refresh_controls(): | |
| config = effective_factory() | |
| models = list(config.models) | |
| loras = lora_library() | |
| return ( | |
| gr.update(choices=models, value=models[0] if models else None), | |
| gr.update(choices=loras, value=[]), | |
| status_text(), | |
| ) | |
| def generate(*args): | |
| try: | |
| video, last_path, used_seed = render_video(*args) | |
| return video, last_path, used_seed, f"Rendered with seed {used_seed}. Last frame is ready to chain." | |
| except Exception as exc: | |
| traceback.print_exc() | |
| raise gr.Error(f"{type(exc).__name__}: {exc}") from exc | |
| def chain_last_frame(last_path: str | None): | |
| if not last_path or not Path(last_path).exists(): | |
| raise gr.Error("Render a video first.") | |
| return Image.open(last_path).convert("RGB"), "Last rendered frame is now the next first frame." | |
| def inspect_repo(repo_id: str): | |
| if not repo_id.strip(): | |
| raise gr.Error("Paste a Hugging Face model or Space ID.") | |
| try: | |
| result = scan_hub_repo(repo_id) | |
| except Exception as exc: | |
| raise gr.Error(f"Could not inspect {repo_id}: {type(exc).__name__}: {exc}") from exc | |
| evidence = "\n".join(f"- {line}" for line in result.evidence[:80]) or "- No model evidence found." | |
| return ",".join(result.models), ",".join(result.loras), result.factory, evidence | |
| def add_to_bucket(models_csv: str, loras_csv: str): | |
| if not models_csv.strip() and not loras_csv.strip(): | |
| raise gr.Error("Nothing was detected. Edit either detected list before importing.") | |
| message = register_imports(models_csv, loras_csv) | |
| config = effective_factory() | |
| return message + " Use Refresh after downloads finish.", gr.update( | |
| choices=list(config.models), value=config.models[-1] if config.models else None | |
| ) | |
| config = effective_factory() | |
| initial_models = list(config.models) | |
| with gr.Blocks(title="Video Model Factory") as demo: | |
| gr.Markdown( | |
| "# Video Model Factory\n" | |
| "Models and LoRAs come from the two lines in `factory.txt`; downloaded weights live in the mounted bucket." | |
| ) | |
| with gr.Tab("Render"): | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| model = gr.Dropdown(initial_models, value=initial_models[0] if initial_models else None, label="Model") | |
| loras = gr.Dropdown(lora_library(), multiselect=True, label="LoRAs from bucket") | |
| lora_scale = gr.Slider(0, 2, value=1, step=0.05, label="LoRA scale") | |
| first = gr.Image(type="pil", label="First frame", sources=["upload", "clipboard"]) | |
| last = gr.Image(type="pil", label="Last frame (optional)", sources=["upload", "clipboard"]) | |
| with gr.Column(scale=2): | |
| prompt = gr.Textbox(lines=5, label="Prompt") | |
| negative = gr.Textbox(lines=2, label="Negative prompt") | |
| with gr.Row(): | |
| width = gr.Slider(256, 1280, value=832, step=32, label="Width") | |
| height = gr.Slider(256, 1280, value=480, step=32, label="Height") | |
| with gr.Row(): | |
| frames = gr.Slider(9, 161, value=81, step=4, label="Frames") | |
| fps = gr.Slider(4, 30, value=16, step=1, label="FPS") | |
| with gr.Row(): | |
| steps = gr.Slider(1, 80, value=30, step=1, label="Steps") | |
| guidance = gr.Slider(0, 15, value=5, step=0.1, label="Guidance") | |
| seed = gr.Number(value=-1, precision=0, label="Seed (-1 random)") | |
| render = gr.Button("Render video", variant="primary") | |
| video = gr.Video(label="Rendered video") | |
| last_state = gr.Textbox(visible=False) | |
| used_seed = gr.Number(label="Used seed", precision=0) | |
| chain = gr.Button("Use last rendered frame as next first frame") | |
| render_status = gr.Markdown() | |
| inputs = [model, loras, prompt, negative, first, last, width, height, frames, fps, steps, guidance, lora_scale, seed] | |
| render.click(generate, inputs=inputs, outputs=[video, last_state, used_seed, render_status]) | |
| chain.click(chain_last_frame, inputs=last_state, outputs=[first, render_status]) | |
| with gr.Tab("Import from Hub"): | |
| gr.Markdown( | |
| "Paste a model repo ID or another Space ID. The inspector reads source text only; it never executes the other Space. " | |
| "It looks at loader calls, model/LoRA keywords, and `.safetensors` / `.gguf` references." | |
| ) | |
| repo = gr.Textbox(label="Hugging Face repo or Space ID", placeholder="owner/name") | |
| inspect_button = gr.Button("Inspect") | |
| found_models = gr.Textbox(label="Detected models (editable before import)") | |
| found_loras = gr.Textbox(label="Detected LoRAs (editable before import)") | |
| proposed = gr.Code(language=None, label="Proposed factory.txt (copy these two lines)") | |
| evidence = gr.Markdown(label="Evidence") | |
| inspect_button.click(inspect_repo, inputs=repo, outputs=[found_models, found_loras, proposed, evidence]) | |
| add_button = gr.Button("Add detected models and LoRAs to bucket", variant="primary") | |
| import_status = gr.Markdown() | |
| add_button.click( | |
| add_to_bucket, | |
| inputs=[found_models, found_loras], | |
| outputs=[import_status, model], | |
| ) | |
| with gr.Tab("Factory & bucket"): | |
| gr.Code(FACTORY_PATH.read_text(encoding="utf-8"), language=None, label="Current factory.txt") | |
| refresh = gr.Button("Refresh model and LoRA library") | |
| bucket_status = gr.Markdown(status_text()) | |
| refresh.click(refresh_controls, outputs=[model, loras, bucket_status]) | |
| demo.queue(default_concurrency_limit=1).launch( | |
| allowed_paths=[str(Path("/data")), str(Path(__file__).parent)], | |
| # Spaces enables Gradio's Node/SvelteKit SSR proxy by default. The app does | |
| # not need SSR, and bypassing it avoids noisy malformed/stray POST failures | |
| # such as "Too little data for declared Content-Length" and page-route 405s. | |
| ssr_mode=False, | |
| ) | |