lucaspedrajas hysts HF staff commited on
Commit
c3690c4
0 Parent(s):

Duplicate from DeepFloyd/IF

Browse files

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

Files changed (10) hide show
  1. .gitattributes +35 -0
  2. .gitignore +162 -0
  3. .pre-commit-config.yaml +38 -0
  4. .style.yapf +5 -0
  5. README.md +15 -0
  6. app.py +664 -0
  7. model.py +309 -0
  8. requirements.txt +16 -0
  9. settings.py +56 -0
  10. style.css +203 -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
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ *.png filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio_cached_examples/
2
+
3
+ # Byte-compiled / optimized / DLL files
4
+ __pycache__/
5
+ *.py[cod]
6
+ *$py.class
7
+
8
+ # C extensions
9
+ *.so
10
+
11
+ # Distribution / packaging
12
+ .Python
13
+ build/
14
+ develop-eggs/
15
+ dist/
16
+ downloads/
17
+ eggs/
18
+ .eggs/
19
+ lib/
20
+ lib64/
21
+ parts/
22
+ sdist/
23
+ var/
24
+ wheels/
25
+ share/python-wheels/
26
+ *.egg-info/
27
+ .installed.cfg
28
+ *.egg
29
+ MANIFEST
30
+
31
+ # PyInstaller
32
+ # Usually these files are written by a python script from a template
33
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
34
+ *.manifest
35
+ *.spec
36
+
37
+ # Installer logs
38
+ pip-log.txt
39
+ pip-delete-this-directory.txt
40
+
41
+ # Unit test / coverage reports
42
+ htmlcov/
43
+ .tox/
44
+ .nox/
45
+ .coverage
46
+ .coverage.*
47
+ .cache
48
+ nosetests.xml
49
+ coverage.xml
50
+ *.cover
51
+ *.py,cover
52
+ .hypothesis/
53
+ .pytest_cache/
54
+ cover/
55
+
56
+ # Translations
57
+ *.mo
58
+ *.pot
59
+
60
+ # Django stuff:
61
+ *.log
62
+ local_settings.py
63
+ db.sqlite3
64
+ db.sqlite3-journal
65
+
66
+ # Flask stuff:
67
+ instance/
68
+ .webassets-cache
69
+
70
+ # Scrapy stuff:
71
+ .scrapy
72
+
73
+ # Sphinx documentation
74
+ docs/_build/
75
+
76
+ # PyBuilder
77
+ .pybuilder/
78
+ target/
79
+
80
+ # Jupyter Notebook
81
+ .ipynb_checkpoints
82
+
83
+ # IPython
84
+ profile_default/
85
+ ipython_config.py
86
+
87
+ # pyenv
88
+ # For a library or package, you might want to ignore these files since the code is
89
+ # intended to run in multiple environments; otherwise, check them in:
90
+ # .python-version
91
+
92
+ # pipenv
93
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
94
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
95
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
96
+ # install all needed dependencies.
97
+ #Pipfile.lock
98
+
99
+ # poetry
100
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
101
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
102
+ # commonly ignored for libraries.
103
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
104
+ #poetry.lock
105
+
106
+ # pdm
107
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
108
+ #pdm.lock
109
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
110
+ # in version control.
111
+ # https://pdm.fming.dev/#use-with-ide
112
+ .pdm.toml
113
+
114
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115
+ __pypackages__/
116
+
117
+ # Celery stuff
118
+ celerybeat-schedule
119
+ celerybeat.pid
120
+
121
+ # SageMath parsed files
122
+ *.sage.py
123
+
124
+ # Environments
125
+ .env
126
+ .venv
127
+ env/
128
+ venv/
129
+ ENV/
130
+ env.bak/
131
+ venv.bak/
132
+
133
+ # Spyder project settings
134
+ .spyderproject
135
+ .spyproject
136
+
137
+ # Rope project settings
138
+ .ropeproject
139
+
140
+ # mkdocs documentation
141
+ /site
142
+
143
+ # mypy
144
+ .mypy_cache/
145
+ .dmypy.json
146
+ dmypy.json
147
+
148
+ # Pyre type checker
149
+ .pyre/
150
+
151
+ # pytype static type analyzer
152
+ .pytype/
153
+
154
+ # Cython debug symbols
155
+ cython_debug/
156
+
157
+ # PyCharm
158
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
161
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162
+ #.idea/
.pre-commit-config.yaml ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ exclude: diffusers-private
2
+ repos:
3
+ - repo: https://github.com/pre-commit/pre-commit-hooks
4
+ rev: v4.2.0
5
+ hooks:
6
+ - id: check-executables-have-shebangs
7
+ - id: check-json
8
+ - id: check-merge-conflict
9
+ - id: check-shebang-scripts-are-executable
10
+ - id: check-toml
11
+ - id: check-yaml
12
+ - id: double-quote-string-fixer
13
+ - id: end-of-file-fixer
14
+ - id: mixed-line-ending
15
+ args: ['--fix=lf']
16
+ - id: requirements-txt-fixer
17
+ - id: trailing-whitespace
18
+ - repo: https://github.com/myint/docformatter
19
+ rev: v1.4
20
+ hooks:
21
+ - id: docformatter
22
+ args: ['--in-place']
23
+ - repo: https://github.com/pycqa/isort
24
+ rev: 5.12.0
25
+ hooks:
26
+ - id: isort
27
+ - repo: https://github.com/pre-commit/mirrors-mypy
28
+ rev: v0.991
29
+ hooks:
30
+ - id: mypy
31
+ args: ['--ignore-missing-imports']
32
+ additional_dependencies: ['types-python-slugify']
33
+ files: ^diffusers-private
34
+ - repo: https://github.com/google/yapf
35
+ rev: v0.32.0
36
+ hooks:
37
+ - id: yapf
38
+ 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
README.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: IF
3
+ emoji: 🔥
4
+ colorFrom: pink
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 3.27.0
8
+ python_version: 3.10.11
9
+ app_file: app.py
10
+ pinned: false
11
+ license: other
12
+ duplicated_from: DeepFloyd/IF
13
+ ---
14
+
15
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,664 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ import datetime
4
+ import hashlib
5
+ import json
6
+ import os
7
+ import random
8
+ import tempfile
9
+
10
+ import gradio as gr
11
+ import torch
12
+ from huggingface_hub import HfApi
13
+
14
+ # isort: off
15
+ from model import Model
16
+ from settings import (
17
+ DEBUG,
18
+ DEFAULT_CUSTOM_TIMESTEPS_1,
19
+ DEFAULT_CUSTOM_TIMESTEPS_2,
20
+ DEFAULT_NUM_IMAGES,
21
+ DEFAULT_NUM_STEPS_3,
22
+ DISABLE_SD_X4_UPSCALER,
23
+ GALLERY_COLUMN_NUM,
24
+ HF_TOKEN,
25
+ MAX_NUM_IMAGES,
26
+ MAX_NUM_STEPS,
27
+ MAX_QUEUE_SIZE,
28
+ MAX_SEED,
29
+ SHOW_ADVANCED_OPTIONS,
30
+ SHOW_CUSTOM_TIMESTEPS_1,
31
+ SHOW_CUSTOM_TIMESTEPS_2,
32
+ SHOW_DEVICE_WARNING,
33
+ SHOW_DUPLICATE_BUTTON,
34
+ SHOW_NUM_IMAGES,
35
+ SHOW_NUM_STEPS_1,
36
+ SHOW_NUM_STEPS_2,
37
+ SHOW_NUM_STEPS_3,
38
+ SHOW_UPSCALE_TO_256_BUTTON,
39
+ UPLOAD_REPO_ID,
40
+ UPLOAD_RESULT_IMAGE,
41
+ )
42
+ # isort: on
43
+
44
+ TITLE = '# [DeepFloyd IF](https://github.com/deep-floyd/IF)'
45
+ DESCRIPTION = 'The DeepFloyd IF model has been initially released as a non-commercial research-only model. Please make sure you read and abide to the [LICENSE](https://huggingface.co/spaces/DeepFloyd/deepfloyd-if-license) before using it.'
46
+ DISCLAIMER = 'In this demo, the DeepFloyd team may collect prompts, and user preferences (which of the images the user chose to upscale) for improving future models'
47
+ FOOTER = """<div class="footer">
48
+ <p>Model by <a href="https://huggingface.co/DeepFloyd" style="text-decoration: underline;" target="_blank">DeepFloyd</a> supported by <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">Stability AI</a>
49
+ </p>
50
+ </div>
51
+ <div class="acknowledgments">
52
+ <p><h4>LICENSE</h4>
53
+ The model is licensed with a bespoke non-commercial research-only license <a href="https://huggingface.co/spaces/DeepFloyd/deepfloyd-if-license" style="text-decoration: underline;" target="_blank">DeepFloyd IF Research License Agreement</a> license. The license forbids you from sharing any content for commercial use, or that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/DeepFloyd/deepfloyd-if-license" style="text-decoration: underline;" target="_blank">read the license</a></p>
54
+ <p><h4>Biases and content acknowledgment</h4>
55
+ Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, explicit content and violence. The model was trained on a subset of the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a> and is meant for research purposes. You can read more in the <a href="https://huggingface.co/DeepFloyd/IF-I-IF-v1.0" style="text-decoration: underline;" target="_blank">model card</a></p>
56
+ </div>
57
+ """
58
+ if SHOW_DUPLICATE_BUTTON:
59
+ SPACE_ID = os.getenv('SPACE_ID')
60
+ DESCRIPTION += f'\n<p><a href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space%20to%20skip%20the%20queue-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></p>'
61
+
62
+ if SHOW_DEVICE_WARNING and not torch.cuda.is_available():
63
+ DESCRIPTION += '\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>'
64
+
65
+ model = Model()
66
+
67
+
68
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
69
+ if randomize_seed:
70
+ seed = random.randint(0, MAX_SEED)
71
+ return seed
72
+
73
+
74
+ def get_stage2_index(evt: gr.SelectData) -> int:
75
+ return evt.index
76
+
77
+
78
+ def check_if_stage2_selected(index: int) -> None:
79
+ if index == -1:
80
+ raise gr.Error(
81
+ 'You need to select the image you would like to upscale from the Stage 1 results by clicking.'
82
+ )
83
+
84
+
85
+ hf_api = HfApi(token=HF_TOKEN)
86
+ if UPLOAD_REPO_ID:
87
+ hf_api.create_repo(repo_id=UPLOAD_REPO_ID,
88
+ private=True,
89
+ repo_type='dataset',
90
+ exist_ok=True)
91
+
92
+
93
+ def get_param_file_hash_name(param_filepath: str) -> str:
94
+ if not UPLOAD_REPO_ID:
95
+ return ''
96
+ with open(param_filepath, 'rb') as f:
97
+ md5 = hashlib.md5(f.read()).hexdigest()
98
+ utcnow = datetime.datetime.utcnow().strftime('%Y-%m-%d-%H-%M-%S-%f')
99
+ return f'{utcnow}-{md5}'
100
+
101
+
102
+ def upload_stage1_result(stage1_param_path: str, stage1_result_path: str,
103
+ save_name: str) -> None:
104
+ if not UPLOAD_REPO_ID:
105
+ return
106
+ try:
107
+ hf_api.upload_file(path_or_fileobj=stage1_param_path,
108
+ path_in_repo=f'stage1_params/{save_name}.json',
109
+ repo_id=UPLOAD_REPO_ID,
110
+ repo_type='dataset')
111
+ hf_api.upload_file(path_or_fileobj=stage1_result_path,
112
+ path_in_repo=f'stage1_results/{save_name}.pth',
113
+ repo_id=UPLOAD_REPO_ID,
114
+ repo_type='dataset')
115
+ except Exception as e:
116
+ print(e)
117
+
118
+
119
+ def upload_stage2_info(stage1_param_file_hash_name: str,
120
+ stage2_output_path: str,
121
+ selected_index_for_upscale: int, seed_2: int,
122
+ guidance_scale_2: float, custom_timesteps_2: str,
123
+ num_inference_steps_2: int) -> None:
124
+ if not UPLOAD_REPO_ID:
125
+ return
126
+ if not stage1_param_file_hash_name:
127
+ raise ValueError
128
+
129
+ stage2_params = {
130
+ 'stage1_param_file_hash_name': stage1_param_file_hash_name,
131
+ 'selected_index_for_upscale': selected_index_for_upscale,
132
+ 'seed_2': seed_2,
133
+ 'guidance_scale_2': guidance_scale_2,
134
+ 'custom_timesteps_2': custom_timesteps_2,
135
+ 'num_inference_steps_2': num_inference_steps_2,
136
+ }
137
+ with tempfile.NamedTemporaryFile(mode='w', delete=False) as param_file:
138
+ param_file.write(json.dumps(stage2_params))
139
+ stage2_param_file_hash_name = get_param_file_hash_name(param_file.name)
140
+ save_name = f'{stage1_param_file_hash_name}_{stage2_param_file_hash_name}'
141
+
142
+ try:
143
+ hf_api.upload_file(path_or_fileobj=param_file.name,
144
+ path_in_repo=f'stage2_params/{save_name}.json',
145
+ repo_id=UPLOAD_REPO_ID,
146
+ repo_type='dataset')
147
+ if UPLOAD_RESULT_IMAGE:
148
+ hf_api.upload_file(path_or_fileobj=stage2_output_path,
149
+ path_in_repo=f'stage2_results/{save_name}.png',
150
+ repo_id=UPLOAD_REPO_ID,
151
+ repo_type='dataset')
152
+ except Exception as e:
153
+ print(e)
154
+
155
+
156
+ def upload_stage2_3_info(stage1_param_file_hash_name: str,
157
+ stage2_3_output_path: str,
158
+ selected_index_for_upscale: int, seed_2: int,
159
+ guidance_scale_2: float, custom_timesteps_2: str,
160
+ num_inference_steps_2: int, prompt: str,
161
+ negative_prompt: str, seed_3: int,
162
+ guidance_scale_3: float,
163
+ num_inference_steps_3: int) -> None:
164
+ if not UPLOAD_REPO_ID:
165
+ return
166
+ if not stage1_param_file_hash_name:
167
+ raise ValueError
168
+
169
+ stage2_3_params = {
170
+ 'stage1_param_file_hash_name': stage1_param_file_hash_name,
171
+ 'selected_index_for_upscale': selected_index_for_upscale,
172
+ 'seed_2': seed_2,
173
+ 'guidance_scale_2': guidance_scale_2,
174
+ 'custom_timesteps_2': custom_timesteps_2,
175
+ 'num_inference_steps_2': num_inference_steps_2,
176
+ 'prompt': prompt,
177
+ 'negative_prompt': negative_prompt,
178
+ 'seed_3': seed_3,
179
+ 'guidance_scale_3': guidance_scale_3,
180
+ 'num_inference_steps_3': num_inference_steps_3,
181
+ }
182
+ with tempfile.NamedTemporaryFile(mode='w', delete=False) as param_file:
183
+ param_file.write(json.dumps(stage2_3_params))
184
+ stage2_3_param_file_hash_name = get_param_file_hash_name(param_file.name)
185
+ save_name = f'{stage1_param_file_hash_name}_{stage2_3_param_file_hash_name}'
186
+
187
+ try:
188
+ hf_api.upload_file(path_or_fileobj=param_file.name,
189
+ path_in_repo=f'stage2_3_params/{save_name}.json',
190
+ repo_id=UPLOAD_REPO_ID,
191
+ repo_type='dataset')
192
+ if UPLOAD_RESULT_IMAGE:
193
+ hf_api.upload_file(
194
+ path_or_fileobj=stage2_3_output_path,
195
+ path_in_repo=f'stage2_3_results/{save_name}.png',
196
+ repo_id=UPLOAD_REPO_ID,
197
+ repo_type='dataset')
198
+ except Exception as e:
199
+ print(e)
200
+
201
+
202
+ def update_upscale_button(selected_index: int) -> tuple[dict, dict]:
203
+ if selected_index == -1:
204
+ return gr.update(interactive=False), gr.update(interactive=False)
205
+ else:
206
+ return gr.update(interactive=True), gr.update(interactive=True)
207
+
208
+
209
+ def _update_result_view(show_gallery: bool) -> tuple[dict, dict]:
210
+ return gr.update(visible=show_gallery), gr.update(visible=not show_gallery)
211
+
212
+
213
+ def show_gallery_view() -> tuple[dict, dict]:
214
+ return _update_result_view(True)
215
+
216
+
217
+ def show_upscaled_view() -> tuple[dict, dict]:
218
+ return _update_result_view(False)
219
+
220
+
221
+ examples = [
222
+ 'high quality dslr photo, a photo product of a lemon inspired by natural and organic materials, wooden accents, intricately decorated with glowing vines of led lights, inspired by baroque luxury',
223
+ 'paper quilling, extremely detailed, paper quilling of a nordic mountain landscape, 8k rendering',
224
+ 'letters made of candy on a plate that says "diet"',
225
+ 'a photo of a violet baseball cap with yellow text: "deep floyd". 50mm lens, photo realism, cine lens. violet baseball cap says "deep floyd". reflections, render. yellow stitch text "deep floyd"',
226
+ 'ultra close-up color photo portrait of rainbow owl with deer horns in the woods',
227
+ 'a cloth embroidered with the text "laion" and an embroidered cute baby lion face',
228
+ 'product image of a crochet Cthulhu the great old one emerging from a spacetime wormhole made of wool',
229
+ 'a little green budgie parrot driving small red toy car in new york street, photo',
230
+ 'origami dancer in white paper, 3d render, ultra-detailed, on white background, studio shot.',
231
+ 'glowing mushrooms in a natural environment with smoke in the frame',
232
+ 'a subway train\'s digital sign saying "open source", vsco preset, 35mm photo, film grain, in a dim subway station',
233
+ 'a bowl full of few adorable golden doodle puppies, the doodles dusted in powdered sugar and look delicious, bokeh, cannon. professional macro photo, super detailed. cute sweet golden doodle confectionery, baking puppies in powdered sugar in the bowl',
234
+ 'a face of a woman made completely out of foliage, twigs, leaves and flowers, side view'
235
+ ]
236
+
237
+ with gr.Blocks(css='style.css') as demo:
238
+ gr.Markdown(TITLE)
239
+ gr.Markdown(DESCRIPTION)
240
+ with gr.Box():
241
+ with gr.Row(elem_id='prompt-container').style(equal_height=True):
242
+ with gr.Column():
243
+ prompt = gr.Text(
244
+ label='Prompt',
245
+ show_label=False,
246
+ max_lines=1,
247
+ placeholder='Enter your prompt',
248
+ elem_id='prompt-text-input',
249
+ ).style(container=False)
250
+ negative_prompt = gr.Text(
251
+ label='Negative prompt',
252
+ show_label=False,
253
+ max_lines=1,
254
+ placeholder='Enter a negative prompt',
255
+ elem_id='negative-prompt-text-input',
256
+ ).style(container=False)
257
+ generate_button = gr.Button('Generate').style(full_width=False)
258
+
259
+ with gr.Column() as gallery_view:
260
+ gallery = gr.Gallery(label='Stage 1 results',
261
+ show_label=False,
262
+ elem_id='gallery').style(
263
+ columns=GALLERY_COLUMN_NUM,
264
+ object_fit='contain')
265
+ gr.Markdown('Pick your favorite generation to upscale.')
266
+ with gr.Row():
267
+ upscale_to_256_button = gr.Button(
268
+ 'Upscale to 256px',
269
+ visible=SHOW_UPSCALE_TO_256_BUTTON
270
+ or DISABLE_SD_X4_UPSCALER,
271
+ interactive=False)
272
+ upscale_button = gr.Button('Upscale',
273
+ interactive=False,
274
+ visible=not DISABLE_SD_X4_UPSCALER)
275
+ with gr.Column(visible=False) as upscale_view:
276
+ result = gr.Image(label='Result',
277
+ show_label=False,
278
+ type='filepath',
279
+ interactive=False,
280
+ elem_id='upscaled-image').style(height=640)
281
+ back_to_selection_button = gr.Button('Back to selection')
282
+
283
+ with gr.Accordion('Advanced options',
284
+ open=False,
285
+ visible=SHOW_ADVANCED_OPTIONS):
286
+ with gr.Tabs():
287
+ with gr.Tab(label='Generation'):
288
+ seed_1 = gr.Slider(label='Seed',
289
+ minimum=0,
290
+ maximum=MAX_SEED,
291
+ step=1,
292
+ value=0)
293
+ randomize_seed_1 = gr.Checkbox(label='Randomize seed',
294
+ value=True)
295
+ guidance_scale_1 = gr.Slider(label='Guidance scale',
296
+ minimum=1,
297
+ maximum=20,
298
+ step=0.1,
299
+ value=7.0)
300
+ custom_timesteps_1 = gr.Dropdown(
301
+ label='Custom timesteps 1',
302
+ choices=[
303
+ 'none',
304
+ 'fast27',
305
+ 'smart27',
306
+ 'smart50',
307
+ 'smart100',
308
+ 'smart185',
309
+ ],
310
+ value=DEFAULT_CUSTOM_TIMESTEPS_1,
311
+ visible=SHOW_CUSTOM_TIMESTEPS_1)
312
+ num_inference_steps_1 = gr.Slider(
313
+ label='Number of inference steps',
314
+ minimum=1,
315
+ maximum=MAX_NUM_STEPS,
316
+ step=1,
317
+ value=100,
318
+ visible=SHOW_NUM_STEPS_1)
319
+ num_images = gr.Slider(label='Number of images',
320
+ minimum=1,
321
+ maximum=MAX_NUM_IMAGES,
322
+ step=1,
323
+ value=DEFAULT_NUM_IMAGES,
324
+ visible=SHOW_NUM_IMAGES)
325
+ with gr.Tab(label='Super-resolution 1'):
326
+ seed_2 = gr.Slider(label='Seed',
327
+ minimum=0,
328
+ maximum=MAX_SEED,
329
+ step=1,
330
+ value=0)
331
+ randomize_seed_2 = gr.Checkbox(label='Randomize seed',
332
+ value=True)
333
+ guidance_scale_2 = gr.Slider(label='Guidance scale',
334
+ minimum=1,
335
+ maximum=20,
336
+ step=0.1,
337
+ value=4.0)
338
+ custom_timesteps_2 = gr.Dropdown(
339
+ label='Custom timesteps 2',
340
+ choices=[
341
+ 'none',
342
+ 'fast27',
343
+ 'smart27',
344
+ 'smart50',
345
+ 'smart100',
346
+ 'smart185',
347
+ ],
348
+ value=DEFAULT_CUSTOM_TIMESTEPS_2,
349
+ visible=SHOW_CUSTOM_TIMESTEPS_2)
350
+ num_inference_steps_2 = gr.Slider(
351
+ label='Number of inference steps',
352
+ minimum=1,
353
+ maximum=MAX_NUM_STEPS,
354
+ step=1,
355
+ value=50,
356
+ visible=SHOW_NUM_STEPS_2)
357
+ with gr.Tab(label='Super-resolution 2'):
358
+ seed_3 = gr.Slider(label='Seed',
359
+ minimum=0,
360
+ maximum=MAX_SEED,
361
+ step=1,
362
+ value=0)
363
+ randomize_seed_3 = gr.Checkbox(label='Randomize seed',
364
+ value=True)
365
+ guidance_scale_3 = gr.Slider(label='Guidance scale',
366
+ minimum=1,
367
+ maximum=20,
368
+ step=0.1,
369
+ value=9.0)
370
+ num_inference_steps_3 = gr.Slider(
371
+ label='Number of inference steps',
372
+ minimum=1,
373
+ maximum=MAX_NUM_STEPS,
374
+ step=1,
375
+ value=DEFAULT_NUM_STEPS_3,
376
+ visible=SHOW_NUM_STEPS_3)
377
+
378
+ gr.Examples(examples=examples, inputs=prompt, examples_per_page=4)
379
+
380
+ with gr.Box(visible=DEBUG):
381
+ with gr.Row():
382
+ with gr.Accordion(label='Hidden params'):
383
+ stage1_param_path = gr.Text(label='Stage 1 param path')
384
+ stage1_result_path = gr.Text(label='Stage 1 result path')
385
+ stage1_param_file_hash_name = gr.Text(
386
+ label='Stage 1 param file hash name')
387
+ selected_index_for_stage2 = gr.Number(
388
+ label='Selected index for Stage 2', value=-1, precision=0)
389
+ gr.Markdown(DISCLAIMER)
390
+ gr.HTML(FOOTER)
391
+ stage1_inputs = [
392
+ prompt,
393
+ negative_prompt,
394
+ seed_1,
395
+ num_images,
396
+ guidance_scale_1,
397
+ custom_timesteps_1,
398
+ num_inference_steps_1,
399
+ ]
400
+ stage1_outputs = [
401
+ gallery,
402
+ stage1_param_path,
403
+ stage1_result_path,
404
+ ]
405
+
406
+ prompt.submit(
407
+ fn=randomize_seed_fn,
408
+ inputs=[seed_1, randomize_seed_1],
409
+ outputs=seed_1,
410
+ queue=False,
411
+ ).then(
412
+ fn=lambda: -1,
413
+ outputs=selected_index_for_stage2,
414
+ queue=False,
415
+ ).then(
416
+ fn=show_gallery_view,
417
+ outputs=[
418
+ gallery_view,
419
+ upscale_view,
420
+ ],
421
+ queue=False,
422
+ ).then(
423
+ fn=update_upscale_button,
424
+ inputs=selected_index_for_stage2,
425
+ outputs=[
426
+ upscale_button,
427
+ upscale_to_256_button,
428
+ ],
429
+ queue=False,
430
+ ).then(
431
+ fn=model.run_stage1,
432
+ inputs=stage1_inputs,
433
+ outputs=stage1_outputs,
434
+ ).success(
435
+ fn=get_param_file_hash_name,
436
+ inputs=stage1_param_path,
437
+ outputs=stage1_param_file_hash_name,
438
+ queue=False,
439
+ ).then(
440
+ fn=upload_stage1_result,
441
+ inputs=[
442
+ stage1_param_path,
443
+ stage1_result_path,
444
+ stage1_param_file_hash_name,
445
+ ],
446
+ queue=False,
447
+ )
448
+
449
+ negative_prompt.submit(
450
+ fn=randomize_seed_fn,
451
+ inputs=[seed_1, randomize_seed_1],
452
+ outputs=seed_1,
453
+ queue=False,
454
+ ).then(
455
+ fn=lambda: -1,
456
+ outputs=selected_index_for_stage2,
457
+ queue=False,
458
+ ).then(
459
+ fn=show_gallery_view,
460
+ outputs=[
461
+ gallery_view,
462
+ upscale_view,
463
+ ],
464
+ queue=False,
465
+ ).then(
466
+ fn=update_upscale_button,
467
+ inputs=selected_index_for_stage2,
468
+ outputs=[
469
+ upscale_button,
470
+ upscale_to_256_button,
471
+ ],
472
+ queue=False,
473
+ ).then(
474
+ fn=model.run_stage1,
475
+ inputs=stage1_inputs,
476
+ outputs=stage1_outputs,
477
+ ).success(
478
+ fn=get_param_file_hash_name,
479
+ inputs=stage1_param_path,
480
+ outputs=stage1_param_file_hash_name,
481
+ queue=False,
482
+ ).then(
483
+ fn=upload_stage1_result,
484
+ inputs=[
485
+ stage1_param_path,
486
+ stage1_result_path,
487
+ stage1_param_file_hash_name,
488
+ ],
489
+ queue=False,
490
+ )
491
+
492
+ generate_button.click(
493
+ fn=randomize_seed_fn,
494
+ inputs=[seed_1, randomize_seed_1],
495
+ outputs=seed_1,
496
+ queue=False,
497
+ ).then(
498
+ fn=lambda: -1,
499
+ outputs=selected_index_for_stage2,
500
+ queue=False,
501
+ ).then(
502
+ fn=show_gallery_view,
503
+ outputs=[
504
+ gallery_view,
505
+ upscale_view,
506
+ ],
507
+ queue=False,
508
+ ).then(
509
+ fn=update_upscale_button,
510
+ inputs=selected_index_for_stage2,
511
+ outputs=[
512
+ upscale_button,
513
+ upscale_to_256_button,
514
+ ],
515
+ queue=False,
516
+ ).then(
517
+ fn=model.run_stage1,
518
+ inputs=stage1_inputs,
519
+ outputs=stage1_outputs,
520
+ api_name='generate64',
521
+ ).success(
522
+ fn=get_param_file_hash_name,
523
+ inputs=stage1_param_path,
524
+ outputs=stage1_param_file_hash_name,
525
+ queue=False,
526
+ ).then(
527
+ fn=upload_stage1_result,
528
+ inputs=[
529
+ stage1_param_path,
530
+ stage1_result_path,
531
+ stage1_param_file_hash_name,
532
+ ],
533
+ queue=False,
534
+ )
535
+
536
+ gallery.select(
537
+ fn=get_stage2_index,
538
+ outputs=selected_index_for_stage2,
539
+ queue=False,
540
+ )
541
+
542
+ selected_index_for_stage2.change(
543
+ fn=update_upscale_button,
544
+ inputs=selected_index_for_stage2,
545
+ outputs=[
546
+ upscale_button,
547
+ upscale_to_256_button,
548
+ ],
549
+ queue=False,
550
+ )
551
+
552
+ stage2_inputs = [
553
+ stage1_result_path,
554
+ selected_index_for_stage2,
555
+ seed_2,
556
+ guidance_scale_2,
557
+ custom_timesteps_2,
558
+ num_inference_steps_2,
559
+ ]
560
+
561
+ upscale_to_256_button.click(
562
+ fn=check_if_stage2_selected,
563
+ inputs=selected_index_for_stage2,
564
+ queue=False,
565
+ ).then(
566
+ fn=randomize_seed_fn,
567
+ inputs=[seed_2, randomize_seed_2],
568
+ outputs=seed_2,
569
+ queue=False,
570
+ ).then(
571
+ fn=show_upscaled_view,
572
+ outputs=[
573
+ gallery_view,
574
+ upscale_view,
575
+ ],
576
+ queue=False,
577
+ ).then(
578
+ fn=model.run_stage2,
579
+ inputs=stage2_inputs,
580
+ outputs=result,
581
+ api_name='upscale256',
582
+ ).success(
583
+ fn=upload_stage2_info,
584
+ inputs=[
585
+ stage1_param_file_hash_name,
586
+ result,
587
+ selected_index_for_stage2,
588
+ seed_2,
589
+ guidance_scale_2,
590
+ custom_timesteps_2,
591
+ num_inference_steps_2,
592
+ ],
593
+ queue=False,
594
+ )
595
+
596
+ stage2_3_inputs = [
597
+ stage1_result_path,
598
+ selected_index_for_stage2,
599
+ seed_2,
600
+ guidance_scale_2,
601
+ custom_timesteps_2,
602
+ num_inference_steps_2,
603
+ prompt,
604
+ negative_prompt,
605
+ seed_3,
606
+ guidance_scale_3,
607
+ num_inference_steps_3,
608
+ ]
609
+
610
+ upscale_button.click(
611
+ fn=check_if_stage2_selected,
612
+ inputs=selected_index_for_stage2,
613
+ queue=False,
614
+ ).then(
615
+ fn=randomize_seed_fn,
616
+ inputs=[seed_2, randomize_seed_2],
617
+ outputs=seed_2,
618
+ queue=False,
619
+ ).then(
620
+ fn=randomize_seed_fn,
621
+ inputs=[seed_3, randomize_seed_3],
622
+ outputs=seed_3,
623
+ queue=False,
624
+ ).then(
625
+ fn=show_upscaled_view,
626
+ outputs=[
627
+ gallery_view,
628
+ upscale_view,
629
+ ],
630
+ queue=False,
631
+ ).then(
632
+ fn=model.run_stage2_3,
633
+ inputs=stage2_3_inputs,
634
+ outputs=result,
635
+ api_name='upscale1024',
636
+ ).success(
637
+ fn=upload_stage2_3_info,
638
+ inputs=[
639
+ stage1_param_file_hash_name,
640
+ result,
641
+ selected_index_for_stage2,
642
+ seed_2,
643
+ guidance_scale_2,
644
+ custom_timesteps_2,
645
+ num_inference_steps_2,
646
+ prompt,
647
+ negative_prompt,
648
+ seed_3,
649
+ guidance_scale_3,
650
+ num_inference_steps_3,
651
+ ],
652
+ queue=False,
653
+ )
654
+
655
+ back_to_selection_button.click(
656
+ fn=show_gallery_view,
657
+ outputs=[
658
+ gallery_view,
659
+ upscale_view,
660
+ ],
661
+ queue=False,
662
+ )
663
+
664
+ demo.queue(api_open=False, max_size=MAX_QUEUE_SIZE).launch(debug=DEBUG)
model.py ADDED
@@ -0,0 +1,309 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import gc
4
+ import json
5
+ import tempfile
6
+ from typing import Generator
7
+
8
+ import numpy as np
9
+ import PIL.Image
10
+ import torch
11
+ from diffusers import DiffusionPipeline, StableDiffusionUpscalePipeline
12
+ from diffusers.pipelines.deepfloyd_if import (fast27_timesteps,
13
+ smart27_timesteps,
14
+ smart50_timesteps,
15
+ smart100_timesteps,
16
+ smart185_timesteps)
17
+
18
+ from settings import (DISABLE_AUTOMATIC_CPU_OFFLOAD, DISABLE_SD_X4_UPSCALER,
19
+ HF_TOKEN, MAX_NUM_IMAGES, MAX_NUM_STEPS, MAX_SEED,
20
+ RUN_GARBAGE_COLLECTION)
21
+
22
+
23
+ class Model:
24
+ def __init__(self):
25
+ self.device = torch.device(
26
+ 'cuda:0' if torch.cuda.is_available() else 'cpu')
27
+ self.pipe = None
28
+ self.super_res_1_pipe = None
29
+ self.super_res_2_pipe = None
30
+ self.watermark_image = None
31
+
32
+ if torch.cuda.is_available():
33
+ self.load_weights()
34
+ self.watermark_image = PIL.Image.fromarray(
35
+ self.pipe.watermarker.watermark_image.to(
36
+ torch.uint8).cpu().numpy(),
37
+ mode='RGBA')
38
+
39
+ def load_weights(self) -> None:
40
+ self.pipe = DiffusionPipeline.from_pretrained(
41
+ 'DeepFloyd/IF-I-XL-v1.0',
42
+ torch_dtype=torch.float16,
43
+ variant='fp16',
44
+ use_safetensors=True,
45
+ use_auth_token=HF_TOKEN)
46
+ self.super_res_1_pipe = DiffusionPipeline.from_pretrained(
47
+ 'DeepFloyd/IF-II-L-v1.0',
48
+ text_encoder=None,
49
+ torch_dtype=torch.float16,
50
+ variant='fp16',
51
+ use_safetensors=True,
52
+ use_auth_token=HF_TOKEN)
53
+
54
+ if not DISABLE_SD_X4_UPSCALER:
55
+ self.super_res_2_pipe = StableDiffusionUpscalePipeline.from_pretrained(
56
+ 'stabilityai/stable-diffusion-x4-upscaler',
57
+ torch_dtype=torch.float16)
58
+
59
+ if DISABLE_AUTOMATIC_CPU_OFFLOAD:
60
+ self.pipe.to(self.device)
61
+ self.super_res_1_pipe.to(self.device)
62
+ if not DISABLE_SD_X4_UPSCALER:
63
+ self.super_res_2_pipe.to(self.device)
64
+ else:
65
+ self.pipe.enable_model_cpu_offload()
66
+ self.super_res_1_pipe.enable_model_cpu_offload()
67
+ if not DISABLE_SD_X4_UPSCALER:
68
+ self.super_res_2_pipe.enable_model_cpu_offload()
69
+
70
+ def apply_watermark_to_sd_x4_upscaler_results(
71
+ self, images: list[PIL.Image.Image]) -> None:
72
+ w, h = images[0].size
73
+
74
+ stability_x4_upscaler_sample_size = 128
75
+
76
+ coef = min(h / stability_x4_upscaler_sample_size,
77
+ w / stability_x4_upscaler_sample_size)
78
+ img_h, img_w = (int(h / coef), int(w / coef)) if coef < 1 else (h, w)
79
+
80
+ S1, S2 = 1024**2, img_w * img_h
81
+ K = (S2 / S1)**0.5
82
+ watermark_size = int(K * 62)
83
+ watermark_x = img_w - int(14 * K)
84
+ watermark_y = img_h - int(14 * K)
85
+
86
+ watermark_image = self.watermark_image.copy().resize(
87
+ (watermark_size, watermark_size),
88
+ PIL.Image.Resampling.BICUBIC,
89
+ reducing_gap=None)
90
+
91
+ for image in images:
92
+ image.paste(watermark_image,
93
+ box=(
94
+ watermark_x - watermark_size,
95
+ watermark_y - watermark_size,
96
+ watermark_x,
97
+ watermark_y,
98
+ ),
99
+ mask=watermark_image.split()[-1])
100
+
101
+ @staticmethod
102
+ def to_pil_images(images: torch.Tensor) -> list[PIL.Image.Image]:
103
+ images = (images / 2 + 0.5).clamp(0, 1)
104
+ images = images.cpu().permute(0, 2, 3, 1).float().numpy()
105
+ images = np.round(images * 255).astype(np.uint8)
106
+ return [PIL.Image.fromarray(image) for image in images]
107
+
108
+ @staticmethod
109
+ def check_seed(seed: int) -> None:
110
+ if not 0 <= seed <= MAX_SEED:
111
+ raise ValueError
112
+
113
+ @staticmethod
114
+ def check_num_images(num_images: int) -> None:
115
+ if not 1 <= num_images <= MAX_NUM_IMAGES:
116
+ raise ValueError
117
+
118
+ @staticmethod
119
+ def check_num_inference_steps(num_steps: int) -> None:
120
+ if not 1 <= num_steps <= MAX_NUM_STEPS:
121
+ raise ValueError
122
+
123
+ @staticmethod
124
+ def get_custom_timesteps(name: str) -> list[int] | None:
125
+ if name == 'none':
126
+ timesteps = None
127
+ elif name == 'fast27':
128
+ timesteps = fast27_timesteps
129
+ elif name == 'smart27':
130
+ timesteps = smart27_timesteps
131
+ elif name == 'smart50':
132
+ timesteps = smart50_timesteps
133
+ elif name == 'smart100':
134
+ timesteps = smart100_timesteps
135
+ elif name == 'smart185':
136
+ timesteps = smart185_timesteps
137
+ else:
138
+ raise ValueError
139
+ return timesteps
140
+
141
+ @staticmethod
142
+ def run_garbage_collection():
143
+ gc.collect()
144
+ torch.cuda.empty_cache()
145
+
146
+ def run_stage1(
147
+ self,
148
+ prompt: str,
149
+ negative_prompt: str = '',
150
+ seed: int = 0,
151
+ num_images: int = 1,
152
+ guidance_scale_1: float = 7.0,
153
+ custom_timesteps_1: str = 'smart100',
154
+ num_inference_steps_1: int = 100,
155
+ ) -> tuple[list[PIL.Image.Image], str, str]:
156
+ self.check_seed(seed)
157
+ self.check_num_images(num_images)
158
+ self.check_num_inference_steps(num_inference_steps_1)
159
+
160
+ if RUN_GARBAGE_COLLECTION:
161
+ self.run_garbage_collection()
162
+
163
+ generator = torch.Generator(device=self.device).manual_seed(seed)
164
+
165
+ prompt_embeds, negative_embeds = self.pipe.encode_prompt(
166
+ prompt=prompt, negative_prompt=negative_prompt)
167
+
168
+ timesteps = self.get_custom_timesteps(custom_timesteps_1)
169
+
170
+ images = self.pipe(prompt_embeds=prompt_embeds,
171
+ negative_prompt_embeds=negative_embeds,
172
+ num_images_per_prompt=num_images,
173
+ guidance_scale=guidance_scale_1,
174
+ timesteps=timesteps,
175
+ num_inference_steps=num_inference_steps_1,
176
+ generator=generator,
177
+ output_type='pt').images
178
+ pil_images = self.to_pil_images(images)
179
+ self.pipe.watermarker.apply_watermark(
180
+ pil_images, self.pipe.unet.config.sample_size)
181
+
182
+ stage1_params = {
183
+ 'prompt': prompt,
184
+ 'negative_prompt': negative_prompt,
185
+ 'seed': seed,
186
+ 'num_images': num_images,
187
+ 'guidance_scale_1': guidance_scale_1,
188
+ 'custom_timesteps_1': custom_timesteps_1,
189
+ 'num_inference_steps_1': num_inference_steps_1,
190
+ }
191
+ with tempfile.NamedTemporaryFile(mode='w', delete=False) as param_file:
192
+ param_file.write(json.dumps(stage1_params))
193
+ stage1_result = {
194
+ 'prompt_embeds': prompt_embeds,
195
+ 'negative_embeds': negative_embeds,
196
+ 'images': images,
197
+ 'pil_images': pil_images,
198
+ }
199
+ with tempfile.NamedTemporaryFile(delete=False) as result_file:
200
+ torch.save(stage1_result, result_file.name)
201
+ return pil_images, param_file.name, result_file.name
202
+
203
+ def run_stage2(
204
+ self,
205
+ stage1_result_path: str,
206
+ stage2_index: int,
207
+ seed_2: int = 0,
208
+ guidance_scale_2: float = 4.0,
209
+ custom_timesteps_2: str = 'smart50',
210
+ num_inference_steps_2: int = 50,
211
+ disable_watermark: bool = False,
212
+ ) -> PIL.Image.Image:
213
+ self.check_seed(seed_2)
214
+ self.check_num_inference_steps(num_inference_steps_2)
215
+
216
+ if RUN_GARBAGE_COLLECTION:
217
+ self.run_garbage_collection()
218
+
219
+ generator = torch.Generator(device=self.device).manual_seed(seed_2)
220
+
221
+ stage1_result = torch.load(stage1_result_path)
222
+ prompt_embeds = stage1_result['prompt_embeds']
223
+ negative_embeds = stage1_result['negative_embeds']
224
+ images = stage1_result['images']
225
+ images = images[[stage2_index]]
226
+
227
+ timesteps = self.get_custom_timesteps(custom_timesteps_2)
228
+
229
+ out = self.super_res_1_pipe(image=images,
230
+ prompt_embeds=prompt_embeds,
231
+ negative_prompt_embeds=negative_embeds,
232
+ num_images_per_prompt=1,
233
+ guidance_scale=guidance_scale_2,
234
+ timesteps=timesteps,
235
+ num_inference_steps=num_inference_steps_2,
236
+ generator=generator,
237
+ output_type='pt',
238
+ noise_level=250).images
239
+ pil_images = self.to_pil_images(out)
240
+
241
+ if disable_watermark:
242
+ return pil_images[0]
243
+
244
+ self.super_res_1_pipe.watermarker.apply_watermark(
245
+ pil_images, self.super_res_1_pipe.unet.config.sample_size)
246
+ return pil_images[0]
247
+
248
+ def run_stage3(
249
+ self,
250
+ image: PIL.Image.Image,
251
+ prompt: str = '',
252
+ negative_prompt: str = '',
253
+ seed_3: int = 0,
254
+ guidance_scale_3: float = 9.0,
255
+ num_inference_steps_3: int = 75,
256
+ ) -> PIL.Image.Image:
257
+ self.check_seed(seed_3)
258
+ self.check_num_inference_steps(num_inference_steps_3)
259
+
260
+ if RUN_GARBAGE_COLLECTION:
261
+ self.run_garbage_collection()
262
+
263
+ generator = torch.Generator(device=self.device).manual_seed(seed_3)
264
+ out = self.super_res_2_pipe(image=image,
265
+ prompt=prompt,
266
+ negative_prompt=negative_prompt,
267
+ num_images_per_prompt=1,
268
+ guidance_scale=guidance_scale_3,
269
+ num_inference_steps=num_inference_steps_3,
270
+ generator=generator,
271
+ noise_level=100).images
272
+ self.apply_watermark_to_sd_x4_upscaler_results(out)
273
+ return out[0]
274
+
275
+ def run_stage2_3(
276
+ self,
277
+ stage1_result_path: str,
278
+ stage2_index: int,
279
+ seed_2: int = 0,
280
+ guidance_scale_2: float = 4.0,
281
+ custom_timesteps_2: str = 'smart50',
282
+ num_inference_steps_2: int = 50,
283
+ prompt: str = '',
284
+ negative_prompt: str = '',
285
+ seed_3: int = 0,
286
+ guidance_scale_3: float = 9.0,
287
+ num_inference_steps_3: int = 75,
288
+ ) -> Generator[PIL.Image.Image]:
289
+ self.check_seed(seed_3)
290
+ self.check_num_inference_steps(num_inference_steps_3)
291
+
292
+ out_image = self.run_stage2(
293
+ stage1_result_path=stage1_result_path,
294
+ stage2_index=stage2_index,
295
+ seed_2=seed_2,
296
+ guidance_scale_2=guidance_scale_2,
297
+ custom_timesteps_2=custom_timesteps_2,
298
+ num_inference_steps_2=num_inference_steps_2,
299
+ disable_watermark=True)
300
+ temp_image = out_image.copy()
301
+ self.super_res_1_pipe.watermarker.apply_watermark(
302
+ [temp_image], self.super_res_1_pipe.unet.config.sample_size)
303
+ yield temp_image
304
+ yield self.run_stage3(image=out_image,
305
+ prompt=prompt,
306
+ negative_prompt=negative_prompt,
307
+ seed_3=seed_3,
308
+ guidance_scale_3=guidance_scale_3,
309
+ num_inference_steps_3=num_inference_steps_3)
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==0.18.0
2
+ beautifulsoup4==4.12.2
3
+ bitsandbytes==0.38.1
4
+ diffusers==0.16.0
5
+ ftfy==6.1.1
6
+ gradio==3.27.0
7
+ huggingface_hub==0.14.1
8
+ numpy==1.24.3
9
+ Pillow==9.5.0
10
+ safetensors==0.3.0
11
+ sentencepiece==0.1.98
12
+ tokenizers==0.13.3
13
+ torch==2.0.0
14
+ torchvision==0.15.1
15
+ tqdm==4.65.0
16
+ transformers==4.28.1
settings.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import numpy as np
4
+
5
+ HF_TOKEN = os.getenv('HF_TOKEN')
6
+ UPLOAD_REPO_ID = os.getenv('UPLOAD_REPO_ID')
7
+ UPLOAD_RESULT_IMAGE = os.getenv('UPLOAD_RESULT_IMAGE') == '1'
8
+
9
+ # UI options
10
+ SHOW_DUPLICATE_BUTTON = os.getenv('SHOW_DUPLICATE_BUTTON', '0') == '1'
11
+ SHOW_DEVICE_WARNING = os.getenv('SHOW_DEVICE_WARNING', '1') == '1'
12
+ SHOW_ADVANCED_OPTIONS = os.getenv('SHOW_ADVANCED_OPTIONS', '1') == '1'
13
+ SHOW_UPSCALE_TO_256_BUTTON = os.getenv('SHOW_UPSCALE_TO_256_BUTTON',
14
+ '0') == '1'
15
+ SHOW_NUM_IMAGES = os.getenv('SHOW_NUM_IMAGES_OPTION', '1') == '1'
16
+ SHOW_CUSTOM_TIMESTEPS_1 = os.getenv('SHOW_CUSTOM_TIMESTEPS_1', '1') == '1'
17
+ SHOW_CUSTOM_TIMESTEPS_2 = os.getenv('SHOW_CUSTOM_TIMESTEPS_2', '1') == '1'
18
+ SHOW_NUM_STEPS_1 = os.getenv('SHOW_NUM_STEPS_1', '0') == '1'
19
+ SHOW_NUM_STEPS_2 = os.getenv('SHOW_NUM_STEPS_2', '0') == '1'
20
+ SHOW_NUM_STEPS_3 = os.getenv('SHOW_NUM_STEPS_3', '1') == '1'
21
+ GALLERY_COLUMN_NUM = int(os.getenv('GALLERY_COLUMN_NUM', '4'))
22
+
23
+ # Parameters
24
+ MAX_QUEUE_SIZE = int(os.getenv('MAX_QUEUE_SIZE', '10'))
25
+ MAX_SEED = np.iinfo(np.int32).max
26
+ MAX_NUM_IMAGES = int(os.getenv('MAX_NUM_IMAGES', '4'))
27
+ DEFAULT_NUM_IMAGES = min(MAX_NUM_IMAGES,
28
+ int(os.getenv('DEFAULT_NUM_IMAGES', '4')))
29
+ MAX_NUM_STEPS = int(os.getenv('MAX_NUM_STEPS', '200'))
30
+ DEFAULT_CUSTOM_TIMESTEPS_1 = os.getenv('DEFAULT_CUSTOM_TIMESTEPS_1',
31
+ 'smart100')
32
+ DEFAULT_CUSTOM_TIMESTEPS_2 = os.getenv('DEFAULT_CUSTOM_TIMESTEPS_2', 'smart50')
33
+ DEFAULT_NUM_STEPS_3 = int(os.getenv('DEFAULT_NUM_STEPS_3', '40'))
34
+
35
+ # Model options
36
+ DISABLE_AUTOMATIC_CPU_OFFLOAD = os.getenv(
37
+ 'DISABLE_AUTOMATIC_CPU_OFFLOAD') == '1'
38
+ DISABLE_SD_X4_UPSCALER = os.getenv('DISABLE_SD_X4_UPSCALER') == '1'
39
+
40
+ # Other options
41
+ RUN_GARBAGE_COLLECTION = os.getenv('RUN_GARBAGE_COLLECTION', '1') == '1'
42
+ DEBUG = os.getenv('DEBUG') == '1'
43
+
44
+ # Default options for the public demo
45
+ if os.getenv('IS_PUBLIC_DEMO') == '1':
46
+ # UI
47
+ SHOW_DUPLICATE_BUTTON = True
48
+ SHOW_NUM_STEPS_3 = False
49
+ SHOW_CUSTOM_TIMESTEPS_1 = False
50
+ SHOW_CUSTOM_TIMESTEPS_2 = False
51
+ SHOW_NUM_IMAGES = False
52
+ # parameters
53
+ DEFAULT_CUSTOM_TIMESTEPS_1 = 'smart50'
54
+ # model
55
+ DISABLE_AUTOMATIC_CPU_OFFLOAD = True
56
+ RUN_GARBAGE_COLLECTION = False
style.css ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ This CSS file is modified from:
3
+ https://huggingface.co/spaces/stabilityai/stable-diffusion/blob/2794a3c3ba66115c307075098e713f572b08bf80/app.py
4
+ */
5
+
6
+ h1 {
7
+ text-align: center;
8
+ }
9
+
10
+ .gradio-container {
11
+ font-family: 'IBM Plex Sans', sans-serif;
12
+ }
13
+
14
+ .gr-button {
15
+ color: white;
16
+ border-color: black;
17
+ background: black;
18
+ }
19
+
20
+ input[type='range'] {
21
+ accent-color: black;
22
+ }
23
+
24
+ .dark input[type='range'] {
25
+ accent-color: #dfdfdf;
26
+ }
27
+
28
+ .container {
29
+ max-width: 730px;
30
+ margin: auto;
31
+ padding-top: 1.5rem;
32
+ }
33
+
34
+ #gallery {
35
+ min-height: auto;
36
+ height: 185px;
37
+ margin-top: 15px;
38
+ margin-left: auto;
39
+ margin-right: auto;
40
+ border-bottom-right-radius: .5rem !important;
41
+ border-bottom-left-radius: .5rem !important;
42
+ }
43
+ #gallery .grid-wrap, #gallery .empty{
44
+ height: 185px;
45
+ min-height: 185px;
46
+ }
47
+ #gallery .preview{
48
+ height: 185px;
49
+ min-height: 185px!important;
50
+ }
51
+ #gallery>div>.h-full {
52
+ min-height: 20rem;
53
+ }
54
+
55
+ .details:hover {
56
+ text-decoration: underline;
57
+ }
58
+
59
+ .gr-button {
60
+ white-space: nowrap;
61
+ }
62
+
63
+ .gr-button:focus {
64
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
65
+ outline: none;
66
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
67
+ --tw-border-opacity: 1;
68
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
69
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
70
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
71
+ --tw-ring-opacity: .5;
72
+ }
73
+
74
+ #advanced-btn {
75
+ font-size: .7rem !important;
76
+ line-height: 19px;
77
+ margin-top: 12px;
78
+ margin-bottom: 12px;
79
+ padding: 2px 8px;
80
+ border-radius: 14px !important;
81
+ }
82
+
83
+ #advanced-options {
84
+ display: none;
85
+ margin-bottom: 20px;
86
+ }
87
+
88
+ .footer {
89
+ margin-bottom: 45px;
90
+ margin-top: 35px;
91
+ text-align: center;
92
+ border-bottom: 1px solid #e5e5e5;
93
+ }
94
+
95
+ .footer>p {
96
+ font-size: .8rem;
97
+ display: inline-block;
98
+ padding: 0 10px;
99
+ transform: translateY(10px);
100
+ background: white;
101
+ }
102
+
103
+ .dark .footer {
104
+ border-color: #303030;
105
+ }
106
+
107
+ .dark .footer>p {
108
+ background: #0b0f19;
109
+ }
110
+
111
+ .acknowledgments h4 {
112
+ margin: 1.25em 0 .25em 0;
113
+ font-weight: bold;
114
+ font-size: 115%;
115
+ }
116
+
117
+ .animate-spin {
118
+ animation: spin 1s linear infinite;
119
+ }
120
+
121
+ @keyframes spin {
122
+ from {
123
+ transform: rotate(0deg);
124
+ }
125
+
126
+ to {
127
+ transform: rotate(360deg);
128
+ }
129
+ }
130
+
131
+ #share-btn-container {
132
+ display: flex;
133
+ padding-left: 0.5rem !important;
134
+ padding-right: 0.5rem !important;
135
+ background-color: #000000;
136
+ justify-content: center;
137
+ align-items: center;
138
+ border-radius: 9999px !important;
139
+ width: 13rem;
140
+ margin-top: 10px;
141
+ margin-left: auto;
142
+ }
143
+
144
+ #share-btn {
145
+ all: initial;
146
+ color: #ffffff;
147
+ font-weight: 600;
148
+ cursor: pointer;
149
+ font-family: 'IBM Plex Sans', sans-serif;
150
+ margin-left: 0.5rem !important;
151
+ padding-top: 0.25rem !important;
152
+ padding-bottom: 0.25rem !important;
153
+ right: 0;
154
+ }
155
+
156
+ #share-btn * {
157
+ all: unset;
158
+ }
159
+
160
+ #share-btn-container div:nth-child(-n+2) {
161
+ width: auto !important;
162
+ min-height: 0px !important;
163
+ }
164
+
165
+ #share-btn-container .wrap {
166
+ display: none !important;
167
+ }
168
+
169
+ .gr-form {
170
+ flex: 1 1 50%;
171
+ border-top-right-radius: 0;
172
+ border-bottom-right-radius: 0;
173
+ }
174
+
175
+ #prompt-container {
176
+ gap: 0;
177
+ }
178
+
179
+ #prompt-text-input,
180
+ #negative-prompt-text-input {
181
+ padding: .45rem 0.625rem
182
+ }
183
+
184
+ #component-16 {
185
+ border-top-width: 1px !important;
186
+ margin-top: 1em
187
+ }
188
+
189
+ .image_duplication {
190
+ position: absolute;
191
+ width: 100px;
192
+ left: 50px
193
+ }
194
+
195
+ #component-0 {
196
+ max-width: 730px;
197
+ margin: auto;
198
+ padding-top: 1.5rem;
199
+ }
200
+
201
+ #upscaled-image img {
202
+ object-fit: scale-down;
203
+ }