Upload app.py
Browse files
app.py
CHANGED
@@ -15,256 +15,30 @@ from diffusers import (
|
|
15 |
EulerDiscreteScheduler # <-- Added import
|
16 |
)
|
17 |
|
|
|
|
|
|
|
18 |
import time
|
19 |
from style import css
|
20 |
|
21 |
BASE_MODEL = "SG161222/Realistic_Vision_V5.1_noVAE"
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
#upscaler = StableDiffusionLatentUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
43 |
-
#upscaler.to("cuda")
|
44 |
-
|
45 |
-
|
46 |
-
# Sampler map
|
47 |
-
SAMPLER_MAP = {
|
48 |
-
"DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True, algorithm_type="sde-dpmsolver++"),
|
49 |
-
"Euler": lambda config: EulerDiscreteScheduler.from_config(config),
|
50 |
-
}
|
51 |
-
|
52 |
-
def center_crop_resize(img, output_size=(512, 512)):
|
53 |
-
width, height = img.size
|
54 |
-
|
55 |
-
# Calculate dimensions to crop to the center
|
56 |
-
new_dimension = min(width, height)
|
57 |
-
left = (width - new_dimension)/2
|
58 |
-
top = (height - new_dimension)/2
|
59 |
-
right = (width + new_dimension)/2
|
60 |
-
bottom = (height + new_dimension)/2
|
61 |
-
|
62 |
-
# Crop and resize
|
63 |
-
img = img.crop((left, top, right, bottom))
|
64 |
-
img = img.resize(output_size)
|
65 |
-
|
66 |
-
return img
|
67 |
-
|
68 |
-
def common_upscale(samples, width, height, upscale_method, crop=False):
|
69 |
-
if crop == "center":
|
70 |
-
old_width = samples.shape[3]
|
71 |
-
old_height = samples.shape[2]
|
72 |
-
old_aspect = old_width / old_height
|
73 |
-
new_aspect = width / height
|
74 |
-
x = 0
|
75 |
-
y = 0
|
76 |
-
if old_aspect > new_aspect:
|
77 |
-
x = round((old_width - old_width * (new_aspect / old_aspect)) / 2)
|
78 |
-
elif old_aspect < new_aspect:
|
79 |
-
y = round((old_height - old_height * (old_aspect / new_aspect)) / 2)
|
80 |
-
s = samples[:,:,y:old_height-y,x:old_width-x]
|
81 |
-
else:
|
82 |
-
s = samples
|
83 |
-
|
84 |
-
return torch.nn.functional.interpolate(s, size=(height, width), mode=upscale_method)
|
85 |
-
|
86 |
-
def upscale(samples, upscale_method, scale_by):
|
87 |
-
#s = samples.copy()
|
88 |
-
width = round(samples["images"].shape[3] * scale_by)
|
89 |
-
height = round(samples["images"].shape[2] * scale_by)
|
90 |
-
s = common_upscale(samples["images"], width, height, upscale_method, "disabled")
|
91 |
-
return (s)
|
92 |
-
|
93 |
-
def check_inputs(prompt: str, control_image: Image.Image):
|
94 |
-
if control_image is None:
|
95 |
-
raise gr.Error("Please select or upload a photo of a person.")
|
96 |
-
if prompt is None or prompt == "":
|
97 |
-
raise gr.Error("Prompt is required")
|
98 |
-
|
99 |
-
def convert_to_pil(base64_image):
|
100 |
-
pil_image = processing_utils.decode_base64_to_image(base64_image)
|
101 |
-
return pil_image
|
102 |
-
|
103 |
-
def convert_to_base64(pil_image):
|
104 |
-
base64_image = processing_utils.encode_pil_to_base64(pil_image)
|
105 |
-
return base64_image
|
106 |
-
|
107 |
-
# Inference function
|
108 |
-
def inference(
|
109 |
-
control_image: Image.Image,
|
110 |
-
prompt: str,
|
111 |
-
negative_prompt: str,
|
112 |
-
guidance_scale: float = 8.0,
|
113 |
-
controlnet_conditioning_scale: float = 1,
|
114 |
-
control_guidance_start: float = 1,
|
115 |
-
control_guidance_end: float = 1,
|
116 |
-
upscaler_strength: float = 0.5,
|
117 |
-
seed: int = -1,
|
118 |
-
sampler = "DPM++ Karras SDE",
|
119 |
-
progress = gr.Progress(track_tqdm=True),
|
120 |
-
profile: gr.OAuthProfile | None = None,
|
121 |
-
):
|
122 |
-
start_time = time.time()
|
123 |
-
start_time_struct = time.localtime(start_time)
|
124 |
-
start_time_formatted = time.strftime("%H:%M:%S", start_time_struct)
|
125 |
-
print(f"Inference started at {start_time_formatted}")
|
126 |
-
|
127 |
-
# Generate the initial image
|
128 |
-
#init_image = init_pipe(prompt).images[0]
|
129 |
-
|
130 |
-
# Rest of your existing code
|
131 |
-
control_image_small = center_crop_resize(control_image)
|
132 |
-
control_image_large = center_crop_resize(control_image, (1024, 1024))
|
133 |
-
|
134 |
-
main_pipe.scheduler = SAMPLER_MAP[sampler](main_pipe.scheduler.config)
|
135 |
-
my_seed = random.randint(0, 2**32 - 1) if seed == -1 else seed
|
136 |
-
generator = torch.Generator(device="cuda").manual_seed(my_seed)
|
137 |
-
|
138 |
-
out = main_pipe(
|
139 |
-
prompt=prompt,
|
140 |
-
negative_prompt=negative_prompt,
|
141 |
-
image=control_image_small,
|
142 |
-
guidance_scale=float(guidance_scale),
|
143 |
-
controlnet_conditioning_scale=float(controlnet_conditioning_scale),
|
144 |
-
generator=generator,
|
145 |
-
control_guidance_start=float(control_guidance_start),
|
146 |
-
control_guidance_end=float(control_guidance_end),
|
147 |
-
num_inference_steps=15,
|
148 |
-
output_type="latent"
|
149 |
-
)
|
150 |
-
upscaled_latents = upscale(out, "nearest-exact", 2)
|
151 |
-
out_image = image_pipe(
|
152 |
-
prompt=prompt,
|
153 |
-
negative_prompt=negative_prompt,
|
154 |
-
control_image=control_image_large,
|
155 |
-
image=upscaled_latents,
|
156 |
-
guidance_scale=float(guidance_scale),
|
157 |
-
generator=generator,
|
158 |
-
num_inference_steps=20,
|
159 |
-
strength=upscaler_strength,
|
160 |
-
control_guidance_start=float(control_guidance_start),
|
161 |
-
control_guidance_end=float(control_guidance_end),
|
162 |
-
controlnet_conditioning_scale=float(controlnet_conditioning_scale)
|
163 |
-
)
|
164 |
-
end_time = time.time()
|
165 |
-
end_time_struct = time.localtime(end_time)
|
166 |
-
end_time_formatted = time.strftime("%H:%M:%S", end_time_struct)
|
167 |
-
print(f"Inference ended at {end_time_formatted}, taking {end_time-start_time}s")
|
168 |
-
|
169 |
-
# Save image + metadata
|
170 |
-
# user_history.save_image(
|
171 |
-
# label=prompt,
|
172 |
-
# image=out_image["images"][0],
|
173 |
-
# profile=profile,
|
174 |
-
# metadata={
|
175 |
-
# "prompt": prompt,
|
176 |
-
# "negative_prompt": negative_prompt,
|
177 |
-
# "guidance_scale": guidance_scale,
|
178 |
-
# "controlnet_conditioning_scale": controlnet_conditioning_scale,
|
179 |
-
# "control_guidance_start": control_guidance_start,
|
180 |
-
# "control_guidance_end": control_guidance_end,
|
181 |
-
# "upscaler_strength": upscaler_strength,
|
182 |
-
# "seed": seed,
|
183 |
-
# "sampler": sampler,
|
184 |
-
# },
|
185 |
-
# )
|
186 |
-
|
187 |
-
return out_image["images"][0], gr.update(visible=True), gr.update(visible=True), my_seed
|
188 |
-
|
189 |
-
with gr.Blocks() as app:
|
190 |
-
gr.Markdown(
|
191 |
-
'''
|
192 |
-
<center><h1>Core Ultra Heroes</h1></span>
|
193 |
-
<span font-size:16px;">Turn yourself into an AI-powered superhero!</span>
|
194 |
-
</center>
|
195 |
-
|
196 |
-
'''
|
197 |
-
)
|
198 |
-
state_img_input = gr.State()
|
199 |
-
state_img_output = gr.State()
|
200 |
-
with gr.Row():
|
201 |
-
with gr.Column():
|
202 |
-
control_image = gr.Image(label="Provide a photo of yourself", type="pil", elem_id="control_image")
|
203 |
-
# controlnet_conditioning_scale = gr.Slider(minimum=0.0, maximum=5.0, step=0.01, value=0.8, label="Illusion strength", elem_id="illusion_strength", info="ControlNet conditioning scale")
|
204 |
-
prompt = gr.Textbox(label="Prompt", elem_id="prompt", info="Type what you want to generate", placeholder="Medieval village scene with busy streets and castle in the distance")
|
205 |
-
negative_prompt = gr.Textbox(label="Negative Prompt", info="Type what you don't want to see", value="low quality", elem_id="negative_prompt")
|
206 |
-
with gr.Accordion(label="Advanced Options", open=False):
|
207 |
-
guidance_scale = gr.Slider(minimum=0.0, maximum=50.0, step=0.25, value=7.5, label="Guidance Scale")
|
208 |
-
sampler = gr.Dropdown(choices=list(SAMPLER_MAP.keys()), value="Euler")
|
209 |
-
control_start = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=0, label="Start of ControlNet")
|
210 |
-
control_end = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=1, label="End of ControlNet")
|
211 |
-
strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=1, label="Strength of the upscaler")
|
212 |
-
seed = gr.Slider(minimum=-1, maximum=9999999999, step=1, value=-1, label="Seed", info="-1 means random seed")
|
213 |
-
used_seed = gr.Number(label="Last seed used",interactive=False)
|
214 |
-
run_btn = gr.Button("Run")
|
215 |
-
with gr.Column():
|
216 |
-
result_image = gr.Image(label="You're a hero!", interactive=False, elem_id="output")
|
217 |
-
|
218 |
-
controlnet_conditioning_scale = 0.5
|
219 |
-
|
220 |
-
prompt.submit(
|
221 |
-
check_inputs,
|
222 |
-
inputs=[prompt, control_image],
|
223 |
-
queue=False
|
224 |
-
).success(
|
225 |
-
convert_to_pil,
|
226 |
-
inputs=[control_image],
|
227 |
-
outputs=[state_img_input],
|
228 |
-
queue=False,
|
229 |
-
preprocess=False,
|
230 |
-
).success(
|
231 |
-
inference,
|
232 |
-
inputs=[state_img_input, prompt, negative_prompt, guidance_scale, control_start, control_end, strength, seed, sampler],
|
233 |
-
outputs=[state_img_output, result_image, used_seed]
|
234 |
-
).success(
|
235 |
-
convert_to_base64,
|
236 |
-
inputs=[state_img_output],
|
237 |
-
outputs=[result_image],
|
238 |
-
queue=False,
|
239 |
-
postprocess=False
|
240 |
-
)
|
241 |
-
run_btn.click(
|
242 |
-
check_inputs,
|
243 |
-
inputs=[prompt, control_image],
|
244 |
-
queue=False
|
245 |
-
).success(
|
246 |
-
convert_to_pil,
|
247 |
-
inputs=[control_image],
|
248 |
-
outputs=[state_img_input],
|
249 |
-
queue=False,
|
250 |
-
preprocess=False,
|
251 |
-
).success(
|
252 |
-
inference,
|
253 |
-
inputs=[state_img_input, prompt, negative_prompt, guidance_scale, controlnet_conditioning_scale, control_start, control_end, strength, seed, sampler],
|
254 |
-
outputs=[state_img_output, result_image, used_seed]
|
255 |
-
).success(
|
256 |
-
convert_to_base64,
|
257 |
-
inputs=[state_img_output],
|
258 |
-
outputs=[result_image],
|
259 |
-
queue=False,
|
260 |
-
postprocess=False
|
261 |
-
)
|
262 |
-
|
263 |
-
with gr.Blocks(css=css) as app_with_history:
|
264 |
-
with gr.Tab("Demo"):
|
265 |
-
app.render()
|
266 |
-
|
267 |
-
app_with_history.queue(max_size=20,api_open=False )
|
268 |
-
|
269 |
-
if __name__ == "__main__":
|
270 |
-
app_with_history.launch(max_threads=400)
|
|
|
15 |
EulerDiscreteScheduler # <-- Added import
|
16 |
)
|
17 |
|
18 |
+
print(f"Is CUDA available: {torch.cuda.is_available()}")
|
19 |
+
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
|
20 |
+
|
21 |
import time
|
22 |
from style import css
|
23 |
|
24 |
BASE_MODEL = "SG161222/Realistic_Vision_V5.1_noVAE"
|
25 |
|
26 |
+
title = "Flan T5 and Vanilla T5"
|
27 |
+
description = "This demo compares [T5-large](https://huggingface.co/t5-large) and [Flan-T5-XX-large](https://huggingface.co/google/flan-t5-xxl). Note that T5 expects a very specific format of the prompts, so the examples below are not necessarily the best prompts to compare."
|
28 |
+
|
29 |
+
def inference(text):
|
30 |
+
output_flan = ""
|
31 |
+
output_vanilla = ""
|
32 |
+
return [output_flan, output_vanilla]
|
33 |
+
|
34 |
+
io = gr.Interface(
|
35 |
+
inference,
|
36 |
+
gr.Textbox(lines=3),
|
37 |
+
outputs=[
|
38 |
+
gr.Textbox(lines=3, label="Flan T5"),
|
39 |
+
gr.Textbox(lines=3, label="T5")
|
40 |
+
],
|
41 |
+
title=title,
|
42 |
+
description=description,
|
43 |
+
)
|
44 |
+
io.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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