gaur3009 commited on
Commit
6dc1f73
·
verified ·
1 Parent(s): 27e4d16

Delete src1/app.py

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