Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files420 unmodified lines4214224234244254264274284294304314324334344354364374384394404414424438 unmodified lines45245345445545645745845912 unmodified lines47247347447547647747847948048148210 unmodified lines49349449549649749849950017 unmodified lines5185195205215225235247 unmodified lines5325335345355365375385397 unmodified lines54754854955055155255355455555655733 unmodified lines5915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306314 unmodified lines63663763863964064164264364464564664764864965065165265365465565665765865966066166266336 unmodified lines70070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873950 unmodified lines790791792793794795796797798799800801802803804805420 unmodified lines@spaces.GPU(duration=get_duration, size=GPU_SIZE)def _generate(prompt_embeds, text_token_tags, references, height, width, num_frames, steps, seed, loras=()):"""The only thing on GPU time: the two reference encoders, the packed-sequence denoise loop and the decoders.References cross as paths and are decoded here; only the three generated outputs come back. A `@spaces.GPU`argument crosses a process boundary by pickling, a 5 s 1344x768 reference video is 370 MB of expanded frames, andthe full `PipelineState` still holds the packed latents and the rotary grid on the card.The adapters are attached here rather than in the caller: `spaces` runs this body in its own worker, so thetransformer the request sees is the one that has to carry them."""import torchif PLACEMENT == "lazy":PIPE.to("cuda")apply_loras(PIPE.transformer_ref, loras or ())state = PIPE(prompt_embeds=prompt_embeds.to("cuda"),text_token_tags=text_token_tags,8 unmodified linesdef generate(# Every parameter after `prompt` has a default, and the newest ones sit at the end, so a positional API client# written against an older signature keeps working.prompt,image_1=None,audio_path=None,12 unmodified linessteps=28,seed=42,upsample=False,*lora_fields,progress=gr.Progress(track_tqdm=True),):"""One request. The LoRA fields are last and default to empty, so a positional API client that predates them isunaffected. `lora_fields` arrives as `reference, strength, reference, strength, ...`."""if LOAD_ERROR:raise gr.Error(LOAD_ERROR)if PIPE is None:10 unmodified linesderivable = len(audio_bearing(references)) == 1requested = 0 if (match and derivable) else snap_frames(duration)loras, lora_labels = collect_loras(lora_fields, progress)progress(0.0, desc="Upsampling the prompt ..." if upsample else "Reading the prompt and references ...")conditioned = time.time()try:17 unmodified linesprogress(0.1, desc=f"Generating {num_frames / FPS:.1f} s at {width}x{height} ...")started = time.time()frames, audio, sampling_rate = _generate(prompt_embeds, text_token_tags, references, height, width, num_frames, steps, seed, loras)generate_seconds = time.time() - started7 unmodified linesf"({num_frames / FPS:.3f} s), {int(steps)} steps · conditioner {condition_seconds:.0f}s "f"({plan['num_text_tokens']} tokens{', upsampled' if refined else ''}) · "f"denoise + decode {generate_seconds:.0f}s "f"({generate_seconds / int(steps):.1f} s/step) · seed {int(seed)}"f"{' · LoRA ' + ', '.join(lora_labels) if lora_labels else ''}",flush=True,)return path, refined, gr.update(visible=bool(refined))7 unmodified lines# is gone by the next visit, so a saved path would restore as a dead file rather than as the image.SETTINGS_VERSION = 1SETTINGS_KEYS = (["prompt", "upsample", "canvas", "match", "duration", "steps", "seed"]+ [f"lora_{slot + 1}" for slot in range(LORA_SLOTS)]+ [f"lora_{slot + 1}_scale" for slot in range(LORA_SLOTS)])def save_settings(*values):33 unmodified linesreturn updatesdef _fill_lora_slots(files, *current):"""Drop `.safetensors` files on the uploader and their paths land in the first free slots, so a local adapterneeds no typing at all."""slots = list(current)for path in files or []:for index, value in enumerate(slots):if not (value or "").strip():slots[index] = pathbreakreturn [gr.update(value=value) for value in slots]def _add_preset_lora(preset, *current):"""Fill the first free LoRA slot with a preset adapter, set its strength to 1.0 and move the steps slider to thepreset's recommended count.The Turbo presets are tuned for a specific step range, so the steps slider is moved along with the slot — it is theone output beyond the LoRA fields. A slot already holding the same reference is a no-op, so the button can bepressed twice without duplicating, and a full set of slots is left untouched."""reference, steps, _ = LORA_PRESETS[preset]slots = list(current[:LORA_SLOTS])scales = list(current[LORA_SLOTS:])if reference not in [(value or "").strip() for value in slots]:for index, value in enumerate(slots):if not (value or "").strip():slots[index] = referencescales[index] = 1.0breakreturn [*slots, *scales, steps]load_models()INTRO = """# MiniMax-H3 Reference Custom Lora<div align="center"><a href="https://huggingface.co/MiniMaxAI/MiniMax-H3" target="_blank" rel="noopener"><strong>[ model ]</strong></a> 4 unmodified lines**MiniMax-H3** is a 33B parameter state of the art video generation model that produces video and afully synchronized soundtrack (ambience, foley, speech). Bring your own subject, voice or camera move as areference.This Space ships with **pre-wired Turbo LoRA presets** from[`larryvrh/MiniMax-H3-Turbo-Lora`](https://huggingface.co/larryvrh/MiniMax-H3-Turbo-Lora) that render joint video +soundtrack in **4–8 steps** instead of the usual ~20 (a ~5× speedup). It also accepts **ComfyUI-trained LoRAs** —adapters in the ComfyUI checkpoint naming are remapped to diffusers' module names on the fly, so a `.safetensors`trained against the ComfyUI base works without a separate conversion step."""LORA_HELP = """Each slot takes a Hugging Face repo (`owner/repo`), a file inside one(`owner/repo/name.safetensors`), a file URL, or a local path — or just drop the files below. A strength of `0`switches a slot off without clearing it. Adapters have to be trained against the `transformer_ref/` partition.**Turbo LoRA presets** — from[`larryvrh/MiniMax-H3-Turbo-Lora`](https://huggingface.co/larryvrh/MiniMax-H3-Turbo-Lora), a few-step distillation thatrenders joint video + soundtrack in **4–8 steps** instead of the usual ~20 (a ~5× speedup). Pick one from the dropdownand **Add to a free slot** to fill a slot, set its strength to `1.0` and move the steps slider to the recommendedcount. Keep strength at `1.0`; only nudge it if a specific clip misbehaves (smear → up, over-sharp → down)."""SETTINGS_HELP = """Saves the prompt, the canvas, the sliders and the LoRA slots — everything typed rather thanuploaded. Images, audio and video are not saved: gradio keeps them in a temporary folder that is gone by the nextvisit, so a saved path would come back as a dead file."""CSS = """36 unmodified lineswith gr.Tab("Video"):video = gr.Video(label="Motion & camera, 2–15 s. Its soundtrack comes along.")run = gr.Button("Generate", variant="primary")with gr.Accordion("LoRA", open=False):gr.Markdown(LORA_HELP)with gr.Row():lora_preset = gr.Dropdown(label="Turbo LoRA presets",choices=list(LORA_PRESETS),value=list(LORA_PRESETS)[0],scale=4,)lora_preset_add = gr.Button("Add to a free slot", size="sm", variant="secondary", scale=1)lora_references, lora_scales = [], []for slot in range(LORA_SLOTS):with gr.Row():lora_references.append(gr.Textbox(label=f"LoRA {slot + 1}", placeholder="owner/repo", scale=3))lora_scales.append(gr.Slider(label="Strength",minimum=LORA_MIN_SCALE,maximum=LORA_MAX_SCALE,step=0.05,value=1.0,scale=2,))lora_upload = gr.File(label="Drop .safetensors here to fill the slots",file_count="multiple",file_types=[".safetensors"],type="filepath",)with gr.Accordion("Advanced options", open=False):canvas = gr.Dropdown(label="Canvas", choices=list(CANVASES), value=DEFAULT_CANVAS)match = gr.Checkbox(label="Match the reference soundtrack", value=True, visible=False)50 unmodified lines)# Same order as `SETTINGS_KEYS`.settings_fields = [prompt, upsample, canvas, match, duration, steps, seed, *lora_references, *lora_scales]save.click(save_settings, settings_fields, settings_download, api_name=False)settings_upload.upload(load_settings, settings_upload, settings_fields, api_name=False)# Same order as `generate`'s signature: the five leading columns first, then the remaining image slots, then the# LoRA fields the `*lora_fields` tail collects.request = [prompt, images[0], audio, video, canvas, *images[1:], match, duration, steps, seed, upsample, *lora_inputs]run.click(generate, request, [result, upsampled, upsampled_panel], api_name="generate")420 unmodified lines4214224234244254264274284294304314324334344354364374384398 unmodified lines44844945045145245312 unmodified lines46646746846947047147247347410 unmodified lines48548648748848949017 unmodified lines5085095105115125135147 unmodified lines5225235245255265275287 unmodified lines53653753853954054154233 unmodified lines5765775785795805815825835844 unmodified lines58959059159259359436 unmodified lines63163263363463563650 unmodified lines687688689690691692693694695696697698699420 unmodified lines@spaces.GPU(duration=get_duration, size=GPU_SIZE)@spaces.GPU(duration=get_duration, size=GPU_SIZE)def _generate(prompt_embeds, text_token_tags, references, height, width, num_frames, steps, seed, loras=()):"""The only thing on GPU time: the two reference encoders, the packed-sequence denoise loop and the decoders.References cross as paths and are decoded here; only the three generated outputs come back. A `@spaces.GPU`argument crosses a process boundary by pickling, a 5 s 1344x768 reference video is 370 MB of expanded frames, andthe full `PipelineState` still holds the packed latents and the rotary grid on the card.The adapters are attached here rather than in the caller: `spaces` runs this body in its own worker, so thetransformer the request sees is the one that has to carry them."""import torchif PLACEMENT == "lazy":PIPE.to("cuda")apply_loras(PIPE.transformer_ref, loras or ())state = PIPE(prompt_embeds=prompt_embeds.to("cuda"),text_token_tags=text_token_tags,8 unmodified linesdef generate(prompt,image_1=None,audio_path=None,12 unmodified linessteps=28,seed=42,upsample=False,progress=gr.Progress(track_tqdm=True),):"""One request."""if LOAD_ERROR:raise gr.Error(LOAD_ERROR)if PIPE is None:10 unmodified linesderivable = len(audio_bearing(references)) == 1requested = 0 if (match and derivable) else snap_frames(duration)progress(0.0, desc="Upsampling the prompt ..." if upsample else "Reading the prompt and references ...")conditioned = time.time()try:17 unmodified linesprogress(0.1, desc=f"Generating {num_frames / FPS:.1f} s at {width}x{height} ...")started = time.time()frames, audio, sampling_rate = _generate(prompt_embeds, text_token_tags, references, height, width, num_frames, steps, seed)generate_seconds = time.time() - started7 unmodified linesf"({num_frames / FPS:.3f} s), {int(steps)} steps · conditioner {condition_seconds:.0f}s "f"({plan['num_text_tokens']} tokens{', upsampled' if refined else ''}) · "f"denoise + decode {generate_seconds:.0f}s "f"({generate_seconds / int(steps):.1f} s/step) · seed {int(seed)}",flush=True,)return path, refined, gr.update(visible=bool(refined))7 unmodified lines# is gone by the next visit, so a saved path would restore as a dead file rather than as the image.SETTINGS_VERSION = 1SETTINGS_KEYS = ["prompt", "upsample", "canvas", "match", "duration", "steps", "seed"]def save_settings(*values):33 unmodified linesreturn updatesload_models()INTRO = """# MiniMax-H3 Reference<div align="center"><a href="https://huggingface.co/MiniMaxAI/MiniMax-H3" target="_blank" rel="noopener"><strong>[ model ]</strong></a> 4 unmodified lines**MiniMax-H3** is a 33B parameter state of the art video generation model that produces video and afully synchronized soundtrack (ambience, foley, speech). Bring your own subject, voice or camera move as areference."""CSS = """36 unmodified lineswith gr.Tab("Video"):video = gr.Video(label="Motion & camera, 2–15 s. Its soundtrack comes along.")run = gr.Button("Generate", variant="primary")with gr.Accordion("Advanced options", open=False):canvas = gr.Dropdown(label="Canvas", choices=list(CANVASES), value=DEFAULT_CANVAS)match = gr.Checkbox(label="Match the reference soundtrack", value=True, visible=False)50 unmodified lines)# Same order as `SETTINGS_KEYS`.settings_fields = [prompt, upsample, canvas, match, duration, steps, seed]save.click(save_settings, settings_fields, settings_download, api_name=False)settings_upload.upload(load_settings, settings_upload, settings_fields, api_name=False)# Same order as `generate`'s signature: the five leading columns first, then the remaining image slots.request = [prompt, images[0], audio, video, canvas, *images[1:], match, duration, steps, seed, upsample]run.click(generate, request, [result, upsampled, upsampled_panel], api_name="generate")
app.py+10-116