CoolFace
Apppublic

unicodeveloper/ltx-video-showcase

sourceHugging Faceupdated 1y agoView on Hugging Face
2likes
app.py171 linesDownload Raw Back to root
1import gradio as gr2import spaces3import torch4import numpy as np5import os6import random7from ltx_video.inference import infer, InferenceConfig8from functools import partial9import warnings10 11 12warnings.filterwarnings("ignore", category=FutureWarning)13 14@spaces.GPU15def create(16        prompt,17        negative_prompt="worst quality, inconsistent motion, blurry, jittery, distorted",18        input_image_filepath=None,19        input_video_filepath=None,20        height_ui=512,21        width_ui=704,22        duration_ui=2.0,23        ui_frames_to_use=16,24        seed_ui=42,25        randomize_seed=True,26        ui_guidance_scale=3.0,27        improve_texture_flag=True,28        fps=8, 29        progress=gr.Progress(track_tqdm=True),30        mode="text-to-video"31    ):32    """33    Generate videos using the LTX Video model.34    """35 36    # pick seed37    used_seed = seed_ui38 39    output_path = f"output_{mode}_{used_seed}.mp4"40 41    # Validate mode-specific required parameters42    if mode == "image-to-video":43        if not input_image_filepath:44            raise gr.Error(f"input_image_filepath, {input_image_filepath} is required for image-to-video mode")45    elif mode == "video-to-video":46        if not input_video_filepath:47            raise gr.Error(f"input_video_filepath, {input_video_filepath} is required for video-to-video mode")48    elif mode == "text-to-video":49        # No additional file inputs required for text-to-video50        pass51    else:52        raise gr.Error(f"Invalid mode: {mode}. Must be one of: text-to-video, image-to-video, video-to-video")53 54    config = InferenceConfig(55        pipeline_config="configs/ltxv-2b-0.9.6-dev.yaml",56        prompt=prompt,57        negative_prompt=negative_prompt,58        height=height_ui,59        width=width_ui,60        num_frames=ui_frames_to_use,61        seed=used_seed,62        output_path=output_path63    )64 65    # attach initial image or video if mode requires66    if mode == "image-to-video" and input_image_filepath:67        config.input_media_path = input_image_filepath68    elif mode == "video-to-video" and input_video_filepath:69        config.input_media_path = input_video_filepath70 71    # run inference72    infer(config)73 74    return output_path, f"โœ… Done! Seed: {used_seed}"75 76# ---- Gradio Blocks & UI ----77with gr.Blocks(title="AI Video Converter", theme=gr.themes.Soft()) as demo:78    gr.Markdown("# ๐ŸŽฌ AI Video Converter")79    gr.Markdown("Convert text, images, and videos into stunning AI-generated videos!")80    81    with gr.Tabs():82        # --- Text to Video ---83        with gr.Tab("๐Ÿ“ Text to Video"):84            gr.Markdown("### Generate videos from text descriptions")85            with gr.Row():86                with gr.Column():87                    text_prompt = gr.Textbox(88                        label="Text Prompt",89                        placeholder="Describe the video you want to create...",90                        value="A Nigerian woman dancing on the streets of Lagos, Nigeria",91                        lines=392                    )93                    text_num_frames = gr.Slider(minimum=8, maximum=32, value=16, step=1,label="Number of Frames")94                    text_fps = gr.Slider(minimum=4, maximum=30, value=8, step=1,label="Frames Per Second")95                    text_generate_video_btn = gr.Button("Generate Video", variant="primary")96                97                with gr.Column():98                    text_output_video = gr.Video(label="Generated Video")99                    text_status = gr.Textbox(label="Status", interactive=False)100        101        # --- Image to Video ---102        with gr.Tab("๐Ÿ–ผ๏ธ Image to Video"):103            gr.Markdown("### Animate images into videos")104            with gr.Row():105                with gr.Column():106                    image_input = gr.Image(label="Input Image",type="filepath", sources=["upload", "webcam", "clipboard"])107                    image_text_prompt = gr.Textbox(108                        label="Text Prompt",109                        placeholder="Describe the video you want to create...",110                        value="The creature from the image starts to move",111                        lines=3112                    )113                    image_num_frames = gr.Slider(minimum=8, maximum=50, value=25, step=1,label="Number of Frames")114                    image_fps = gr.Slider(minimum=4, maximum=30, value=8, step=1,label="Frames Per Second")115                    image_generate_video_btn = gr.Button("Generate Video", variant="primary")116                117                with gr.Column():118                    image_output_video = gr.Video(label="Generated Video")119                    image_status = gr.Textbox(label="Status", interactive=False)120        121        # --- Video to Video ---122        with gr.Tab("๐ŸŽฅ Video to Video"):123            gr.Markdown("### Transform videos with AI")124            with gr.Row():125                with gr.Column():126                    video_input = gr.Video(label="Input Video")127                    video_prompt = gr.Textbox(128                        label="Transformation Prompt",129                        placeholder="Describe how you want to transform the video...",130                        lines=3131                    )132                    video_strength = gr.Slider(minimum=0.1, maximum=1.0, value=0.8, step=0.1,label="Transformation Strength")133                    video_generate_video_btn = gr.Button("Transform Video", variant="primary")134                135                with gr.Column():136                    video_output_video = gr.Video(label="Transformed Video")137                    video_status = gr.Textbox(label="Status", interactive=False)138    139    140    # --- Inputs ---141    tgv_inputs = [text_prompt, gr.State(None), gr.State(None), text_num_frames, text_fps]142    igv_inputs = [image_text_prompt, image_input, gr.State(None), image_num_frames, image_fps]143    vgv_inputs = [video_prompt, gr.State(None), video_input, video_strength]144 145    # --- Outputs ---146    tgv_outputs = [text_output_video, text_status]147    igv_outputs = [image_output_video, image_status]148    vgv_outputs = [video_output_video, video_status]149    150 151    # --- Button Logic ---152    text_generate_video_btn.click(153        fn=partial(create, mode="text-to-video"),154        inputs=tgv_inputs,155        outputs=tgv_outputs156    )157    158    image_generate_video_btn.click(159        fn=partial(create, mode="image-to-video"),160        inputs=igv_inputs,161        outputs=igv_outputs162    )163    164    video_generate_video_btn.click(165        fn=partial(create, mode="video-to-video"),166        inputs=vgv_inputs,167        outputs=vgv_outputs168    )169 170if __name__ == "__main__":171    demo.launch(debug=True, share=False)