Spaces:
Running
on
Zero
Running
on
Zero
Update gradio_seesr.py
Browse files- gradio_seesr.py +48 -16
gradio_seesr.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import sys
|
@@ -8,9 +9,29 @@ import numpy as np
|
|
8 |
from PIL import Image
|
9 |
|
10 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
import torch.utils.checkpoint
|
12 |
from pytorch_lightning import seed_everything
|
13 |
-
from diffusers import AutoencoderKL,
|
14 |
from diffusers.utils import check_min_version
|
15 |
from diffusers.utils.import_utils import is_xformers_available
|
16 |
from transformers import CLIPTextModel, CLIPTokenizer, CLIPImageProcessor
|
@@ -36,7 +57,7 @@ ram_transforms = transforms.Compose([
|
|
36 |
])
|
37 |
|
38 |
snapshot_download(
|
39 |
-
repo_id="
|
40 |
local_dir="preset/models"
|
41 |
)
|
42 |
|
@@ -46,6 +67,11 @@ snapshot_download(
|
|
46 |
local_dir="preset/models/stable-diffusion-2-1-base"
|
47 |
)
|
48 |
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
# Load scheduler, tokenizer and models.
|
51 |
pretrained_model_path = 'preset/models/stable-diffusion-2-1-base'
|
@@ -65,15 +91,14 @@ text_encoder.requires_grad_(False)
|
|
65 |
unet.requires_grad_(False)
|
66 |
controlnet.requires_grad_(False)
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
raise ValueError("xformers is not available. Make sure it is installed correctly")
|
73 |
|
74 |
# Get the validation pipeline
|
75 |
validation_pipeline = StableDiffusionControlNetPipeline(
|
76 |
-
vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, feature_extractor=
|
77 |
unet=unet, controlnet=controlnet, scheduler=scheduler, safety_checker=None, requires_safety_checker=False,
|
78 |
)
|
79 |
|
@@ -96,10 +121,12 @@ tag_model = ram(pretrained='preset/models/ram_swin_large_14m.pth',
|
|
96 |
tag_model.eval()
|
97 |
tag_model.to(device, dtype=weight_dtype)
|
98 |
|
99 |
-
@
|
100 |
def process(
|
101 |
input_image: Image.Image,
|
102 |
user_prompt: str,
|
|
|
|
|
103 |
positive_prompt: str,
|
104 |
negative_prompt: str,
|
105 |
num_inference_steps: int,
|
@@ -150,7 +177,8 @@ def process(
|
|
150 |
height=height, width=width,
|
151 |
guidance_scale=cfg_scale, conditioning_scale=1,
|
152 |
start_point='lr', start_steps=999,ram_encoder_hidden_states=ram_encoder_hidden_states,
|
153 |
-
latent_tiled_size=latent_tiled_size, latent_tiled_overlap=latent_tiled_overlap
|
|
|
154 |
).images[0]
|
155 |
|
156 |
if True: # alpha<1.0:
|
@@ -181,8 +209,10 @@ with block:
|
|
181 |
gr.Markdown(MARKDOWN)
|
182 |
with gr.Row():
|
183 |
with gr.Column():
|
184 |
-
input_image = gr.Image(
|
185 |
-
|
|
|
|
|
186 |
with gr.Accordion("Options", open=True):
|
187 |
user_prompt = gr.Textbox(label="User Prompt", value="")
|
188 |
positive_prompt = gr.Textbox(label="Positive Prompt", value="clean, high-resolution, 8k, best quality, masterpiece")
|
@@ -190,19 +220,21 @@ with block:
|
|
190 |
label="Negative Prompt",
|
191 |
value="dotted, noise, blur, lowres, oversmooth, longbody, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality"
|
192 |
)
|
193 |
-
cfg_scale = gr.Slider(label="Classifier Free Guidance Scale (Set
|
194 |
-
num_inference_steps = gr.Slider(label="Inference Steps", minimum=
|
195 |
seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, value=231)
|
196 |
sample_times = gr.Slider(label="Sample Times", minimum=1, maximum=10, step=1, value=1)
|
197 |
latent_tiled_size = gr.Slider(label="Diffusion Tile Size", minimum=128, maximum=480, value=320, step=1)
|
198 |
latent_tiled_overlap = gr.Slider(label="Diffusion Tile Overlap", minimum=4, maximum=16, value=4, step=1)
|
199 |
scale_factor = gr.Number(label="SR Scale", value=4)
|
200 |
with gr.Column():
|
201 |
-
result_gallery = gr.Gallery(label="Output", show_label=False, elem_id="gallery")
|
202 |
|
203 |
inputs = [
|
204 |
input_image,
|
205 |
user_prompt,
|
|
|
|
|
206 |
positive_prompt,
|
207 |
negative_prompt,
|
208 |
num_inference_steps,
|
@@ -215,5 +247,5 @@ with block:
|
|
215 |
]
|
216 |
run_button.click(fn=process, inputs=inputs, outputs=[result_gallery])
|
217 |
|
218 |
-
block.launch()
|
219 |
|
|
|
1 |
+
import spaces
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
import sys
|
|
|
9 |
from PIL import Image
|
10 |
|
11 |
import torch
|
12 |
+
|
13 |
+
print(f'torch version:{torch.__version__}')
|
14 |
+
|
15 |
+
# import subprocess
|
16 |
+
# import importlib, site, sys
|
17 |
+
|
18 |
+
# # Re-discover all .pth/.egg-link files
|
19 |
+
# for sitedir in site.getsitepackages():
|
20 |
+
# site.addsitedir(sitedir)
|
21 |
+
|
22 |
+
# # Clear caches so importlib will pick up new modules
|
23 |
+
# importlib.invalidate_caches()
|
24 |
+
|
25 |
+
# def sh(cmd): subprocess.check_call(cmd, shell=True)
|
26 |
+
|
27 |
+
# sh("pip install -U xformers --index-url https://download.pytorch.org/whl/cu126")
|
28 |
+
|
29 |
+
# # tell Python to re-scan site-packages now that the egg-link exists
|
30 |
+
# import importlib, site; site.addsitedir(site.getsitepackages()[0]); importlib.invalidate_caches()
|
31 |
+
|
32 |
import torch.utils.checkpoint
|
33 |
from pytorch_lightning import seed_everything
|
34 |
+
from diffusers import AutoencoderKL, DDIMScheduler
|
35 |
from diffusers.utils import check_min_version
|
36 |
from diffusers.utils.import_utils import is_xformers_available
|
37 |
from transformers import CLIPTextModel, CLIPTokenizer, CLIPImageProcessor
|
|
|
57 |
])
|
58 |
|
59 |
snapshot_download(
|
60 |
+
repo_id="alexnasa/SEESR",
|
61 |
local_dir="preset/models"
|
62 |
)
|
63 |
|
|
|
67 |
local_dir="preset/models/stable-diffusion-2-1-base"
|
68 |
)
|
69 |
|
70 |
+
snapshot_download(
|
71 |
+
repo_id="xinyu1205/recognize_anything_model",
|
72 |
+
local_dir="preset/models/"
|
73 |
+
)
|
74 |
+
|
75 |
|
76 |
# Load scheduler, tokenizer and models.
|
77 |
pretrained_model_path = 'preset/models/stable-diffusion-2-1-base'
|
|
|
91 |
unet.requires_grad_(False)
|
92 |
controlnet.requires_grad_(False)
|
93 |
|
94 |
+
# unet.to("cuda")
|
95 |
+
# controlnet.to("cuda")
|
96 |
+
# unet.enable_xformers_memory_efficient_attention()
|
97 |
+
# controlnet.enable_xformers_memory_efficient_attention()
|
|
|
98 |
|
99 |
# Get the validation pipeline
|
100 |
validation_pipeline = StableDiffusionControlNetPipeline(
|
101 |
+
vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, feature_extractor=None,
|
102 |
unet=unet, controlnet=controlnet, scheduler=scheduler, safety_checker=None, requires_safety_checker=False,
|
103 |
)
|
104 |
|
|
|
121 |
tag_model.eval()
|
122 |
tag_model.to(device, dtype=weight_dtype)
|
123 |
|
124 |
+
@spaces.GPU()
|
125 |
def process(
|
126 |
input_image: Image.Image,
|
127 |
user_prompt: str,
|
128 |
+
use_KDS: bool,
|
129 |
+
num_particles: int,
|
130 |
positive_prompt: str,
|
131 |
negative_prompt: str,
|
132 |
num_inference_steps: int,
|
|
|
177 |
height=height, width=width,
|
178 |
guidance_scale=cfg_scale, conditioning_scale=1,
|
179 |
start_point='lr', start_steps=999,ram_encoder_hidden_states=ram_encoder_hidden_states,
|
180 |
+
latent_tiled_size=latent_tiled_size, latent_tiled_overlap=latent_tiled_overlap, use_KDS=use_KDS,
|
181 |
+
num_particles=num_particles
|
182 |
).images[0]
|
183 |
|
184 |
if True: # alpha<1.0:
|
|
|
209 |
gr.Markdown(MARKDOWN)
|
210 |
with gr.Row():
|
211 |
with gr.Column():
|
212 |
+
input_image = gr.Image(type="pil")
|
213 |
+
num_particles = gr.Slider(label="Num of Partickes", minimum=1, maximum=16, step=1, value=4)
|
214 |
+
use_KDS = gr.Checkbox(label="Use Kernel Density Steering")
|
215 |
+
run_button = gr.Button("Run")
|
216 |
with gr.Accordion("Options", open=True):
|
217 |
user_prompt = gr.Textbox(label="User Prompt", value="")
|
218 |
positive_prompt = gr.Textbox(label="Positive Prompt", value="clean, high-resolution, 8k, best quality, masterpiece")
|
|
|
220 |
label="Negative Prompt",
|
221 |
value="dotted, noise, blur, lowres, oversmooth, longbody, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality"
|
222 |
)
|
223 |
+
cfg_scale = gr.Slider(label="Classifier Free Guidance Scale (Set to 1.0 in sd-turbo)", minimum=1, maximum=1, value=1, step=0)
|
224 |
+
num_inference_steps = gr.Slider(label="Inference Steps", minimum=2, maximum=8, value=2, step=1)
|
225 |
seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, value=231)
|
226 |
sample_times = gr.Slider(label="Sample Times", minimum=1, maximum=10, step=1, value=1)
|
227 |
latent_tiled_size = gr.Slider(label="Diffusion Tile Size", minimum=128, maximum=480, value=320, step=1)
|
228 |
latent_tiled_overlap = gr.Slider(label="Diffusion Tile Overlap", minimum=4, maximum=16, value=4, step=1)
|
229 |
scale_factor = gr.Number(label="SR Scale", value=4)
|
230 |
with gr.Column():
|
231 |
+
result_gallery = gr.Gallery(label="Output", show_label=False, elem_id="gallery")
|
232 |
|
233 |
inputs = [
|
234 |
input_image,
|
235 |
user_prompt,
|
236 |
+
use_KDS,
|
237 |
+
num_particles,
|
238 |
positive_prompt,
|
239 |
negative_prompt,
|
240 |
num_inference_steps,
|
|
|
247 |
]
|
248 |
run_button.click(fn=process, inputs=inputs, outputs=[result_gallery])
|
249 |
|
250 |
+
block.launch(share=True)
|
251 |
|