Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,20 +53,20 @@ torch_dtype = torch.bfloat16
|
|
| 53 |
|
| 54 |
checkpoint = "microsoft/Phi-3.5-mini-instruct"
|
| 55 |
#vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 56 |
-
|
| 57 |
|
| 58 |
-
pipe = StableDiffusion3Pipeline.from_pretrained("ford442/stable-diffusion-3.5-medium-bf16", torch_dtype=torch.bfloat16)
|
| 59 |
#pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3.5-medium", token=hftoken, torch_dtype=torch.float32, device_map='balanced')
|
| 60 |
|
| 61 |
# pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")
|
| 62 |
|
| 63 |
#pipe.scheduler.config.requires_aesthetics_score = False
|
| 64 |
#pipe.enable_model_cpu_offload()
|
| 65 |
-
pipe.to(device)
|
| 66 |
#pipe = torch.compile(pipe)
|
| 67 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear"
|
| 68 |
|
| 69 |
-
|
| 70 |
#refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float32, requires_aesthetics_score=True, device_map='balanced')
|
| 71 |
|
| 72 |
#refiner.enable_model_cpu_offload()
|
|
@@ -74,7 +74,7 @@ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.conf
|
|
| 74 |
#refiner.scheduler.config.requires_aesthetics_score=False
|
| 75 |
#refiner.to(device)
|
| 76 |
#refiner = torch.compile(refiner)
|
| 77 |
-
|
| 78 |
|
| 79 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint, add_prefix_space=False, device_map='balanced')
|
| 80 |
tokenizer.tokenizer_legacy=False
|
|
@@ -90,7 +90,7 @@ def filter_text(text):
|
|
| 90 |
MAX_SEED = np.iinfo(np.int32).max
|
| 91 |
MAX_IMAGE_SIZE = 4096
|
| 92 |
|
| 93 |
-
@spaces.GPU(duration=
|
| 94 |
def infer(
|
| 95 |
prompt,
|
| 96 |
negative_prompt,
|
|
@@ -139,21 +139,31 @@ def infer(
|
|
| 139 |
print('-- filtered prompt --')
|
| 140 |
print(enhanced_prompt)
|
| 141 |
print('-- generating image --')
|
| 142 |
-
#with torch.no_grad():
|
| 143 |
sd_image = pipe(
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
).images[0]
|
| 152 |
print('-- got image --')
|
| 153 |
image_path = f"sd35m_{seed}.png"
|
| 154 |
sd_image.save(image_path,optimize=False,compress_level=0)
|
| 155 |
upload_to_ftp(image_path)
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
examples = [
|
| 159 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
|
@@ -168,6 +178,37 @@ css = """
|
|
| 168 |
}
|
| 169 |
"""
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
with gr.Blocks(css=css) as demo:
|
| 172 |
with gr.Column(elem_id="col-container"):
|
| 173 |
gr.Markdown(" # Text-to-Text-to-Image StableDiffusion 3.5 Medium (with refine)")
|
|
@@ -191,6 +232,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 191 |
placeholder="Enter a negative prompt",
|
| 192 |
visible=False,
|
| 193 |
)
|
|
|
|
|
|
|
|
|
|
| 194 |
seed = gr.Slider(
|
| 195 |
label="Seed",
|
| 196 |
minimum=0,
|
|
|
|
| 53 |
|
| 54 |
checkpoint = "microsoft/Phi-3.5-mini-instruct"
|
| 55 |
#vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 56 |
+
vae = AutoencoderKL.from_pretrained("ford442/sdxl-vae-bf16", torch_dtype=torch.bfloat16, device_map='balanced')
|
| 57 |
|
| 58 |
+
pipe = StableDiffusion3Pipeline.from_pretrained("ford442/stable-diffusion-3.5-medium-bf16", torch_dtype=torch.bfloat16, device_map='balanced')
|
| 59 |
#pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3.5-medium", token=hftoken, torch_dtype=torch.float32, device_map='balanced')
|
| 60 |
|
| 61 |
# pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")
|
| 62 |
|
| 63 |
#pipe.scheduler.config.requires_aesthetics_score = False
|
| 64 |
#pipe.enable_model_cpu_offload()
|
| 65 |
+
#pipe.to(device)
|
| 66 |
#pipe = torch.compile(pipe)
|
| 67 |
+
# pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config, beta_schedule="scaled_linear")
|
| 68 |
|
| 69 |
+
refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("ford442/stable-diffusion-xl-refiner-1.0-bf16", vae=vae, torch_dtype=torch.bfloat16, use_safetensors=True, requires_aesthetics_score=True, device_map='balanced')
|
| 70 |
#refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float32, requires_aesthetics_score=True, device_map='balanced')
|
| 71 |
|
| 72 |
#refiner.enable_model_cpu_offload()
|
|
|
|
| 74 |
#refiner.scheduler.config.requires_aesthetics_score=False
|
| 75 |
#refiner.to(device)
|
| 76 |
#refiner = torch.compile(refiner)
|
| 77 |
+
refiner.scheduler = EulerAncestralDiscreteScheduler.from_config(refiner.scheduler.config, beta_schedule="scaled_linear")
|
| 78 |
|
| 79 |
tokenizer = AutoTokenizer.from_pretrained(checkpoint, add_prefix_space=False, device_map='balanced')
|
| 80 |
tokenizer.tokenizer_legacy=False
|
|
|
|
| 90 |
MAX_SEED = np.iinfo(np.int32).max
|
| 91 |
MAX_IMAGE_SIZE = 4096
|
| 92 |
|
| 93 |
+
@spaces.GPU(duration=80)
|
| 94 |
def infer(
|
| 95 |
prompt,
|
| 96 |
negative_prompt,
|
|
|
|
| 139 |
print('-- filtered prompt --')
|
| 140 |
print(enhanced_prompt)
|
| 141 |
print('-- generating image --')
|
|
|
|
| 142 |
sd_image = pipe(
|
| 143 |
+
prompt=enhanced_prompt, # This conversion is fine
|
| 144 |
+
negative_prompt=negative_prompt,
|
| 145 |
+
guidance_scale=guidance_scale,
|
| 146 |
+
num_inference_steps=num_inference_steps,
|
| 147 |
+
width=width,
|
| 148 |
+
height=height,
|
| 149 |
+
generator=generator
|
| 150 |
).images[0]
|
| 151 |
print('-- got image --')
|
| 152 |
image_path = f"sd35m_{seed}.png"
|
| 153 |
sd_image.save(image_path,optimize=False,compress_level=0)
|
| 154 |
upload_to_ftp(image_path)
|
| 155 |
+
refine = refiner(
|
| 156 |
+
prompt=f"{prompt}, high quality masterpiece, complex details",
|
| 157 |
+
negative_prompt = negative_prompt,
|
| 158 |
+
guidance_scale=7.5,
|
| 159 |
+
num_inference_steps=num_inference_steps,
|
| 160 |
+
image=sd_image,
|
| 161 |
+
generator=generator,
|
| 162 |
+
).images[0]
|
| 163 |
+
refine_path = f"refine_{seed}.png"
|
| 164 |
+
refine.save(refine_path,optimize=False,compress_level=0)
|
| 165 |
+
upload_to_ftp(refine_path)
|
| 166 |
+
return refine, seed, refine_path, enhanced_prompt
|
| 167 |
|
| 168 |
examples = [
|
| 169 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
|
|
|
| 178 |
}
|
| 179 |
"""
|
| 180 |
|
| 181 |
+
def repeat_infer(
|
| 182 |
+
prompt,
|
| 183 |
+
negative_prompt,
|
| 184 |
+
seed,
|
| 185 |
+
randomize_seed,
|
| 186 |
+
width,
|
| 187 |
+
height,
|
| 188 |
+
guidance_scale,
|
| 189 |
+
num_inference_steps,
|
| 190 |
+
num_iterations, # New input for number of iterations
|
| 191 |
+
):
|
| 192 |
+
i = 0
|
| 193 |
+
while i < num_iterations:
|
| 194 |
+
time.sleep(700) # Wait for 10 minutes (600 seconds)
|
| 195 |
+
result, seed, image_path, enhanced_prompt = infer(
|
| 196 |
+
prompt,
|
| 197 |
+
negative_prompt,
|
| 198 |
+
seed,
|
| 199 |
+
randomize_seed,
|
| 200 |
+
width,
|
| 201 |
+
height,
|
| 202 |
+
guidance_scale,
|
| 203 |
+
num_inference_steps,
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# Optionally, you can add logic here to process the results of each iteration
|
| 207 |
+
# For example, you could display the image, save it with a different name, etc.
|
| 208 |
+
i += 1
|
| 209 |
+
return result, seed, image_path, enhanced_prompt
|
| 210 |
+
|
| 211 |
+
|
| 212 |
with gr.Blocks(css=css) as demo:
|
| 213 |
with gr.Column(elem_id="col-container"):
|
| 214 |
gr.Markdown(" # Text-to-Text-to-Image StableDiffusion 3.5 Medium (with refine)")
|
|
|
|
| 232 |
placeholder="Enter a negative prompt",
|
| 233 |
visible=False,
|
| 234 |
)
|
| 235 |
+
num_iterations = gr.Number(
|
| 236 |
+
value=1000,
|
| 237 |
+
label="Number of Iterations")
|
| 238 |
seed = gr.Slider(
|
| 239 |
label="Seed",
|
| 240 |
minimum=0,
|