Spaces:
Running
on
Zero
Running
on
Zero
update
Browse files
gradio/controllers/gra_06_video/video.py
DELETED
@@ -1,60 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import torch
|
3 |
-
import random
|
4 |
-
import os
|
5 |
-
import tempfile
|
6 |
-
import imageio
|
7 |
-
import numpy as np
|
8 |
-
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
|
9 |
-
|
10 |
-
DESCRIPTION = '# [ModelScope Text to Video Synthesis](https://modelscope.cn/models/damo/text-to-video-synthesis/summary)'
|
11 |
-
|
12 |
-
MAX_NUM_FRAMES = int(os.getenv('MAX_NUM_FRAMES', '200'))
|
13 |
-
DEFAULT_NUM_FRAMES = min(MAX_NUM_FRAMES, int(os.getenv('DEFAULT_NUM_FRAMES', '16')))
|
14 |
-
|
15 |
-
pipe = DiffusionPipeline.from_pretrained(
|
16 |
-
'damo-vilab/text-to-video-ms-1.7b',
|
17 |
-
torch_dtype=torch.float16,
|
18 |
-
variant='fp16'
|
19 |
-
)
|
20 |
-
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
21 |
-
pipe.enable_model_cpu_offload()
|
22 |
-
pipe.enable_vae_slicing()
|
23 |
-
|
24 |
-
def to_video(frames: list[np.ndarray], fps: int) -> str:
|
25 |
-
out_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
|
26 |
-
writer = imageio.get_writer(out_file.name, format='FFMPEG', fps=fps)
|
27 |
-
for frame in frames:
|
28 |
-
writer.append_data(frame)
|
29 |
-
writer.close()
|
30 |
-
return out_file.name
|
31 |
-
|
32 |
-
def generate(prompt: str, seed: int, num_frames: int, num_inference_steps: int) -> str:
|
33 |
-
if seed == -1:
|
34 |
-
seed = random.randint(0, 1000000)
|
35 |
-
generator = torch.Generator().manual_seed(seed)
|
36 |
-
frames = pipe(prompt, num_inference_steps=num_inference_steps, num_frames=num_frames, generator=generator).frames
|
37 |
-
return to_video(frames, 8)
|
38 |
-
|
39 |
-
examples = [
|
40 |
-
['An astronaut riding a horse.', 0, 16, 25],
|
41 |
-
['A panda eating bamboo on a rock.', 0, 16, 25],
|
42 |
-
['Spiderman is surfing.', 0, 16, 25],
|
43 |
-
]
|
44 |
-
|
45 |
-
gradio_interface = gr.Interface(
|
46 |
-
fn=generate,
|
47 |
-
inputs=[
|
48 |
-
gr.Textbox(label="Prompt", placeholder="Enter your prompt"),
|
49 |
-
gr.Slider(label="Seed", minimum=-1, maximum=1000000, step=1, value=-1),
|
50 |
-
gr.Slider(label="Number of frames", minimum=16, maximum=MAX_NUM_FRAMES, step=1, value=16),
|
51 |
-
gr.Slider(label="Number of inference steps", minimum=10, maximum=50, step=1, value=25),
|
52 |
-
],
|
53 |
-
outputs=gr.Video(label="Generated Video"),
|
54 |
-
title="Text to Video Synthesis",
|
55 |
-
description=DESCRIPTION,
|
56 |
-
examples=examples
|
57 |
-
)
|
58 |
-
|
59 |
-
if __name__ == "__main__":
|
60 |
-
gradio_interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|