File size: 8,821 Bytes
94bc625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd4d0db
94bc625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
961921f
94bc625
0c975eb
94bc625
 
 
 
0c975eb
 
94bc625
 
 
 
 
 
 
961921f
e9be03d
94bc625
 
 
e9be03d
 
 
 
 
 
 
 
 
 
 
94bc625
 
 
 
 
0c975eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94bc625
 
 
 
0c975eb
94bc625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import os
import torch
from PIL import Image
import numpy as np
import cv2
import random
import gradio as gr
from gradio.themes import Soft

from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
from diffusers import AutoencoderKL, UNet2DConditionModel, DDPMScheduler
from transformers import AutoTokenizer, CLIPTextModel, CLIPFeatureExtractor
from transformers import DPTForDepthEstimation, DPTImageProcessor


stable_diffusion_base = "runwayml/stable-diffusion-v1-5"

finetune_controlnet_path = "controlnet"

DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
DTYPE = torch.float16 if torch.cuda.is_available() else torch.float32

pipeline = None
depth_estimator_model = None
depth_estimator_processor = None


def load_depth_estimator():
    global depth_estimator_model, depth_estimator_processor
    if depth_estimator_model is None:
        model_name = "Intel/dpt-hybrid-midas"
        depth_estimator_model = DPTForDepthEstimation.from_pretrained(model_name)
        depth_estimator_processor = DPTImageProcessor.from_pretrained(model_name)
        depth_estimator_model.to(DEVICE)
        depth_estimator_model.eval()

    return depth_estimator_model, depth_estimator_processor



def load_diffusion_pipeline():
    global pipeline
    if pipeline is None:
        try:
            if not os.path.exists(finetune_controlnet_path):
                raise FileNotFoundError(f"ControlNet model not found: {finetune_controlnet_path}")

            # 1. Load individual components of the base Stable Diffusion pipeline from Hugging Face Hub
            vae = AutoencoderKL.from_pretrained(stable_diffusion_base, subfolder="vae", torch_dtype=DTYPE)
            tokenizer = AutoTokenizer.from_pretrained(stable_diffusion_base, subfolder="tokenizer")
            text_encoder = CLIPTextModel.from_pretrained(stable_diffusion_base, subfolder="text_encoder", torch_dtype=DTYPE)
            unet = UNet2DConditionModel.from_pretrained(stable_diffusion_base, subfolder="unet", torch_dtype=DTYPE)
            scheduler = DDPMScheduler.from_pretrained(stable_diffusion_base, subfolder="scheduler")
            feature_extractor = CLIPFeatureExtractor.from_pretrained(stable_diffusion_base, subfolder="feature_extractor")

            controlnet = ControlNetModel.from_pretrained(finetune_controlnet_path, torch_dtype=DTYPE)
            pipeline = StableDiffusionControlNetPipeline(
                vae=vae,
                text_encoder=text_encoder,
                tokenizer=tokenizer,
                unet=unet,
                controlnet=controlnet, # Your fine-tuned ControlNet
                scheduler=scheduler,
                safety_checker=None,
                feature_extractor=feature_extractor,
                image_encoder=None, # Explicitly set to None as it's not part of this setup
                requires_safety_checker=False,
            )

            pipeline.to(DEVICE)
            if torch.cuda.is_available() and hasattr(pipeline, "enable_xformers_memeory_efficient_attention"):
                try:
                    pipeline.enable_xformers_memory_efficient_attention()
                    print("xformers memory efficient attention enabled.")
                except Exception as e:
                    print(f"Could not enable xformers: {e}")


            load_depth_estimator()

        except Exception as e:
            print(f"Error loading pipeline: {e}")
            pipeline = None
            raise RuntimeError(f"Failed to load diffusion pipeline: {e}")
    return pipeline



def estimate_depth(pil_image: Image.Image) ->Image.Image:
    global depth_estimator_model, depth_estimator_processor
    if depth_estimator_model is None or depth_estimator_processor is None:
        try:
            load_depth_estimator()
        except RuntimeError as e:
            raise RuntimeError(f"Depth estimator not loaded: {e}")

    input = depth_estimator_processor(pil_image, return_tensors = "pt")
    input = {k: v.to(DEVICE) for k, v in input.items()}


    with torch.no_grad():
      output = depth_estimator_model(**input)
      predicted_depth = output.predicted_depth

    depth_numpy = predicted_depth.squeeze().cpu().numpy()

    min_depth = depth_numpy.min()
    max_depth = depth_numpy.max()
    normalized_depth = (depth_numpy - min_depth) / (max_depth - min_depth)

    inverted_normalized_depth = 1 - normalized_depth

    depth_image_array = (inverted_normalized_depth * 255).astype(np.uint8)
    depth_pil_image = Image.fromarray(depth_image_array).convert("RGB")

    print("Depth estimation complete.")
    return depth_pil_image


