Files changed (1) hide show
  1. src/app.py +0 -299
src/app.py DELETED
@@ -1,299 +0,0 @@
1
- from pathlib import Path
2
-
3
- import gradio as gr
4
- import pillow_heif
5
- import spaces
6
- import torch
7
- from gradio_imageslider import ImageSlider
8
- from huggingface_hub import hf_hub_download
9
- from PIL import Image
10
- from refiners.fluxion.utils import manual_seed
11
- from refiners.foundationals.latent_diffusion import Solver, solvers
12
-
13
- from enhancer import ESRGANUpscaler, ESRGANUpscalerCheckpoints
14
-
15
- pillow_heif.register_heif_opener()
16
- pillow_heif.register_avif_opener()
17
-
18
- TITLE = """
19
- <center>
20
-
21
- <h1 style="font-size: 1.5rem; margin-bottom: 0.5rem;">
22
- Image Enhancer Powered By Refiners
23
- </h1>
24
-
25
- <div style="
26
- display: flex;
27
- align-items: center;
28
- justify-content: center;
29
- gap: 0.5rem;
30
- margin-bottom: 0.5rem;
31
- font-size: 1.25rem;
32
- flex-wrap: wrap;
33
- ">
34
- <a href="https://blog.finegrain.ai/posts/reproducing-clarity-upscaler/" target="_blank">[Blog Post]</a>
35
- <a href="https://github.com/finegrain-ai/refiners" target="_blank">[Refiners]</a>
36
- <a href="https://finegrain.ai/" target="_blank">[Finegrain]</a>
37
- <a href="https://huggingface.co/spaces/finegrain/finegrain-object-eraser" target="_blank">
38
- [Finegrain Object Eraser]
39
- </a>
40
- <a href="https://huggingface.co/spaces/finegrain/finegrain-object-cutter" target="_blank">
41
- [Finegrain Object Cutter]
42
- </a>
43
- </div>
44
-
45
- <p>
46
- Turn low resolution images into high resolution versions with added generated details (your image will be modified).
47
- </p>
48
-
49
- <p>
50
- This space is powered by Refiners, our open source micro-framework for simple foundation model adaptation.
51
- If you enjoyed it, please consider starring Refiners on GitHub!
52
- </p>
53
-
54
- <a href="https://github.com/finegrain-ai/refiners" target="_blank">
55
- <img src="https://img.shields.io/github/stars/finegrain-ai/refiners?style=social" />
56
- </a>
57
-
58
- </center>
59
- """
60
-
61
- CHECKPOINTS = ESRGANUpscalerCheckpoints(
62
- unet=Path(
63
- hf_hub_download(
64
- repo_id="refiners/juggernaut.reborn.sd1_5.unet",
65
- filename="model.safetensors",
66
- revision="347d14c3c782c4959cc4d1bb1e336d19f7dda4d2",
67
- )
68
- ),
69
- clip_text_encoder=Path(
70
- hf_hub_download(
71
- repo_id="refiners/juggernaut.reborn.sd1_5.text_encoder",
72
- filename="model.safetensors",
73
- revision="744ad6a5c0437ec02ad826df9f6ede102bb27481",
74
- )
75
- ),
76
- lda=Path(
77
- hf_hub_download(
78
- repo_id="refiners/juggernaut.reborn.sd1_5.autoencoder",
79
- filename="model.safetensors",
80
- revision="3c1aae3fc3e03e4a2b7e0fa42b62ebb64f1a4c19",
81
- )
82
- ),
83
- controlnet_tile=Path(
84
- hf_hub_download(
85
- repo_id="refiners/controlnet.sd1_5.tile",
86
- filename="model.safetensors",
87
- revision="48ced6ff8bfa873a8976fa467c3629a240643387",
88
- )
89
- ),
90
- esrgan=Path(
91
- hf_hub_download(
92
- repo_id="philz1337x/upscaler",
93
- filename="4x-UltraSharp.pth",
94
- revision="011deacac8270114eb7d2eeff4fe6fa9a837be70",
95
- )
96
- ),
97
- negative_embedding=Path(
98
- hf_hub_download(
99
- repo_id="philz1337x/embeddings",
100
- filename="JuggernautNegative-neg.pt",
101
- revision="203caa7e9cc2bc225031a4021f6ab1ded283454a",
102
- )
103
- ),
104
- negative_embedding_key="string_to_param.*",
105
- loras={
106
- "more_details": Path(
107
- hf_hub_download(
108
- repo_id="philz1337x/loras",
109
- filename="more_details.safetensors",
110
- revision="a3802c0280c0d00c2ab18d37454a8744c44e474e",
111
- )
112
- ),
113
- "sdxl_render": Path(
114
- hf_hub_download(
115
- repo_id="philz1337x/loras",
116
- filename="SDXLrender_v2.0.safetensors",
117
- revision="a3802c0280c0d00c2ab18d37454a8744c44e474e",
118
- )
119
- ),
120
- },
121
- )
122
-
123
- # initialize the enhancer, on the cpu
124
- DEVICE_CPU = torch.device("cpu")
125
- DTYPE = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float32
126
- enhancer = ESRGANUpscaler(checkpoints=CHECKPOINTS, device=DEVICE_CPU, dtype=DTYPE)
127
-
128
- # "move" the enhancer to the gpu, this is handled by Zero GPU
129
- DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
130
- enhancer.to(device=DEVICE, dtype=DTYPE)
131
-
132
-
133
- @spaces.GPU
134
- def process(
135
- input_image: Image.Image,
136
- prompt: str = "masterpiece, best quality, highres",
137
- negative_prompt: str = "worst quality, low quality, normal quality",
138
- seed: int = 42,
139
- upscale_factor: int = 2,
140
- controlnet_scale: float = 0.6,
141
- controlnet_decay: float = 1.0,
142
- condition_scale: int = 6,
143
- tile_width: int = 112,
144
- tile_height: int = 144,
145
- denoise_strength: float = 0.35,
146
- num_inference_steps: int = 18,
147
- solver: str = "DDIM",
148
- ) -> tuple[Image.Image, Image.Image]:
149
- manual_seed(seed)
150
-
151
- solver_type: type[Solver] = getattr(solvers, solver)
152
-
153
- enhanced_image = enhancer.upscale(
154
- image=input_image,
155
- prompt=prompt,
156
- negative_prompt=negative_prompt,
157
- upscale_factor=upscale_factor,
158
- controlnet_scale=controlnet_scale,
159
- controlnet_scale_decay=controlnet_decay,
160
- condition_scale=condition_scale,
161
- tile_size=(tile_height, tile_width),
162
- denoise_strength=denoise_strength,
163
- num_inference_steps=num_inference_steps,
164
- loras_scale={"more_details": 0.5, "sdxl_render": 1.0},
165
- solver_type=solver_type,
166
- )
167
-
168
- return (input_image, enhanced_image)
169
-
170
-
171
- with gr.Blocks() as demo:
172
- gr.HTML(TITLE)
173
-
174
- with gr.Row():
175
- with gr.Column():
176
- input_image = gr.Image(type="pil", label="Input Image")
177
- run_button = gr.ClearButton(components=None, value="Enhance Image")
178
- with gr.Column():
179
- output_slider = ImageSlider(label="Before / After")
180
- run_button.add(output_slider)
181
-
182
- with gr.Accordion("Advanced Options", open=False):
183
- prompt = gr.Textbox(
184
- label="Prompt",
185
- placeholder="masterpiece, best quality, highres",
186
- )
187
- negative_prompt = gr.Textbox(
188
- label="Negative Prompt",
189
- placeholder="worst quality, low quality, normal quality",
190
- )
191
- seed = gr.Slider(
192
- minimum=0,
193
- maximum=10_000,
194
- value=42,
195
- step=1,
196
- label="Seed",
197
- )
198
- upscale_factor = gr.Slider(
199
- minimum=1,
200
- maximum=4,
201
- value=2,
202
- step=0.2,
203
- label="Upscale Factor",
204
- )
205
- controlnet_scale = gr.Slider(
206
- minimum=0,
207
- maximum=1.5,
208
- value=0.6,
209
- step=0.1,
210
- label="ControlNet Scale",
211
- )
212
- controlnet_decay = gr.Slider(
213
- minimum=0.5,
214
- maximum=1,
215
- value=1.0,
216
- step=0.025,
217
- label="ControlNet Scale Decay",
218
- )
219
- condition_scale = gr.Slider(
220
- minimum=2,
221
- maximum=20,
222
- value=6,
223
- step=1,
224
- label="Condition Scale",
225
- )
226
- tile_width = gr.Slider(
227
- minimum=64,
228
- maximum=200,
229
- value=112,
230
- step=1,
231
- label="Latent Tile Width",
232
- )
233
- tile_height = gr.Slider(
234
- minimum=64,
235
- maximum=200,
236
- value=144,
237
- step=1,
238
- label="Latent Tile Height",
239
- )
240
- denoise_strength = gr.Slider(
241
- minimum=0,
242
- maximum=1,
243
- value=0.35,
244
- step=0.1,
245
- label="Denoise Strength",
246
- )
247
- num_inference_steps = gr.Slider(
248
- minimum=1,
249
- maximum=30,
250
- value=18,
251
- step=1,
252
- label="Number of Inference Steps",
253
- )
254
- solver = gr.Radio(
255
- choices=["DDIM", "DPMSolver"],
256
- value="DDIM",
257
- label="Solver",
258
- )
259
-
260
- run_button.click(
261
- fn=process,
262
- inputs=[
263
- input_image,
264
- prompt,
265
- negative_prompt,
266
- seed,
267
- upscale_factor,
268
- controlnet_scale,
269
- controlnet_decay,
270
- condition_scale,
271
- tile_width,
272
- tile_height,
273
- denoise_strength,
274
- num_inference_steps,
275
- solver,
276
- ],
277
- outputs=output_slider,
278
- )
279
-
280
- gr.Examples(
281
- examples=[
282
- "examples/kara-eads-L7EwHkq1B2s-unsplash.jpg",
283
- "examples/clarity_bird.webp",
284
- "examples/edgar-infocus-gJH8AqpiSEU-unsplash.jpg",
285
- "examples/jeremy-wallace-_XjW3oN8UOE-unsplash.jpg",
286
- "examples/karina-vorozheeva-rW-I87aPY5Y-unsplash.jpg",
287
- "examples/karographix-photography-hIaOPjYCEj4-unsplash.jpg",
288
- "examples/melissa-walker-horn-gtDYwUIr9Vg-unsplash.jpg",
289
- "examples/ryoji-iwata-X53e51WfjlE-unsplash.jpg",
290
- "examples/tadeusz-lakota-jggQZkITXng-unsplash.jpg",
291
- ],
292
- inputs=[input_image],
293
- outputs=output_slider,
294
- fn=process,
295
- cache_examples="lazy",
296
- run_on_click=False,
297
- )
298
-
299
- demo.launch(share=False)