1aurent commited on
Commit
d7a0271
·
unverified ·
0 Parent(s):
Files changed (6) hide show
  1. .gitattributes +35 -0
  2. .gitignore +164 -0
  3. README.md +13 -0
  4. pyproject.toml +2 -0
  5. requirements.txt +2 -0
  6. src/app.py +190 -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
.gitignore ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .gradio
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/latest/usage/project/#working-with-version-control
112
+ .pdm.toml
113
+ .pdm-python
114
+ .pdm-build/
115
+
116
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
117
+ __pypackages__/
118
+
119
+ # Celery stuff
120
+ celerybeat-schedule
121
+ celerybeat.pid
122
+
123
+ # SageMath parsed files
124
+ *.sage.py
125
+
126
+ # Environments
127
+ .env
128
+ .venv
129
+ env/
130
+ venv/
131
+ ENV/
132
+ env.bak/
133
+ venv.bak/
134
+
135
+ # Spyder project settings
136
+ .spyderproject
137
+ .spyproject
138
+
139
+ # Rope project settings
140
+ .ropeproject
141
+
142
+ # mkdocs documentation
143
+ /site
144
+
145
+ # mypy
146
+ .mypy_cache/
147
+ .dmypy.json
148
+ dmypy.json
149
+
150
+ # Pyre type checker
151
+ .pyre/
152
+
153
+ # pytype static type analyzer
154
+ .pytype/
155
+
156
+ # Cython debug symbols
157
+ cython_debug/
158
+
159
+ # PyCharm
160
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
161
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
162
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
163
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
164
+ #.idea/
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Refiners SD1.5
3
+ emoji: 🕯️
4
+ colorFrom: green
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 5.1.0
8
+ app_file: src/app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
pyproject.toml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ [tool.ruff]
2
+ line-length = 120
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ git+https://github.com/finegrain-ai/refiners@06204731093d8055e65b21b4da2ce586737d6ea4
2
+ pillow-heif>=0.18.0
src/app.py ADDED
@@ -0,0 +1,190 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr # pyright: ignore[reportMissingTypeStubs]
2
+ import pillow_heif # pyright: ignore[reportMissingTypeStubs]
3
+ import spaces # pyright: ignore[reportMissingTypeStubs]
4
+ import torch
5
+ from huggingface_hub import ( # pyright: ignore[reportMissingTypeStubs]
6
+ hf_hub_download, # pyright: ignore[reportUnknownVariableType]
7
+ )
8
+ from PIL import Image
9
+ from refiners.fluxion.utils import manual_seed, no_grad
10
+ from refiners.foundationals.latent_diffusion.stable_diffusion_1 import StableDiffusion_1
11
+
12
+ pillow_heif.register_heif_opener() # pyright: ignore[reportUnknownMemberType]
13
+ pillow_heif.register_avif_opener() # pyright: ignore[reportUnknownMemberType]
14
+
15
+ TITLE = """
16
+ # SD1.5 with Refiners
17
+ """
18
+
19
+ # initialize the model, on the cpu
20
+ DEVICE_CPU = torch.device("cpu")
21
+ DEVICE_GPU = torch.device("cuda" if torch.cuda.is_available() else "cpu")
22
+ DTYPE = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float32
23
+
24
+ model = StableDiffusion_1(device=DEVICE_CPU, dtype=DTYPE)
25
+ model.unet.load_from_safetensors(
26
+ tensors_path=hf_hub_download(
27
+ repo_id="refiners/sd15.unet",
28
+ filename="model.safetensors",
29
+ revision="6b01fc610c7465fa79e44c52c4d2eb0ea56821c9",
30
+ )
31
+ )
32
+ model.lda.load_from_safetensors(
33
+ tensors_path=hf_hub_download(
34
+ repo_id="refiners/sd15.autoencoder",
35
+ filename="model.safetensors",
36
+ revision="7565efe4812d8e14072111ab326b15eea4c908a5",
37
+ )
38
+ )
39
+ model.clip_text_encoder.load_from_safetensors(
40
+ tensors_path=hf_hub_download(
41
+ repo_id="refiners/sd15.text_encoder",
42
+ filename="model.safetensors",
43
+ revision="1b5023ecf0d646b7403f4ad182b6f0ab6b251fef",
44
+ )
45
+ )
46
+
47
+ # "move" the model to the gpu, this is handled/intercepted by Zero GPU
48
+ model.to(device=DEVICE_GPU, dtype=DTYPE)
49
+ model.unet.to(device=DEVICE_GPU, dtype=DTYPE)
50
+ model.lda.to(device=DEVICE_GPU, dtype=DTYPE)
51
+ model.clip_text_encoder.to(device=DEVICE_GPU, dtype=DTYPE)
52
+ model.solver.to(device=DEVICE_GPU, dtype=DTYPE)
53
+ model.device = DEVICE_GPU
54
+ model.dtype = DTYPE
55
+
56
+
57
+ @spaces.GPU
58
+ @no_grad()
59
+ def process(
60
+ prompt: str,
61
+ negative_prompt: str,
62
+ condition_scale: float,
63
+ num_inference_steps: int,
64
+ seed: int,
65
+ ) -> Image.Image:
66
+ assert condition_scale >= 0
67
+ assert num_inference_steps > 0
68
+ assert seed >= 0
69
+
70
+ # set the seed
71
+ manual_seed(seed)
72
+
73
+ # compute embeddings
74
+ clip_text_embedding = model.compute_clip_text_embedding(
75
+ text=prompt,
76
+ negative_text=negative_prompt,
77
+ )
78
+
79
+ # init latents
80
+ x = model.init_latents(size=(512, 512))
81
+
82
+ # denoise latents
83
+ for step in model.steps:
84
+ x = model(
85
+ x,
86
+ step=step,
87
+ clip_text_embedding=clip_text_embedding,
88
+ condition_scale=condition_scale,
89
+ )
90
+
91
+ # decode denoised latents
92
+ image = model.lda.latents_to_image(x)
93
+
94
+ return image
95
+
96
+
97
+ with gr.Blocks() as demo:
98
+ gr.Markdown(TITLE)
99
+
100
+ with gr.Column():
101
+ with gr.Row():
102
+ prompt = gr.Text(
103
+ label="Prompt",
104
+ show_label=False,
105
+ max_lines=1,
106
+ placeholder="Enter your prompt",
107
+ container=False,
108
+ )
109
+ run_button = gr.Button(
110
+ value="Run",
111
+ scale=0,
112
+ )
113
+
114
+ output_image = gr.Image(
115
+ label="Output Image",
116
+ image_mode="RGB",
117
+ type="pil",
118
+ )
119
+
120
+ with gr.Accordion("Advanced Settings", open=True):
121
+ negative_prompt = gr.Textbox(
122
+ label="Negative Prompt",
123
+ placeholder="Enter your (optional) negative prompt",
124
+ )
125
+ seed = gr.Slider(
126
+ label="Seed",
127
+ minimum=0,
128
+ maximum=100_000,
129
+ value=2,
130
+ step=1,
131
+ )
132
+ condition_scale = gr.Slider(
133
+ label="Condition scale",
134
+ minimum=0,
135
+ maximum=20,
136
+ value=7.5,
137
+ step=0.05,
138
+ )
139
+ num_inference_steps = gr.Slider(
140
+ label="Number of inference steps",
141
+ minimum=1,
142
+ maximum=50,
143
+ value=30,
144
+ step=1,
145
+ )
146
+
147
+ run_button.click(
148
+ fn=process,
149
+ inputs=[
150
+ prompt,
151
+ negative_prompt,
152
+ condition_scale,
153
+ num_inference_steps,
154
+ seed,
155
+ ],
156
+ outputs=output_image,
157
+ )
158
+
159
+ gr.Examples( # pyright: ignore[reportUnknownMemberType]
160
+ examples=[
161
+ [
162
+ "a cute cat, detailed high-quality professional image",
163
+ "lowres, bad anatomy, bad hands, cropped, worst quality",
164
+ 7.5,
165
+ 30,
166
+ 2,
167
+ ],
168
+ [
169
+ "a cute dog, detailed high-quality professional image",
170
+ "lowres, bad anatomy, bad hands, cropped, worst quality",
171
+ 7.5,
172
+ 30,
173
+ 2,
174
+ ],
175
+ ],
176
+ inputs=[
177
+ prompt,
178
+ negative_prompt,
179
+ condition_scale,
180
+ num_inference_steps,
181
+ seed,
182
+ ],
183
+ outputs=output_image,
184
+ fn=process,
185
+ cache_examples=True,
186
+ cache_mode="lazy",
187
+ run_on_click=False,
188
+ )
189
+
190
+ demo.launch()