Bhavibond commited on
Commit
7727288
·
verified ·
1 Parent(s): dad5224

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
4
+ from transformers import CLIPImageProcessor
5
+ from PIL import Image
6
+ import numpy as np
7
+
8
+ def load_controlnet_model():
9
+ """Load Stable Diffusion ControlNet pipeline with IP-Adapter."""
10
+ controlnet = ControlNetModel.from_pretrained(
11
+ "lllyasviel/sd-controlnet-depth", torch_dtype=torch.float16
12
+ )
13
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
14
+ "runwayml/stable-diffusion-v1-5",
15
+ controlnet=controlnet,
16
+ torch_dtype=torch.float16
17
+ ).to("cuda")
18
+ return pipe
19
+
20
+ def preprocess_image(image):
21
+ """Convert image to depth map for ControlNet input."""
22
+ image = image.convert("L") # Convert to grayscale
23
+ image = np.array(image)
24
+ depth_map = np.clip(image, 0, 255)
25
+ return Image.fromarray(depth_map)
26
+
27
+ def generate_image(prompt, input_image):
28
+ """Generate an image using Stable Diffusion ControlNet with IP-Adapter."""
29
+ pipe = load_controlnet_model()
30
+ processed_image = preprocess_image(input_image)
31
+ result = pipe(prompt, image=processed_image, num_inference_steps=50).images[0]
32
+ return result
33
+
34
+ # Gradio Interface
35
+ demo = gr.Interface(
36
+ fn=generate_image,
37
+ inputs=[
38
+ gr.Textbox(label="Enter your prompt"),
39
+ gr.Image(type="pil", label="Upload reference image")
40
+ ],
41
+ outputs=gr.Image(type="pil", label="Generated Image"),
42
+ title="Stable Diffusion with ControlNet and IP-Adapter",
43
+ description="Generate images with precise object placement and consistent style using ControlNet and IP-Adapter.",
44
+ )
45
+
46
+ demo.launch(share=True)