|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import gradio as gr |
| 6 | + |
| 7 | +from fastvideo.configs.pipelines.base import PipelineConfig |
| 8 | +from fastvideo.configs.sample.base import SamplingParam |
| 9 | +from fastvideo.entrypoints.video_generator import VideoGenerator |
| 10 | +from fastvideo.layers.quantization.fp4_config import FP4Config |
| 11 | +from fastvideo.utils import maybe_download_model |
| 12 | + |
| 13 | +from .config import ( |
| 14 | + GENERATED_CLIP_ROOT, |
| 15 | + MODEL_ID, |
| 16 | + apply_ltx2_defaults, |
| 17 | + resolve_model_path, |
| 18 | + resolve_refine_upsampler_path, |
| 19 | + setup_model_environment, |
| 20 | +) |
| 21 | +from .ui import create_gradio_interface |
| 22 | + |
| 23 | +def main(): |
| 24 | + parser = argparse.ArgumentParser(description="FastVideo Gradio Local Demo") |
| 25 | + parser.add_argument("--t2v_model_paths", type=str, |
| 26 | + default=MODEL_ID, |
| 27 | + help="Comma separated list of paths to the T2V model(s)") |
| 28 | + parser.add_argument("--host", type=str, default="0.0.0.0", |
| 29 | + help="Host to bind to") |
| 30 | + parser.add_argument("--port", type=int, default=7860, |
| 31 | + help="Port to bind to") |
| 32 | + args = parser.parse_args() |
| 33 | + gradio_temp_dir = os.path.abspath("outputs/gradio_tmp") |
| 34 | + os.makedirs(gradio_temp_dir, exist_ok=True) |
| 35 | + os.environ["GRADIO_TEMP_DIR"] = gradio_temp_dir |
| 36 | + generators = {} |
| 37 | + default_params = {} |
| 38 | + model_paths = args.t2v_model_paths.split(",") |
| 39 | + for model_path in model_paths: |
| 40 | + print(f"Loading model: {model_path}") |
| 41 | + setup_model_environment(model_path) |
| 42 | + resolved_model_input = str(resolve_model_path(model_path)) |
| 43 | + model_root = maybe_download_model(resolved_model_input) |
| 44 | + resolved_model_path = Path(model_root) |
| 45 | + |
| 46 | + pipeline_config = PipelineConfig.from_pretrained(str(resolved_model_path)) |
| 47 | + pipeline_config.dit_config.quant_config = FP4Config() |
| 48 | + refine_upsampler_path = resolve_refine_upsampler_path(resolved_model_path) |
| 49 | + print(f"Using refine upsampler: {refine_upsampler_path}") |
| 50 | + |
| 51 | + generators[model_path] = VideoGenerator.from_pretrained( |
| 52 | + str(resolved_model_path), |
| 53 | + num_gpus=1, |
| 54 | + ltx2_refine_enabled=True, |
| 55 | + ltx2_refine_upsampler_path=str(refine_upsampler_path), |
| 56 | + ltx2_refine_lora_path="", # disable refine LoRA for distilled model |
| 57 | + ltx2_refine_num_inference_steps=2, |
| 58 | + ltx2_refine_guidance_scale=1.0, |
| 59 | + ltx2_refine_add_noise=True, |
| 60 | + pipeline_config=pipeline_config, |
| 61 | + enable_torch_compile=True, |
| 62 | + enable_torch_compile_text_encoder=True, |
| 63 | + torch_compile_kwargs={ |
| 64 | + "backend": "inductor", |
| 65 | + "fullgraph": True, |
| 66 | + "mode": "max-autotune-no-cudagraphs", |
| 67 | + "dynamic": False, |
| 68 | + }, |
| 69 | + dit_cpu_offload=False, |
| 70 | + vae_cpu_offload=False, |
| 71 | + text_encoder_cpu_offload=False, |
| 72 | + ltx2_vae_tiling=False, |
| 73 | + ) |
| 74 | + default_params[model_path] = apply_ltx2_defaults( |
| 75 | + SamplingParam.from_pretrained(str(resolved_model_path)) |
| 76 | + ) |
| 77 | + demo = create_gradio_interface(default_params, generators) |
| 78 | + print(f"Starting Gradio frontend at http://{args.host}:{args.port}") |
| 79 | + print(f"T2V Models: {args.t2v_model_paths}") |
| 80 | + |
| 81 | + from fastapi import FastAPI, Request, HTTPException |
| 82 | + from fastapi.responses import HTMLResponse, FileResponse |
| 83 | + import uvicorn |
| 84 | + |
| 85 | + app = FastAPI() |
| 86 | + |
| 87 | + @app.get("/logo.png") |
| 88 | + def get_logo(): |
| 89 | + return FileResponse( |
| 90 | + "assets/full.svg", |
| 91 | + media_type="image/svg+xml", |
| 92 | + headers={ |
| 93 | + "Cache-Control": "public, max-age=3600", |
| 94 | + "Access-Control-Allow-Origin": "*" |
| 95 | + } |
| 96 | + ) |
| 97 | + |
| 98 | + @app.get("/nvidia.png") |
| 99 | + def get_nvidia_logo(): |
| 100 | + return FileResponse( |
| 101 | + "assets/nv.png", |
| 102 | + media_type="image/png", |
| 103 | + headers={ |
| 104 | + "Cache-Control": "public, max-age=3600", |
| 105 | + "Access-Control-Allow-Origin": "*" |
| 106 | + } |
| 107 | + ) |
| 108 | + |
| 109 | + @app.get("/favicon.ico") |
| 110 | + def get_favicon(): |
| 111 | + favicon_path = "assets/icon-simple.svg" |
| 112 | + |
| 113 | + if os.path.exists(favicon_path): |
| 114 | + return FileResponse( |
| 115 | + favicon_path, |
| 116 | + media_type="image/svg+xml", |
| 117 | + headers={ |
| 118 | + "Cache-Control": "public, max-age=3600", |
| 119 | + "Access-Control-Allow-Origin": "*" |
| 120 | + } |
| 121 | + ) |
| 122 | + else: |
| 123 | + raise HTTPException(status_code=404, detail="Favicon not found") |
| 124 | + |
| 125 | + @app.get("/generated-clips/{clip_path:path}") |
| 126 | + def get_generated_clip(clip_path: str): |
| 127 | + root = GENERATED_CLIP_ROOT.resolve() |
| 128 | + resolved_path = (root / clip_path).resolve() |
| 129 | + |
| 130 | + if root not in resolved_path.parents or not resolved_path.is_file(): |
| 131 | + raise HTTPException(status_code=404, detail="Clip not found") |
| 132 | + |
| 133 | + return FileResponse( |
| 134 | + resolved_path, |
| 135 | + media_type="video/mp4", |
| 136 | + headers={ |
| 137 | + "Cache-Control": "no-store", |
| 138 | + "Access-Control-Allow-Origin": "*", |
| 139 | + }, |
| 140 | + ) |
| 141 | + |
| 142 | + @app.get("/", response_class=HTMLResponse) |
| 143 | + def index(request: Request): |
| 144 | + base_url = str(request.base_url).rstrip('/') |
| 145 | + return f""" |
| 146 | + <!DOCTYPE html> |
| 147 | + <html lang="en"> |
| 148 | + <head> |
| 149 | + <meta charset="UTF-8" /> |
| 150 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 151 | + |
| 152 | + <title>FastLTX-2.3</title> |
| 153 | + <meta name="title" content="FastLTX-2.3"> |
| 154 | + <meta name="description" content="Make video generation go blurrrrrrr"> |
| 155 | + <meta name="keywords" content="FastVideo, video generation, AI, machine learning, FastLTX-2.3"> |
| 156 | + |
| 157 | + <meta property="og:type" content="website"> |
| 158 | + <meta property="og:url" content="{base_url}/"> |
| 159 | + <meta property="og:title" content="FastLTX-2.3"> |
| 160 | + <meta property="og:description" content="Make video generation go blurrrrrrr"> |
| 161 | + <meta property="og:image" content="{base_url}/logo.png"> |
| 162 | + <meta property="og:image:width" content="1200"> |
| 163 | + <meta property="og:image:height" content="630"> |
| 164 | + <meta property="og:site_name" content="FastLTX-2.3"> |
| 165 | + |
| 166 | + <meta property="twitter:card" content="summary_large_image"> |
| 167 | + <meta property="twitter:url" content="{base_url}/"> |
| 168 | + <meta property="twitter:title" content="FastLTX-2.3"> |
| 169 | + <meta property="twitter:description" content="Make video generation go blurrrrrrr"> |
| 170 | + <meta property="twitter:image" content="{base_url}/logo.png"> |
| 171 | + <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico"> |
| 172 | + <link rel="icon" type="image/png" sizes="16x16" href="/favicon.ico"> |
| 173 | + <link rel="apple-touch-icon" href="/favicon.ico"> |
| 174 | + <style> |
| 175 | + body, html {{ |
| 176 | + margin: 0; |
| 177 | + padding: 0; |
| 178 | + min-height: 100%; |
| 179 | + width: 100%; |
| 180 | + background: #000; |
| 181 | + background-color: #000; |
| 182 | + background-image: none; |
| 183 | + overscroll-behavior-y: auto; |
| 184 | + scroll-behavior: smooth; |
| 185 | + }} |
| 186 | + body {{ |
| 187 | + position: relative; |
| 188 | + }} |
| 189 | + body::before {{ |
| 190 | + content: ""; |
| 191 | + position: fixed; |
| 192 | + inset: 0; |
| 193 | + background: #000; |
| 194 | + pointer-events: none; |
| 195 | + z-index: -1; |
| 196 | + }} |
| 197 | + iframe {{ |
| 198 | + display: block; |
| 199 | + width: 100%; |
| 200 | + height: 100vh; |
| 201 | + background: #000; |
| 202 | + background-color: #000; |
| 203 | + background-image: none; |
| 204 | + border: none; |
| 205 | + }} |
| 206 | + </style> |
| 207 | + </head> |
| 208 | + <body> |
| 209 | + <iframe src="/gradio" width="100%" height="100%" style="border: none;"></iframe> |
| 210 | + </body> |
| 211 | + </html> |
| 212 | + """ |
| 213 | + |
| 214 | + app = gr.mount_gradio_app( |
| 215 | + app, |
| 216 | + demo, |
| 217 | + path="/gradio", |
| 218 | + allowed_paths=[ |
| 219 | + os.path.abspath("outputs"), |
| 220 | + os.path.abspath("outputs_video"), |
| 221 | + os.path.abspath("fastvideo-logos"), |
| 222 | + ] |
| 223 | + ) |
| 224 | + |
| 225 | + uvicorn.run(app, host=args.host, port=args.port) |
0 commit comments