jbilcke-hf HF staff commited on
Commit
75ed479
0 Parent(s):

Duplicate from jbilcke-hf/zeroscope-server-1

Browse files
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 +127 -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.38.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ suggested_hardware: t4-small
12
+ duplicated_from: jbilcke-hf/zeroscope-server-1
13
+ ---
14
+
15
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
25
+ if torch.cuda.is_available():
26
+ pipe = DiffusionPipeline.from_pretrained('cerspense/zeroscope_v2_576w',
27
+ torch_dtype=torch.float16)
28
+ pipe.enable_model_cpu_offload()
29
+ else:
30
+ pipe = DiffusionPipeline.from_pretrained('cerspense/zeroscope_v2_576w')
31
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
32
+ pipe.enable_vae_slicing()
33
+
34
+
35
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
36
+ if randomize_seed:
37
+ seed = random.randint(0, MAX_SEED)
38
+ return seed
39
+
40
+
41
+ def to_video(frames: list[np.ndarray], fps: int) -> str:
42
+ out_file = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
43
+ writer = imageio.get_writer(out_file.name, format='FFMPEG', fps=fps)
44
+ for frame in frames:
45
+ writer.append_data(frame)
46
+ writer.close()
47
+ return out_file.name
48
+
49
+
50
+ @spaces.GPU
51
+ def generate(prompt: str, seed: int, num_frames: int,
52
+ num_inference_steps: int) -> str:
53
+ generator = torch.Generator().manual_seed(seed)
54
+ frames = pipe(prompt,
55
+ num_inference_steps=num_inference_steps,
56
+ num_frames=num_frames,
57
+ width=576,
58
+ height=320,
59
+ generator=generator).frames
60
+ return to_video(frames, 8)
61
+
62
+
63
+ with gr.Blocks(css='style.css') as demo:
64
+ gr.Markdown(DESCRIPTION)
65
+ gr.DuplicateButton(value='Duplicate Space for private use',
66
+ elem_id='duplicate-button',
67
+ visible=os.getenv('SHOW_DUPLICATE_BUTTON') == '1')
68
+ with gr.Box():
69
+ with gr.Row():
70
+ prompt = gr.Text(label='Prompt',
71
+ show_label=False,
72
+ max_lines=1,
73
+ placeholder='Enter your prompt',
74
+ container=False)
75
+ run_button = gr.Button('Generate video', scale=0)
76
+ result = gr.Video(label='Result', show_label=False)
77
+ with gr.Accordion('Advanced options', open=False):
78
+ seed = gr.Slider(label='Seed',
79
+ minimum=0,
80
+ maximum=MAX_SEED,
81
+ step=1,
82
+ value=0)
83
+ randomize_seed = gr.Checkbox(label='Randomize seed', value=True)
84
+ num_frames = gr.Slider(
85
+ label='Number of frames',
86
+ minimum=24,
87
+ maximum=MAX_NUM_FRAMES,
88
+ step=1,
89
+ value=24,
90
+ info=
91
+ 'Note that the content of the video also changes when you change the number of frames.'
92
+ )
93
+ num_inference_steps = gr.Slider(label='Number of inference steps',
94
+ minimum=10,
95
+ maximum=50,
96
+ step=1,
97
+ value=25)
98
+
99
+ inputs = [
100
+ prompt,
101
+ seed,
102
+ num_frames,
103
+ num_inference_steps,
104
+ ]
105
+
106
+ prompt.submit(
107
+ fn=randomize_seed_fn,
108
+ inputs=[seed, randomize_seed],
109
+ outputs=seed,
110
+ queue=False,
111
+ ).then(
112
+ fn=generate,
113
+ inputs=inputs,
114
+ outputs=result,
115
+ api_name='run',
116
+ )
117
+ run_button.click(
118
+ fn=randomize_seed_fn,
119
+ inputs=[seed, randomize_seed],
120
+ outputs=seed,
121
+ queue=False,
122
+ ).then(
123
+ fn=generate,
124
+ inputs=inputs,
125
+ outputs=result,
126
+ )
127
+ 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
+ }