jbilcke-hf HF staff hysts HF staff commited on
Commit
8378c79
·
0 Parent(s):

Duplicate from hysts/zeroscope-v2

Browse files

Co-authored-by: hysts <hysts@users.noreply.huggingface.co>

Files changed (9) hide show
  1. .gitattributes +35 -0
  2. .pre-commit-config.yaml +36 -0
  3. .style.yapf +5 -0
  4. .vscode/settings.json +18 -0
  5. LICENSE +21 -0
  6. README.md +15 -0
  7. app.py +140 -0
  8. requirements.txt +9 -0
  9. style.css +16 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.pre-commit-config.yaml ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.2.0
4
+ hooks:
5
+ - id: check-executables-have-shebangs
6
+ - id: check-json
7
+ - id: check-merge-conflict
8
+ - id: check-shebang-scripts-are-executable
9
+ - id: check-toml
10
+ - id: check-yaml
11
+ - id: double-quote-string-fixer
12
+ - id: end-of-file-fixer
13
+ - id: mixed-line-ending
14
+ args: ['--fix=lf']
15
+ - id: requirements-txt-fixer
16
+ - id: trailing-whitespace
17
+ - repo: https://github.com/myint/docformatter
18
+ rev: v1.4
19
+ hooks:
20
+ - id: docformatter
21
+ args: ['--in-place']
22
+ - repo: https://github.com/pycqa/isort
23
+ rev: 5.12.0
24
+ hooks:
25
+ - id: isort
26
+ - repo: https://github.com/pre-commit/mirrors-mypy
27
+ rev: v0.991
28
+ hooks:
29
+ - id: mypy
30
+ args: ['--ignore-missing-imports']
31
+ additional_dependencies: ['types-python-slugify']
32
+ - repo: https://github.com/google/yapf
33
+ rev: v0.32.0
34
+ hooks:
35
+ - id: yapf
36
+ args: ['--parallel', '--in-place']
.style.yapf ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [style]
2
+ based_on_style = pep8
3
+ blank_line_before_nested_class_or_def = false
4
+ spaces_before_comment = 2
5
+ split_before_logical_operator = true
.vscode/settings.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "python.linting.enabled": true,
3
+ "python.linting.flake8Enabled": true,
4
+ "python.linting.pylintEnabled": false,
5
+ "python.linting.lintOnSave": true,
6
+ "python.formatting.provider": "yapf",
7
+ "python.formatting.yapfArgs": [
8
+ "--style={based_on_style: pep8, indent_width: 4, blank_line_before_nested_class_or_def: false, spaces_before_comment: 2, split_before_logical_operator: true}"
9
+ ],
10
+ "[python]": {
11
+ "editor.formatOnType": true,
12
+ "editor.codeActionsOnSave": {
13
+ "source.organizeImports": true
14
+ }
15
+ },
16
+ "editor.formatOnSave": true,
17
+ "files.insertFinalNewline": true
18
+ }
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 hysts
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Zeroscope V2
3
+ emoji: 🌖
4
+ colorFrom: gray
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 3.36.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ suggested_hardware: t4-small
12
+ duplicated_from: hysts/zeroscope-v2
13
+ ---
14
+
15
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 spaces
13
+ import torch
14
+ from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
15
+
16
+ DESCRIPTION = '# zeroscope v2'
17
+ if not torch.cuda.is_available():
18
+ DESCRIPTION += '\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>'
19
+
20
+ MAX_NUM_FRAMES = int(os.getenv('MAX_NUM_FRAMES', '200'))
21
+ DEFAULT_NUM_FRAMES = min(MAX_NUM_FRAMES,
22
+ int(os.getenv('DEFAULT_NUM_FRAMES', '24')))
23
+ MAX_SEED = np.iinfo(np.int32).max
24
+ CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv(
25
+ 'CACHE_EXAMPLES') == '1'
26
+
27
+ if torch.cuda.is_available():
28
+ pipe = DiffusionPipeline.from_pretrained('cerspense/zeroscope_v2_576w',
29
+ torch_dtype=torch.float16)
30
+ pipe.enable_model_cpu_offload()
31
+ else:
32
+ pipe = DiffusionPipeline.from_pretrained('cerspense/zeroscope_v2_576w')
33
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
34
+ pipe.enable_vae_slicing()
35
+
36
+
37
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
38
+ if randomize_seed:
39
+ seed = random.randint(0, MAX_SEED)
40
+ return seed
41
+
42
+
43
+ def to_video(frames: list[np.ndarray], fps: int) -> str:
44
+ out_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
45
+ writer = imageio.get_writer(out_file.name, format='FFMPEG', fps=fps)
46
+ for frame in frames:
47
+ writer.append_data(frame)
48
+ writer.close()
49
+ return out_file.name
50
+
51
+
52
+ @spaces.GPU
53
+ def generate(prompt: str, seed: int, num_frames: int,
54
+ num_inference_steps: int) -> str:
55
+ generator = torch.Generator().manual_seed(seed)
56
+ frames = pipe(prompt,
57
+ num_inference_steps=num_inference_steps,
58
+ num_frames=num_frames,
59
+ width=576,
60
+ height=320,
61
+ generator=generator).frames
62
+ return to_video(frames, 8)
63
+
64
+
65
+ examples = [
66
+ ['An astronaut riding a horse', 0, 24, 25],
67
+ ['A panda eating bamboo on a rock', 0, 24, 25],
68
+ ['Spiderman is surfing', 0, 24, 25],
69
+ ]
70
+
71
+ with gr.Blocks(css='style.css') as demo:
72
+ gr.Markdown(DESCRIPTION)
73
+ gr.DuplicateButton(value='Duplicate Space for private use',
74
+ elem_id='duplicate-button',
75
+ visible=os.getenv('SHOW_DUPLICATE_BUTTON') == '1')
76
+ with gr.Box():
77
+ with gr.Row():
78
+ prompt = gr.Text(label='Prompt',
79
+ show_label=False,
80
+ max_lines=1,
81
+ placeholder='Enter your prompt',
82
+ container=False)
83
+ run_button = gr.Button('Generate video', scale=0)
84
+ result = gr.Video(label='Result', show_label=False)
85
+ with gr.Accordion('Advanced options', open=False):
86
+ seed = gr.Slider(label='Seed',
87
+ minimum=0,
88
+ maximum=MAX_SEED,
89
+ step=1,
90
+ value=0)
91
+ randomize_seed = gr.Checkbox(label='Randomize seed', value=True)
92
+ num_frames = gr.Slider(
93
+ label='Number of frames',
94
+ minimum=24,
95
+ maximum=MAX_NUM_FRAMES,
96
+ step=1,
97
+ value=24,
98
+ info=
99
+ 'Note that the content of the video also changes when you change the number of frames.'
100
+ )
101
+ num_inference_steps = gr.Slider(label='Number of inference steps',
102
+ minimum=10,
103
+ maximum=50,
104
+ step=1,
105
+ value=25)
106
+
107
+ inputs = [
108
+ prompt,
109
+ seed,
110
+ num_frames,
111
+ num_inference_steps,
112
+ ]
113
+ gr.Examples(examples=examples,
114
+ inputs=inputs,
115
+ outputs=result,
116
+ fn=generate,
117
+ cache_examples=CACHE_EXAMPLES)
118
+
119
+ prompt.submit(
120
+ fn=randomize_seed_fn,
121
+ inputs=[seed, randomize_seed],
122
+ outputs=seed,
123
+ queue=False,
124
+ ).then(
125
+ fn=generate,
126
+ inputs=inputs,
127
+ outputs=result,
128
+ api_name='run',
129
+ )
130
+ run_button.click(
131
+ fn=randomize_seed_fn,
132
+ inputs=[seed, randomize_seed],
133
+ outputs=seed,
134
+ queue=False,
135
+ ).then(
136
+ fn=generate,
137
+ inputs=inputs,
138
+ outputs=result,
139
+ )
140
+ demo.queue(max_size=10).launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==0.20.3
2
+ diffusers==0.18.2
3
+ gradio==3.36.1
4
+ huggingface-hub==0.16.4
5
+ imageio[ffmpeg]==2.31.1
6
+ spaces==0.7.0
7
+ torch==2.0.0
8
+ torchvision==0.15.1
9
+ transformers==4.30.2
style.css ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }
4
+
5
+ #duplicate-button {
6
+ margin: auto;
7
+ color: #fff;
8
+ background: #1565c0;
9
+ border-radius: 100vh;
10
+ }
11
+
12
+ #component-0 {
13
+ max-width: 730px;
14
+ margin: auto;
15
+ padding-top: 1.5rem;
16
+ }