kenken999 commited on
Commit
4155ecc
1 Parent(s): 5528d9e
controllers/gra_06_video/__init__.py ADDED
File without changes
controllers/gra_06_video/video.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import random
7
+ import tempfile
8
+
9
+ import gradio as gr
10
+ import imageio
11
+ import numpy as np
12
+ import torch
13
+ from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
14
+
15
+ DESCRIPTION = '# [ModelScope Text to Video Synthesis](https://modelscope.cn/models/damo/text-to-video-synthesis/summary)'
16
+ DESCRIPTION += '\n<p>For Colab usage, you can view <a href="https://colab.research.google.com/drive/1uW1ZqswkQ9Z9bp5Nbo5z59cAn7I0hE6R?usp=sharing" style="text-decoration: underline;" target="_blank">this webpage</a>.(the latest update on 2023.03.21)</p>'
17
+ DESCRIPTION += '\n<p>This model can only be used for non-commercial purposes. To learn more about the model, take a look at the <a href="https://huggingface.co/damo-vilab/modelscope-damo-text-to-video-synthesis" style="text-decoration: underline;" target="_blank">model card</a>.</p>'
18
+ if (SPACE_ID := os.getenv('SPACE_ID')) is not None:
19
+ DESCRIPTION += f'\n<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. <a href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>'
20
+
21
+ MAX_NUM_FRAMES = int(os.getenv('MAX_NUM_FRAMES', '200'))
22
+ DEFAULT_NUM_FRAMES = min(MAX_NUM_FRAMES,
23
+ int(os.getenv('DEFAULT_NUM_FRAMES', '16')))
24
+
25
+ pipe = DiffusionPipeline.from_pretrained('damo-vilab/text-to-video-ms-1.7b',
26
+ torch_dtype=torch.float16,
27
+ variant='fp16')
28
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
29
+ pipe.enable_model_cpu_offload()
30
+ pipe.enable_vae_slicing()
31
+
32
+
33
+ def to_video(frames: list[np.ndarray], fps: int) -> str:
34
+ out_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
35
+ writer = imageio.get_writer(out_file.name, format='FFMPEG', fps=fps)
36
+ for frame in frames:
37
+ writer.append_data(frame)
38
+ writer.close()
39
+ return out_file.name
40
+
41
+
42
+ def generate(prompt: str, seed: int, num_frames: int,
43
+ num_inference_steps: int) -> str:
44
+ if seed == -1:
45
+ seed = random.randint(0, 1000000)
46
+ generator = torch.Generator().manual_seed(seed)
47
+ frames = pipe(prompt,
48
+ num_inference_steps=num_inference_steps,
49
+ num_frames=num_frames,
50
+ generator=generator).frames
51
+ return to_video(frames, 8)
52
+
53
+
54
+ examples = [
55
+ ['An astronaut riding a horse.', 0, 16, 25],
56
+ ['A panda eating bamboo on a rock.', 0, 16, 25],
57
+ ['Spiderman is surfing.', 0, 16, 25],
58
+ ]
59
+
60
+ with gr.Blocks(css='style.css') as gradio_interface:
61
+ gr.Markdown(DESCRIPTION)
62
+ with gr.Group():
63
+ with gr.Box():
64
+ with gr.Row(elem_id='prompt-container').style(equal_height=True):
65
+ prompt = gr.Text(
66
+ label='Prompt',
67
+ show_label=False,
68
+ max_lines=1,
69
+ placeholder='Enter your prompt',
70
+ elem_id='prompt-text-input').style(container=False)
71
+ run_button = gr.Button('Generate video').style(
72
+ full_width=False)
73
+ result = gr.Video(label='Result', show_label=False, elem_id='gallery')
74
+ with gr.Accordion('Advanced options', open=False):
75
+ seed = gr.Slider(
76
+ label='Seed',
77
+ minimum=-1,
78
+ maximum=1000000,
79
+ step=1,
80
+ value=-1,
81
+ info='If set to -1, a different seed will be used each time.')
82
+ num_frames = gr.Slider(
83
+ label='Number of frames',
84
+ minimum=16,
85
+ maximum=MAX_NUM_FRAMES,
86
+ step=1,
87
+ value=16,
88
+ info=
89
+ 'Note that the content of the video also changes when you change the number of frames.'
90
+ )
91
+ num_inference_steps = gr.Slider(label='Number of inference steps',
92
+ minimum=10,
93
+ maximum=50,
94
+ step=1,
95
+ value=25)
96
+
97
+ inputs = [
98
+ prompt,
99
+ seed,
100
+ num_frames,
101
+ num_inference_steps,
102
+ ]
103
+ gr.Examples(examples=examples,
104
+ inputs=inputs,
105
+ outputs=result,
106
+ fn=generate,
107
+ cache_examples=os.getenv('SYSTEM') == 'spaces')
108
+
109
+ prompt.submit(fn=generate, inputs=inputs, outputs=result)
110
+ run_button.click(fn=generate, inputs=inputs, outputs=result)
111
+
112
+
113
+ with gr.Accordion(label='We are hiring(Based in Beijing / Hangzhou, China.)', open=False):
114
+ gr.HTML("""<div class="acknowledgments">
115
+ <p>
116
+ If you're looking for an exciting challenge and the opportunity to work with cutting-edge technologies in AIGC and large-scale pretraining, then we are the place for you. We are looking for talented, motivated and creative individuals to join our team. If you are interested, please send your CV to us.
117
+ </p>
118
+ <p>
119
+ <b>EMAIL: [email protected]</b>.
120
+ </p>
121
+ </div>
122
+ """)
123
+
124
+ with gr.Accordion(label='Biases and content acknowledgment', open=False):
125
+ gr.HTML("""<div class="acknowledgments">
126
+ <h4>Biases and content acknowledgment</h4>
127
+ <p>
128
+ 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.
129
+ </p>
130
+ <p>
131
+ 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. <b>The model is meant for research purposes</b>.
132
+ </p>
133
+ <p>
134
+ To learn more about the model, head to its <a href="https://huggingface.co/damo-vilab/modelscope-damo-text-to-video-synthesis" style="text-decoration: underline;" target="_blank">model card</a>.
135
+ </p>
136
+ </div>
137
+ """)
requirements.txt CHANGED
@@ -48,7 +48,7 @@ diffusers
48
  psycopg2-binary
49
  accelerate==0.17.1
50
  git+https://github.com/huggingface/diffusers@9dc8444
51
- gradio==3.23.0
52
  huggingface-hub==0.13.3
53
  imageio[ffmpeg]==2.26.1
54
  torch==2.0.0
 
48
  psycopg2-binary
49
  accelerate==0.17.1
50
  git+https://github.com/huggingface/diffusers@9dc8444
51
+ #gradio==3.23.0
52
  huggingface-hub==0.13.3
53
  imageio[ffmpeg]==2.26.1
54
  torch==2.0.0