import gradio as gr import torch from diffusers import DiffusionPipeline device = "cpu" if torch.cuda.is_available(): device = "cuda" model_id = "SimianLuo/LCM_Dreamshaper_v7" pipe = DiffusionPipeline.from_pretrained(model_id) pipe.to(device) def generate_image(prompt, width, height, steps): result = pipe( prompt=prompt, width=int(width), height=int(height), num_inference_steps=int(steps), guidance_scale=8.0, lcm_origin_steps=50, output_type="pil" ).images[0] return result with gr.Blocks() as demo: gr.Markdown("## LCM Image Generator (CPU Optimized)") with gr.Row(): with gr.Column(): prompt_input = gr.Textbox(label="Prompt", placeholder="A futuristic city with neon lights...") with gr.Row(): width_slider = gr.Slider(minimum=256, maximum=768, step=64, value=512, label="Width") height_slider = gr.Slider(minimum=256, maximum=768, step=64, value=512, label="Height") steps_slider = gr.Slider(minimum=1, maximum=15, step=1, value=4, label="Inference Steps (Quality)") generate_btn = gr.Button("Generate Image") with gr.Column(): image_output = gr.Image(label="Generated Image") generate_btn.click( fn=generate_image, inputs=[prompt_input, width_slider, height_slider, steps_slider], outputs=image_output ) demo.launch()