Spaces:
Paused
Paused
CryptoCreeper commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,22 +4,23 @@ import random
|
|
| 4 |
from diffusers import DiffusionPipeline, LCMScheduler
|
| 5 |
import time
|
| 6 |
|
| 7 |
-
# Load model
|
| 8 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 9 |
adapter_id = "latent-consistency/lcm-lora-sdv1-5"
|
| 10 |
|
|
|
|
| 11 |
pipe = DiffusionPipeline.from_pretrained(
|
| 12 |
model_id,
|
| 13 |
-
safety_checker=None,
|
| 14 |
-
|
| 15 |
)
|
| 16 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 17 |
pipe.load_lora_weights(adapter_id)
|
| 18 |
pipe.to("cpu")
|
| 19 |
|
| 20 |
-
# Estimation logic: Approx 8 seconds per step on 2-vCPUs for 512x512
|
| 21 |
def update_estimate(steps, size):
|
| 22 |
-
|
|
|
|
| 23 |
resolution_factor = (int(size) / 512) ** 2
|
| 24 |
estimate = round(steps * base_time_per_step * resolution_factor, 1)
|
| 25 |
return f"⏳ Estimated generation time: ~{estimate} seconds"
|
|
@@ -27,16 +28,14 @@ def update_estimate(steps, size):
|
|
| 27 |
def generate(prompt, size, steps, is_random, manual_seed):
|
| 28 |
start_time = time.time()
|
| 29 |
|
| 30 |
-
# Handle Seed
|
| 31 |
seed = random.randint(0, 1000000) if is_random else int(manual_seed)
|
| 32 |
-
generator = torch.Generator().manual_seed(seed)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
image = pipe(
|
| 37 |
prompt=prompt,
|
| 38 |
num_inference_steps=int(steps),
|
| 39 |
-
guidance_scale=1.0,
|
| 40 |
width=int(size),
|
| 41 |
height=int(size),
|
| 42 |
generator=generator
|
|
@@ -45,19 +44,21 @@ def generate(prompt, size, steps, is_random, manual_seed):
|
|
| 45 |
end_time = time.time()
|
| 46 |
duration = round(end_time - start_time, 2)
|
| 47 |
|
| 48 |
-
return
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
gr.Markdown("# ⚡ Fast CPU Image Gen (SD 1.5 + LCM)")
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
-
with gr.Column(
|
| 56 |
-
|
| 57 |
|
| 58 |
with gr.Accordion("Advanced Settings", open=False):
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
random_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
|
@@ -65,22 +66,22 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 65 |
|
| 66 |
estimate_display = gr.Markdown("⏳ Estimated generation time: ~32 seconds")
|
| 67 |
|
| 68 |
-
generate_btn = gr.Button("Generate", variant="primary")
|
| 69 |
|
| 70 |
-
with gr.Column(
|
| 71 |
status_label = gr.Label(value="Ready", label="Status")
|
| 72 |
-
image_out = gr.Image(label="Output")
|
| 73 |
|
| 74 |
-
#
|
| 75 |
random_seed.change(lambda x: gr.update(visible=not x), inputs=random_seed, outputs=seed_val)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
generate_btn.click(
|
| 82 |
fn=generate,
|
| 83 |
-
inputs=[
|
| 84 |
outputs=[image_out, status_label]
|
| 85 |
)
|
| 86 |
|
|
|
|
| 4 |
from diffusers import DiffusionPipeline, LCMScheduler
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
# Load model
|
| 8 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 9 |
adapter_id = "latent-consistency/lcm-lora-sdv1-5"
|
| 10 |
|
| 11 |
+
# Use float32 explicitly for CPU to avoid black images (NaN errors)
|
| 12 |
pipe = DiffusionPipeline.from_pretrained(
|
| 13 |
model_id,
|
| 14 |
+
safety_checker=None,
|
| 15 |
+
torch_dtype=torch.float32
|
| 16 |
)
|
| 17 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 18 |
pipe.load_lora_weights(adapter_id)
|
| 19 |
pipe.to("cpu")
|
| 20 |
|
|
|
|
| 21 |
def update_estimate(steps, size):
|
| 22 |
+
# 2-vCPU logic: ~8s per step at 512, scaled by total pixels
|
| 23 |
+
base_time_per_step = 8
|
| 24 |
resolution_factor = (int(size) / 512) ** 2
|
| 25 |
estimate = round(steps * base_time_per_step * resolution_factor, 1)
|
| 26 |
return f"⏳ Estimated generation time: ~{estimate} seconds"
|
|
|
|
| 28 |
def generate(prompt, size, steps, is_random, manual_seed):
|
| 29 |
start_time = time.time()
|
| 30 |
|
|
|
|
| 31 |
seed = random.randint(0, 1000000) if is_random else int(manual_seed)
|
| 32 |
+
generator = torch.Generator(device="cpu").manual_seed(seed)
|
| 33 |
|
| 34 |
+
# Run inference
|
| 35 |
+
output = pipe(
|
|
|
|
| 36 |
prompt=prompt,
|
| 37 |
num_inference_steps=int(steps),
|
| 38 |
+
guidance_scale=1.0,
|
| 39 |
width=int(size),
|
| 40 |
height=int(size),
|
| 41 |
generator=generator
|
|
|
|
| 44 |
end_time = time.time()
|
| 45 |
duration = round(end_time - start_time, 2)
|
| 46 |
|
| 47 |
+
return output, f"✅ Done in {duration}s | Seed: {seed}"
|
| 48 |
|
| 49 |
+
# Custom CSS to make it look clean
|
| 50 |
+
css = ".gradio-container {max-width: 850px !important}"
|
| 51 |
+
|
| 52 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 53 |
gr.Markdown("# ⚡ Fast CPU Image Gen (SD 1.5 + LCM)")
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
+
with gr.Column():
|
| 57 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="A cute robot eating a taco...", lines=3)
|
| 58 |
|
| 59 |
with gr.Accordion("Advanced Settings", open=False):
|
| 60 |
+
size_choice = gr.Radio([128, 256, 512, 768, 1024], label="Image Size", value=512)
|
| 61 |
+
step_slider = gr.Slider(1, 6, value=4, step=1, label="Steps (Keep low for CPU)")
|
| 62 |
|
| 63 |
with gr.Row():
|
| 64 |
random_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
|
|
|
| 66 |
|
| 67 |
estimate_display = gr.Markdown("⏳ Estimated generation time: ~32 seconds")
|
| 68 |
|
| 69 |
+
generate_btn = gr.Button("Generate Image", variant="primary")
|
| 70 |
|
| 71 |
+
with gr.Column():
|
| 72 |
status_label = gr.Label(value="Ready", label="Status")
|
| 73 |
+
image_out = gr.Image(label="Output", type="pil")
|
| 74 |
|
| 75 |
+
# Visibility Logic
|
| 76 |
random_seed.change(lambda x: gr.update(visible=not x), inputs=random_seed, outputs=seed_val)
|
| 77 |
|
| 78 |
+
# Estimation Logic
|
| 79 |
+
size_choice.change(update_estimate, [step_slider, size_choice], estimate_display)
|
| 80 |
+
step_slider.change(update_estimate, [step_slider, size_choice], estimate_display)
|
| 81 |
|
| 82 |
generate_btn.click(
|
| 83 |
fn=generate,
|
| 84 |
+
inputs=[prompt_input, size_choice, step_slider, random_seed, seed_val],
|
| 85 |
outputs=[image_out, status_label]
|
| 86 |
)
|
| 87 |
|