Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import DiffusionPipeline, LCMScheduler
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
# Load the lightweight SD 1.5 model
|
| 7 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 8 |
+
adapter_id = "latent-consistency/lcm-lora-sdv1-5"
|
| 9 |
+
|
| 10 |
+
# Load pipeline and move to CPU
|
| 11 |
+
pipe = DiffusionPipeline.from_pretrained(model_id)
|
| 12 |
+
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 13 |
+
|
| 14 |
+
# Load the LCM LoRA to allow 4-step generation
|
| 15 |
+
pipe.load_lora_weights(adapter_id)
|
| 16 |
+
pipe.to("cpu")
|
| 17 |
+
|
| 18 |
+
# Function to generate image and time it
|
| 19 |
+
def generate(prompt):
|
| 20 |
+
start_time = time.time()
|
| 21 |
+
|
| 22 |
+
# We use 4 steps for the best speed/quality balance on 2 vCPUs
|
| 23 |
+
# guidance_scale is low (1.0 - 2.0) for LCM models
|
| 24 |
+
image = pipe(
|
| 25 |
+
prompt=prompt,
|
| 26 |
+
num_inference_steps=4,
|
| 27 |
+
guidance_scale=1.0,
|
| 28 |
+
width=512,
|
| 29 |
+
height=512
|
| 30 |
+
).images[0]
|
| 31 |
+
|
| 32 |
+
end_time = time.time()
|
| 33 |
+
duration = round(end_time - start_time, 2)
|
| 34 |
+
|
| 35 |
+
timer_output = f"✅ Image generated in {duration} seconds"
|
| 36 |
+
return image, timer_output
|
| 37 |
+
|
| 38 |
+
# Create the Gradio interface
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("# ⚡ Fast CPU Image Generator (SD 1.5 + LCM)")
|
| 41 |
+
gr.Markdown("Optimized for 2 vCPUs. Expect ~20-40 seconds per image.")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column():
|
| 45 |
+
prompt_input = gr.Textbox(label="Enter your prompt", placeholder="A cinematic forest...")
|
| 46 |
+
generate_btn = gr.Button("Generate Image")
|
| 47 |
+
|
| 48 |
+
with gr.Column():
|
| 49 |
+
timer_text = gr.Markdown("Timer: Ready")
|
| 50 |
+
image_output = gr.Image(label="Generated Image")
|
| 51 |
+
|
| 52 |
+
generate_btn.click(
|
| 53 |
+
fn=generate,
|
| 54 |
+
inputs=prompt_input,
|
| 55 |
+
outputs=[image_output, timer_text]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
demo.launch()
|