Spaces:
Runtime error
Runtime error
Download ui/h3_ui.py from Samuelsrmendozs/MiniMax-H3-e: direct link, hf CLI and curl.
- Browser
- Download file 19.3 kB
-
https://huggingface.co/spaces/Samuelsrmendozs/MiniMax-H3-e/resolve/main/ui/h3_ui.py
- Command line
-
hf download hf://spaces/Samuelsrmendozs/MiniMax-H3-e/ui/h3_ui.py
-
curl -L -o h3_ui.py https://huggingface.co/spaces/Samuelsrmendozs/MiniMax-H3-e/resolve/main/ui/h3_ui.py
19.3 kB
| import gradio as gr | |
| from .h3_logic import process_inputs, RESOLUTION_PRESETS | |
| from core.generation_logic import generate_h3_video_wrapper | |
| from .shared.ui_components import ( | |
| create_lora_settings_ui, | |
| create_h3_controlnet_ui, | |
| create_h3_guide_ui | |
| ) | |
| from .events.chain_handlers import ( | |
| create_lora_event_handlers, | |
| create_h3_controlnet_event_handlers, | |
| create_h3_guide_event_handlers | |
| ) | |
| MAX_REF_IMAGES = 9 | |
| MAX_REF_VIDEOS = 3 | |
| MAX_REF_AUDIOS = 3 | |
| TASK_CHOICES = ["T2VA", "I2VA", "FLF2VA", "REF2VA"] | |
| def create_ui(): | |
| prefix = "h3" | |
| components = {} | |
| with gr.Column(): | |
| gr.Markdown("## MiniMax H3 Video Generation") | |
| gr.Markdown( | |
| "💡 **Tip:** Under the default ZeroGPU Duration of 120s, tasks with 544p resolution, 20 steps, and 5s duration complete successfully." | |
| ) | |
| components['task'] = gr.Radio( | |
| choices=TASK_CHOICES, | |
| value="T2VA", | |
| label="Task", | |
| interactive=True | |
| ) | |
| with gr.Row(visible=False) as frame_row: | |
| components['first_frame'] = gr.Image(type="pil", label="First Frame", height=220, visible=False) | |
| components['last_frame'] = gr.Image(type="pil", label="Last Frame", height=220, visible=False) | |
| components['frame_row'] = frame_row | |
| components['prompt'] = gr.Textbox(label="Prompt", lines=5) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| with gr.Row(): | |
| components['resolution'] = gr.Radio( | |
| label="Resolution", | |
| choices=["544p", "768p", "1080p"], | |
| value="544p", | |
| interactive=True | |
| ) | |
| with gr.Row(): | |
| components['aspect_ratio'] = gr.Dropdown( | |
| label="Aspect Ratio", | |
| choices=list(RESOLUTION_PRESETS["544p"].keys()), | |
| value="16:9 (Landscape)", | |
| interactive=True | |
| ) | |
| with gr.Row(): | |
| components['width'] = gr.Number(label="Width", value=960, precision=0) | |
| components['height'] = gr.Number(label="Height", value=544, precision=0) | |
| with gr.Row(visible=False): | |
| components['sampler'] = gr.Dropdown(label="Sampler", choices=["res_multistep", "euler"], value="res_multistep", visible=False) | |
| components['scheduler'] = gr.Dropdown(label="Scheduler", choices=["simple", "normal"], value="simple", visible=False) | |
| with gr.Row(): | |
| components['steps'] = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=20, interactive=True) | |
| components['duration'] = gr.Slider(label="Duration (s)", minimum=0.2, maximum=15.0, step=0.1, value=5.0, interactive=True) | |
| with gr.Row(): | |
| components['cfg_scale'] = gr.Slider(label="CFG Scale", minimum=1.0, maximum=20.0, step=0.1, value=5.0, visible=False) | |
| components['seed'] = gr.Number(label="Seed (-1 for random)", value=-1, precision=0) | |
| components['zero_gpu_duration'] = gr.Number( | |
| label="ZeroGPU Duration (s)", | |
| value=120, | |
| placeholder="Default: 120s, Max: 120s" | |
| ) | |
| with gr.Column(scale=1): | |
| components['output_video'] = gr.Video(label="Result", interactive=False, height=492) | |
| # Chain Modules (LoRA) | |
| components.update(create_lora_settings_ui(prefix)) | |
| # Multi-modal Reference Panels (REF2VA) | |
| with gr.Accordion("Reference Image Settings", open=False, visible=False) as ref_img_accordion: | |
| ref_image_groups = [] | |
| ref_image_inputs = [] | |
| with gr.Row(): | |
| for i in range(MAX_REF_IMAGES): | |
| with gr.Column(visible=(i < 1), min_width=160) as img_col: | |
| img_comp = gr.Image(type="pil", label=f"Ref Image {i+1}", sources=["upload"], height=160) | |
| ref_image_groups.append(img_col) | |
| ref_image_inputs.append(img_comp) | |
| components['ref_image_groups'] = ref_image_groups | |
| components['ref_image_inputs'] = ref_image_inputs | |
| components['ref_images'] = ref_image_inputs | |
| with gr.Row(): | |
| components['add_ref_button'] = gr.Button("✚ Add Reference Image") | |
| components['delete_ref_button'] = gr.Button("➖ Delete Reference Image", visible=False) | |
| components['ref_count_state'] = gr.State(1) | |
| components['ref_img_accordion'] = ref_img_accordion | |
| with gr.Accordion("Reference Video Settings", open=False, visible=False) as ref_vid_accordion: | |
| ref_video_groups = [] | |
| ref_video_inputs = [] | |
| with gr.Row(): | |
| for i in range(MAX_REF_VIDEOS): | |
| with gr.Column(visible=(i < 1), min_width=200) as vid_col: | |
| vid_comp = gr.Video(label=f"Ref Video {i+1}", sources=["upload"], height=200) | |
| ref_video_groups.append(vid_col) | |
| ref_video_inputs.append(vid_comp) | |
| components['ref_video_groups'] = ref_video_groups | |
| components['ref_video_inputs'] = ref_video_inputs | |
| components['ref_videos'] = ref_video_inputs | |
| with gr.Row(): | |
| components['add_ref_video_button'] = gr.Button("✚ Add Reference Video") | |
| components['delete_ref_video_button'] = gr.Button("➖ Delete Reference Video", visible=False) | |
| components['ref_video_count_state'] = gr.State(1) | |
| components['ref_vid_accordion'] = ref_vid_accordion | |
| with gr.Accordion("Reference Audio Settings", open=False, visible=False) as ref_aud_accordion: | |
| ref_audio_groups = [] | |
| ref_audio_inputs = [] | |
| with gr.Row(): | |
| for i in range(MAX_REF_AUDIOS): | |
| with gr.Column(visible=(i < 1), min_width=200) as aud_col: | |
| aud_comp = gr.Audio(label=f"Ref Audio {i+1}", sources=["upload"], type="filepath") | |
| ref_audio_groups.append(aud_col) | |
| ref_audio_inputs.append(aud_comp) | |
| components['ref_audio_groups'] = ref_audio_groups | |
| components['ref_audio_inputs'] = ref_audio_inputs | |
| components['ref_audios'] = ref_audio_inputs | |
| with gr.Row(): | |
| components['add_ref_audio_button'] = gr.Button("✚ Add Reference Audio") | |
| components['delete_ref_audio_button'] = gr.Button("➖ Delete Reference Audio", visible=False) | |
| components['ref_audio_count_state'] = gr.State(1) | |
| components['ref_aud_accordion'] = ref_aud_accordion | |
| # ControlNet Settings (Placed below Reference Audio Settings) | |
| components.update(create_h3_controlnet_ui(prefix)) | |
| # Timeline Keyframe Guide Panel (Shared across all tasks) | |
| components.update(create_h3_guide_ui(prefix)) | |
| components['run_button'] = gr.Button("🎬 Generate H3 Video", variant="primary") | |
| return components | |
| def create_event_handlers(components: dict, demo: gr.Blocks): | |
| prefix = "h3" | |
| create_lora_event_handlers(prefix, components) | |
| create_h3_controlnet_event_handlers(prefix, components) | |
| create_h3_guide_event_handlers(prefix, components) | |
| def on_task_change(task_val): | |
| is_i2va = (task_val == "I2VA") | |
| is_flf2va = (task_val in ("FLF2VA", "FL2VA")) | |
| is_ref2va = (task_val == "REF2VA") | |
| show_frame_row = is_i2va or is_flf2va | |
| return ( | |
| gr.update(visible=show_frame_row), # frame_row | |
| gr.update(visible=show_frame_row), # first_frame | |
| gr.update(visible=is_flf2va), # last_frame | |
| gr.update(visible=is_ref2va), # ref_img_accordion | |
| gr.update(visible=is_ref2va), # ref_vid_accordion | |
| gr.update(visible=is_ref2va) # ref_aud_accordion | |
| ) | |
| components['task'].change( | |
| fn=on_task_change, | |
| inputs=[components['task']], | |
| outputs=[ | |
| components['frame_row'], | |
| components['first_frame'], | |
| components['last_frame'], | |
| components['ref_img_accordion'], | |
| components['ref_vid_accordion'], | |
| components['ref_aud_accordion'] | |
| ], | |
| api_name=False | |
| ) | |
| def update_dimensions(resolution, aspect_ratio): | |
| w, h = RESOLUTION_PRESETS.get(resolution, {}).get(aspect_ratio, (960, 544)) | |
| return w, h | |
| components['resolution'].change( | |
| fn=update_dimensions, | |
| inputs=[components['resolution'], components['aspect_ratio']], | |
| outputs=[components['width'], components['height']], | |
| api_name=False | |
| ) | |
| components['aspect_ratio'].change( | |
| fn=update_dimensions, | |
| inputs=[components['resolution'], components['aspect_ratio']], | |
| outputs=[components['width'], components['height']], | |
| api_name=False | |
| ) | |
| # Reference Image events | |
| ref_count_state = components['ref_count_state'] | |
| add_ref_btn = components['add_ref_button'] | |
| del_ref_btn = components['delete_ref_button'] | |
| ref_image_groups = components['ref_image_groups'] | |
| ref_image_inputs = components['ref_image_inputs'] | |
| def add_ref_row(count): | |
| count += 1 | |
| visibility_updates = tuple(gr.update(visible=i < count) for i in range(MAX_REF_IMAGES)) | |
| return (count, gr.update(visible=count < MAX_REF_IMAGES), gr.update(visible=count > 1)) + visibility_updates | |
| def delete_ref_row(count): | |
| count -= 1 | |
| image_clear_updates = [gr.update()] * MAX_REF_IMAGES | |
| if count >= 0: | |
| image_clear_updates[count] = None | |
| visibility_updates = tuple(gr.update(visible=i < count) for i in range(MAX_REF_IMAGES)) | |
| return (count, gr.update(visible=count < MAX_REF_IMAGES), gr.update(visible=count > 1)) + visibility_updates + tuple(image_clear_updates) | |
| add_ref_outputs = [ref_count_state, add_ref_btn, del_ref_btn] + ref_image_groups | |
| del_ref_outputs = [ref_count_state, add_ref_btn, del_ref_btn] + ref_image_groups + ref_image_inputs | |
| add_ref_btn.click( | |
| fn=add_ref_row, | |
| inputs=[ref_count_state], | |
| outputs=add_ref_outputs, | |
| show_progress=False, | |
| api_name=False | |
| ) | |
| del_ref_btn.click( | |
| fn=delete_ref_row, | |
| inputs=[ref_count_state], | |
| outputs=del_ref_outputs, | |
| show_progress=False, | |
| api_name=False | |
| ) | |
| # Reference Video events | |
| ref_video_count_state = components['ref_video_count_state'] | |
| add_ref_vid_btn = components['add_ref_video_button'] | |
| del_ref_vid_btn = components['delete_ref_video_button'] | |
| ref_video_groups = components['ref_video_groups'] | |
| ref_video_inputs = components['ref_video_inputs'] | |
| def add_ref_video_row(count): | |
| count += 1 | |
| visibility_updates = tuple(gr.update(visible=i < count) for i in range(MAX_REF_VIDEOS)) | |
| return (count, gr.update(visible=count < MAX_REF_VIDEOS), gr.update(visible=count > 1)) + visibility_updates | |
| def delete_ref_video_row(count): | |
| count -= 1 | |
| video_clear_updates = [gr.update()] * MAX_REF_VIDEOS | |
| if count >= 0: | |
| video_clear_updates[count] = None | |
| visibility_updates = tuple(gr.update(visible=i < count) for i in range(MAX_REF_VIDEOS)) | |
| return (count, gr.update(visible=count < MAX_REF_VIDEOS), gr.update(visible=count > 1)) + visibility_updates + tuple(video_clear_updates) | |
| add_ref_vid_outputs = [ref_video_count_state, add_ref_vid_btn, del_ref_vid_btn] + ref_video_groups | |
| del_ref_vid_outputs = [ref_video_count_state, add_ref_vid_btn, del_ref_vid_btn] + ref_video_groups + ref_video_inputs | |
| add_ref_vid_btn.click( | |
| fn=add_ref_video_row, | |
| inputs=[ref_video_count_state], | |
| outputs=add_ref_vid_outputs, | |
| show_progress=False, | |
| api_name=False | |
| ) | |
| del_ref_vid_btn.click( | |
| fn=delete_ref_video_row, | |
| inputs=[ref_video_count_state], | |
| outputs=del_ref_vid_outputs, | |
| show_progress=False, | |
| api_name=False | |
| ) | |
| # Reference Audio events | |
| ref_audio_count_state = components['ref_audio_count_state'] | |
| add_ref_aud_btn = components['add_ref_audio_button'] | |
| del_ref_aud_btn = components['delete_ref_audio_button'] | |
| ref_audio_groups = components['ref_audio_groups'] | |
| ref_audio_inputs = components['ref_audio_inputs'] | |
| def add_ref_audio_row(count): | |
| count += 1 | |
| visibility_updates = tuple(gr.update(visible=i < count) for i in range(MAX_REF_AUDIOS)) | |
| return (count, gr.update(visible=count < MAX_REF_AUDIOS), gr.update(visible=count > 1)) + visibility_updates | |
| def delete_ref_audio_row(count): | |
| count -= 1 | |
| audio_clear_updates = [gr.update()] * MAX_REF_AUDIOS | |
| if count >= 0: | |
| audio_clear_updates[count] = None | |
| visibility_updates = tuple(gr.update(visible=i < count) for i in range(MAX_REF_AUDIOS)) | |
| return (count, gr.update(visible=count < MAX_REF_AUDIOS), gr.update(visible=count > 1)) + visibility_updates + tuple(audio_clear_updates) | |
| add_ref_aud_outputs = [ref_audio_count_state, add_ref_aud_btn, del_ref_aud_btn] + ref_audio_groups | |
| del_ref_aud_outputs = [ref_audio_count_state, add_ref_aud_btn, del_ref_aud_btn] + ref_audio_groups + ref_audio_inputs | |
| add_ref_aud_btn.click( | |
| fn=add_ref_audio_row, | |
| inputs=[ref_audio_count_state], | |
| outputs=add_ref_aud_outputs, | |
| show_progress=False, | |
| api_name=False | |
| ) | |
| del_ref_aud_btn.click( | |
| fn=delete_ref_audio_row, | |
| inputs=[ref_audio_count_state], | |
| outputs=del_ref_aud_outputs, | |
| show_progress=False, | |
| api_name=False | |
| ) | |
| # Collect LoRA, ControlNet, and Guide components | |
| lora_sources = components.get(f'lora_sources_{prefix}', []) | |
| lora_ids = components.get(f'lora_ids_{prefix}', []) | |
| lora_scales = components.get(f'lora_scales_{prefix}', []) | |
| cn_videos = components.get(f'h3_controlnet_videos_{prefix}', []) | |
| cn_types = components.get(f'h3_controlnet_types_{prefix}', []) | |
| cn_series = components.get(f'h3_controlnet_series_{prefix}', []) | |
| cn_strengths = components.get(f'h3_controlnet_strengths_{prefix}', []) | |
| cn_start_percents = components.get(f'h3_controlnet_start_percents_{prefix}', []) | |
| cn_end_percents = components.get(f'h3_controlnet_end_percents_{prefix}', []) | |
| cn_filepaths = components.get(f'h3_controlnet_filepaths_{prefix}', []) | |
| guide_images = components.get(f'h3_guide_images_{prefix}', []) | |
| guide_videos = components.get(f'h3_guide_videos_{prefix}', []) | |
| guide_audios = components.get(f'h3_guide_audios_{prefix}', []) | |
| guide_times = components.get(f'h3_guide_times_{prefix}', []) | |
| guide_frames = components.get(f'h3_guide_frames_{prefix}', []) | |
| inputs_list = [ | |
| components['task'], | |
| components['first_frame'], | |
| components['last_frame'], | |
| components['prompt'], | |
| components['resolution'], | |
| components['aspect_ratio'], | |
| components['width'], | |
| components['height'], | |
| components['duration'], | |
| components['sampler'], | |
| components['scheduler'], | |
| components['steps'], | |
| components['cfg_scale'], | |
| components['seed'], | |
| components['zero_gpu_duration'] | |
| ] + ref_image_inputs + ref_video_inputs + ref_audio_inputs + lora_sources + lora_ids + lora_scales + cn_videos + cn_types + cn_series + cn_strengths + cn_start_percents + cn_end_percents + cn_filepaths + guide_images + guide_videos + guide_audios + guide_times + guide_frames | |
| def on_run_click(*args, progress=gr.Progress(track_tqdm=True)): | |
| task, first_frame, last_frame, prompt, resolution, aspect_ratio, width, height, duration, sampler, scheduler, steps, cfg_scale, seed, zero_gpu_duration = args[:15] | |
| idx = 15 | |
| imgs = list(args[idx : idx + len(ref_image_inputs)]) | |
| idx += len(ref_image_inputs) | |
| vids = list(args[idx : idx + len(ref_video_inputs)]) | |
| idx += len(ref_video_inputs) | |
| auds = list(args[idx : idx + len(ref_audio_inputs)]) | |
| idx += len(ref_audio_inputs) | |
| num_loras = len(lora_sources) | |
| l_sources = args[idx : idx + num_loras] | |
| l_ids = args[idx + num_loras : idx + 2*num_loras] | |
| l_scales = args[idx + 2*num_loras : idx + 3*num_loras] | |
| idx += 3 * num_loras | |
| num_cns = len(cn_videos) | |
| c_vids = args[idx : idx + num_cns] | |
| c_types = args[idx + num_cns : idx + 2*num_cns] | |
| c_series = args[idx + 2*num_cns : idx + 3*num_cns] | |
| c_strengths = args[idx + 3*num_cns : idx + 4*num_cns] | |
| c_starts = args[idx + 4*num_cns : idx + 5*num_cns] | |
| c_ends = args[idx + 5*num_cns : idx + 6*num_cns] | |
| c_fps = args[idx + 6*num_cns : idx + 7*num_cns] | |
| idx += 7 * num_cns | |
| num_guides = len(guide_images) | |
| g_imgs = args[idx : idx + num_guides] | |
| g_vids = args[idx + num_guides : idx + 2*num_guides] | |
| g_auds = args[idx + 2*num_guides : idx + 3*num_guides] | |
| g_times = args[idx + 3*num_guides : idx + 4*num_guides] | |
| g_frames = args[idx + 4*num_guides : idx + 5*num_guides] if guide_frames else [] | |
| ui_values = { | |
| 'task': task, | |
| 'first_frame': first_frame, | |
| 'last_frame': last_frame, | |
| 'prompt': prompt, | |
| 'resolution': resolution, | |
| 'aspect_ratio': aspect_ratio, | |
| 'width': width, | |
| 'height': height, | |
| 'duration': duration, | |
| 'sampler_name': sampler, | |
| 'scheduler': scheduler, | |
| 'steps': steps, | |
| 'cfg_scale': cfg_scale, | |
| 'seed': seed, | |
| 'zero_gpu_duration': zero_gpu_duration, | |
| 'batch_count': 1, | |
| 'ref_images': imgs, | |
| 'ref_videos': vids, | |
| 'ref_audios': auds, | |
| f'lora_sources_{prefix}': list(l_sources), | |
| f'lora_ids_{prefix}': list(l_ids), | |
| f'lora_scales_{prefix}': list(l_scales), | |
| f'h3_controlnet_videos_{prefix}': list(c_vids), | |
| f'h3_controlnet_types_{prefix}': list(c_types), | |
| f'h3_controlnet_series_{prefix}': list(c_series), | |
| f'h3_controlnet_strengths_{prefix}': list(c_strengths), | |
| f'h3_controlnet_start_percents_{prefix}': list(c_starts), | |
| f'h3_controlnet_end_percents_{prefix}': list(c_ends), | |
| f'h3_controlnet_filepaths_{prefix}': list(c_fps), | |
| f'h3_guide_images_{prefix}': list(g_imgs), | |
| f'h3_guide_videos_{prefix}': list(g_vids), | |
| f'h3_guide_audios_{prefix}': list(g_auds), | |
| f'h3_guide_times_{prefix}': list(g_times), | |
| f'h3_guide_frames_{prefix}': list(g_frames), | |
| } | |
| return generate_h3_video_wrapper(process_inputs, ui_values, progress=progress) | |
| components['run_button'].click( | |
| fn=on_run_click, | |
| inputs=inputs_list, | |
| outputs=[components['output_video']], | |
| api_name=False | |
| ) | |