def generate_image_for_gradio(
    input_image_for_depth: Image.Image,
    prompt: str
) -> Image.Image:

    global pipeline
    if pipeline is None:
        try:
            load_diffusion_pipeline()
        except RuntimeError as e:
            return gr.Error(f"Model not loaded: {e}")

    try:
        depth_map_pil = estimate_depth(input_image_for_depth)
    except Exception as e:
        return gr.Error(f"Error during depth estimation: {e}")

    print(f"Generating image for prompt: '{prompt}'")

    negative_prompt = "lowres, watermark, banner, logo, watermark, contactinfo, text, deformed, blurry, blur, out of focus, out of frame, surreal, ugly"
    control_image = depth_map_pil.convert("RGB")
    control_image = control_image.resize((512, 512), Image.LANCZOS)

    input_image_for_pipeline = [control_image]

    generator = None
    # if seed is None:
    seed = random.randint(0, 100000)
    generator = torch.Generator(device=DEVICE).manual_seed(seed)


    with torch.no_grad():
        generated_images = pipeline(
            prompt,
            image=input_image_for_pipeline,
            num_inference_steps=25,
            guidance_scale=8.0,
            generator=generator,
        ).images

    # with torch.no_grad():
    #     generated_images = pipeline(
    #         prompt,
    #         negative_prompt,
    #         image=input_image_for_pipeline,
    #         num_inference_steps=25,
    #         # guidance_scale=8.0,
    #         strength = 0.85,
    #         generator=generator,
    #     ).images

    print(f"Image generation complete (seed: {seed}).")
    return generated_images[0]



# iface = gr.Interface(
#     fn=generate_image_for_gradio,
#     inputs=[
#         gr.Textbox(label="Prompt", value="a high-quality photo of a modern interior design"),
#         gr.Image(type="pil", label="Input Image (for Depth Estimation)"),
#         gr.Slider(minimum=10, maximum=100, value=25, step=1, label="Inference Steps"),
#         gr.Slider(minimum=1.0, maximum=20.0, value=8.0, step=0.5, label="Guidance Scale"),
#         gr.Number(label="Seed (optional, leave blank for random)", value=None),
#         gr.Number(label="Resolution", value=512, interactive=False)
#     ],
#     outputs=gr.Image(type="pil", label="Generated Image"),
#     title="Stable Diffusion ControlNet Depth Demo (with Depth Estimation)",
#     description="Upload an input image, and the app will estimate its depth map, then use it with your prompt to generate a new image. This allows for structural guidance from your input photo.",
#     allow_flagging="never",
#     live=False,
#     theme=Soft(),

iface = gr.Interface(
    fn=generate_image_for_gradio,
    inputs=[
        gr.Image(type="pil", label="Input Image (for Depth Estimation)"),
        gr.Textbox(label="Prompt", value="a high-quality photo of a modern interior design"),
    ],
    outputs=gr.Image(type="pil", label="Generated Image"),
    title="Stable Diffusion ControlNet Depth Demo (with Depth Estimation)",
    description="Upload an input image, and the app will estimate its depth map, then use it with your prompt to generate a new image. This allows for structural guidance from your input photo.",
    allow_flagging="never",
    live=False,
    theme=Soft(),
    css="""
    /* Target the upload icon within the Image component */
    .gr-image .icon-lg {
        font-size: 2em !important; /* Adjust size as needed, e.g., 2em, 3em */
        max-width: 50px; /* Max width to prevent it from filling the container */
        max-height: 50px; /* Max height */
    }
    /* Target the image placeholder icon (if it's different) */
    .gr-image .gr-image-placeholder {
        max-width: 100px; /* Adjust size as needed */
        max-height: 100px;
        object-fit: contain; /* Ensures the icon scales down without distortion */
    }
    /* General styling for the image input area to ensure it has space */
    .gr-image-container {
        min-height: 200px; /* Give the image input area a minimum height */
        display: flex;
        align-items: center;
        justify-content: center;
    }
    """
)



load_diffusion_pipeline()


if __name__ == "__main__":
    iface.launch()