Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files420 unmodified lines4214224234244254264264274284294304304314324334354344354384394364404374384398 unmodified lines44844945045545645145245312 unmodified lines46646746847546947047847947147247347410 unmodified lines48548648749649748848949017 unmodified lines5085095105215115125135147 unmodified lines5225235245355365255265275287 unmodified lines53653753855055153955255355454054154233 unmodified lines5765775785945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246255795806285815825835844 unmodified lines58959059163964064164264364464564664764864965065165265365465565665765865966059259359436 unmodified lines63163263370370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573663463563650 unmodified lines687688689793690691692796797693798694800695696802697698699420 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, and
the 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 the
transformer the request sees is the one that has to carry them.
"""
import torch
if PLACEMENT == "lazy":
PIPE.to("cuda")
apply_loras(PIPE.transformer_ref, loras or ())
apply_loras(PIPE.transformer_ref, loras or ())
state = PIPE(
prompt_embeds=prompt_embeds.to("cuda"),
text_token_tags=text_token_tags,8 unmodified lines
def 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 lines steps=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 is
unaffected. `lora_fields` arrives as `reference, strength, reference, strength, ...`."""
"""One request."""
if LOAD_ERROR:
raise gr.Error(LOAD_ERROR)
if PIPE is None:10 unmodified lines derivable = len(audio_bearing(references)) == 1
requested = 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 lines progress(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
prompt_embeds, text_token_tags, references, height, width, num_frames, steps, seed
)
generate_seconds = time.time() - started
7 unmodified lines f"({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 ''}",
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 = 1
SETTINGS_KEYS = (
["prompt", "upsample", "canvas", "match", "duration", "steps", "seed"]
SETTINGS_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 lines return updates
def _fill_lora_slots(files, *current):
"""Drop `.safetensors` files on the uploader and their paths land in the first free slots, so a local adapter
needs 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] = path
break
return [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 the
preset'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 the
one output beyond the LoRA fields. A slot already holding the same reference is a no-op, so the button can be
pressed 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] = reference
scales[index] = 1.0
break
return [*slots, *scales, steps]
load_models()
INTRO = """# MiniMax-H3 Reference Custom Lora
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 a
fully synchronized soundtrack (ambience, foley, speech). Bring your own subject, voice or camera move as a
reference.
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 that
renders joint video + soundtrack in **4–8 steps** instead of the usual ~20 (a ~5× speedup). Pick one from the dropdown
and **Add to a free slot** to fill a slot, set its strength to `1.0` and move the steps slider to the recommended
count. 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 than
uploaded. Images, audio and video are not saved: gradio keeps them in a temporary folder that is gone by the next
visit, so a saved path would come back as a dead file.
"""
CSS = """36 unmodified lines with 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]
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, then the
# Same order as `generate`'s signature: the five leading columns first, then the remaining image slots.
# LoRA fields the `*lora_fields` tail collects.
request = [
prompt, images[0], audio, video, canvas, *images[1:], match, duration, steps, seed, upsample, *lora_inputs
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