#!/usr/bin/env python from __future__ import annotations import os import random import tempfile import spaces import gradio as gr import torch from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler from diffusers.utils import export_to_video DESCRIPTION = '# ModelScope Text to Video Synthesis' DESCRIPTION += '\n
This model can only be used for non-commercial purposes. To learn more about the model, take a look at the model card.
' MAX_NUM_FRAMES = int(os.getenv('MAX_NUM_FRAMES', '100')) DEFAULT_NUM_FRAMES = min(MAX_NUM_FRAMES, int(os.getenv('DEFAULT_NUM_FRAMES', '24'))) pipe = DiffusionPipeline.from_pretrained('ali-vilab/text-to-video-ms-1.7b', torch_dtype=torch.float16, variant='fp16') pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) pipe = pipe.to('cuda') @spaces.GPU def generate(prompt: str, seed: int = 42, num_frames: int = DEFAULT_NUM_FRAMES, num_inference_steps: int = 25, progress=gr.Progress(track_tqdm=True)) -> str: if seed == -1: seed = random.randint(0, 1000000) generator = torch.Generator().manual_seed(seed) frames = pipe(prompt, num_inference_steps=num_inference_steps, num_frames=num_frames, generator=generator).frames[0] out_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) return export_to_video(frames, out_file.name, fps=8) examples = [ ['Will Smith eating spaghetti'], ['A panda eating bamboo on a rock'], ['Spiderman is surfing'], ] css = ''' h1 { text-align: center; } .gradio-container { max-width: 730px !important; margin: auto; } ''' with gr.Blocks() as demo: gr.Markdown(DESCRIPTION) with gr.Group(): with gr.Row(): prompt = gr.Text(label='Prompt', show_label=False, max_lines=1, placeholder='Enter your prompt', container=False, scale=4) run_button = gr.Button('Generate video', scale=1, variant='primary') result = gr.Video(label='Result', show_label=False, autoplay=True, loop=True) with gr.Accordion('Advanced options', open=False): seed = gr.Slider( label='Seed', minimum=-1, maximum=1000000, step=1, value=-1, info='If set to -1, a different seed will be used each time.') num_frames = gr.Slider( label='Number of frames', minimum=16, maximum=MAX_NUM_FRAMES, step=1, value=DEFAULT_NUM_FRAMES, info='Note that the content of the video also changes when you change the number of frames.') num_inference_steps = gr.Slider(label='Number of inference steps', minimum=10, maximum=50, step=1, value=25) inputs = [prompt, seed, num_frames, num_inference_steps] gr.Examples(examples=examples, inputs=prompt, outputs=result, fn=generate, cache_examples=True, cache_mode="lazy") prompt.submit(fn=generate, inputs=inputs, outputs=result) run_button.click(fn=generate, inputs=inputs, outputs=result) with gr.Accordion(label='Biases and content acknowledgment', open=False): gr.HTML("""Despite how impressive being able to turn text into video is, beware to the fact that this model may output content that reinforces or exacerbates societal biases. The training data includes LAION5B, ImageNet, Webvid and other public datasets. The model was not trained to realistically represent people or events, so using it to generate such content is beyond the model's capabilities.
It is not intended to generate content that is demeaning or harmful to people or their environment, culture, religion, etc. Similarly, it is not allowed to generate pornographic, violent and bloody content generation. The model is meant for research purposes.
To learn more about the model, head to its model card.