Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 7 |
+
controlnet_id = "lllyasviel/control_v11p_sd15_openpose"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
controlnet = ControlNetModel.from_pretrained(controlnet_id, torch_dtype=torch.float32)
|
| 11 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 12 |
+
model_id,
|
| 13 |
+
controlnet=controlnet,
|
| 14 |
+
safety_checker=None, # Disable safety checker for demo purposes
|
| 15 |
+
torch_dtype=torch.float32
|
| 16 |
+
)
|
| 17 |
+
pipe = pipe.to("cpu")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
pipe.enable_attention_slicing()
|
| 21 |
+
|
| 22 |
+
def generate_image(prompt, control_image, num_inference_steps=25, guidance_scale=7.5, controlnet_conditioning_scale=1.0):
|
| 23 |
+
"""
|
| 24 |
+
Generate an image using the ControlNet pipeline.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
prompt (str): Your text prompt for image generation.
|
| 28 |
+
control_image (PIL.Image): A control image to guide generation.
|
| 29 |
+
num_inference_steps (int): Number of denoising steps.
|
| 30 |
+
guidance_scale (float): Classifier-free guidance scale.
|
| 31 |
+
controlnet_conditioning_scale (float): How strongly to condition on the control image.
|
| 32 |
+
|
| 33 |
+
Returns:
|
| 34 |
+
PIL.Image: The generated image.
|
| 35 |
+
"""
|
| 36 |
+
result = pipe(
|
| 37 |
+
prompt=prompt,
|
| 38 |
+
image=control_image,
|
| 39 |
+
num_inference_steps=num_inference_steps,
|
| 40 |
+
guidance_scale=guidance_scale,
|
| 41 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale
|
| 42 |
+
)
|
| 43 |
+
return result.images[0]
|
| 44 |
+
|
| 45 |
+
# Create the Gradio interface.
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("# ControlNet Image Generator on CPU\nThis demo uses a ControlNet pipeline (openpose variant) with Stable Diffusion to generate images guided by a control image. Note: Running on CPU can be slow!")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your image prompt here", value="A futuristic cityscape at dusk")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
control_image_input = gr.Image(label="Control Image", type="pil", source="upload")
|
| 54 |
+
output_image = gr.Image(label="Generated Image", type="pil")
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
num_steps = gr.Slider(minimum=10, maximum=50, value=25, step=1, label="Inference Steps")
|
| 58 |
+
guidance = gr.Slider(minimum=1.0, maximum=15.0, value=7.5, step=0.5, label="Guidance Scale")
|
| 59 |
+
control_scale = gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="ControlNet Conditioning Scale")
|
| 60 |
+
|
| 61 |
+
generate_btn = gr.Button("Generate Image")
|
| 62 |
+
generate_btn.click(
|
| 63 |
+
fn=generate_image,
|
| 64 |
+
inputs=[prompt_input, control_image_input, num_steps, guidance, control_scale],
|
| 65 |
+
outputs=output_image
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
demo.launch()
|