Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,51 +6,59 @@ from PIL import Image
|
|
| 6 |
import numpy as np
|
| 7 |
import cv2
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
| 10 |
controlnet = ControlNetModel.from_pretrained(
|
| 11 |
-
"lllyasviel/sd-controlnet-depth", torch_dtype=torch.
|
| 12 |
-
)
|
| 13 |
|
| 14 |
-
# Load
|
| 15 |
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 16 |
"runwayml/stable-diffusion-v1-5",
|
| 17 |
controlnet=controlnet,
|
| 18 |
-
torch_dtype=torch.
|
| 19 |
-
).to(
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to(
|
| 23 |
depth_processor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
|
| 24 |
|
| 25 |
def generate(input_image, prompt):
|
|
|
|
| 26 |
image = input_image.convert("RGB")
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
with torch.no_grad():
|
| 30 |
outputs = depth_model(**inputs)
|
| 31 |
depth = outputs.predicted_depth.squeeze().cpu().numpy()
|
| 32 |
|
|
|
|
| 33 |
depth = cv2.normalize(depth, None, 0, 255, norm_type=cv2.NORM_MINMAX)
|
| 34 |
depth_image = Image.fromarray(depth.astype(np.uint8))
|
| 35 |
|
|
|
|
| 36 |
result = pipe(
|
| 37 |
prompt=prompt,
|
| 38 |
image=depth_image,
|
| 39 |
-
height=512,
|
| 40 |
-
width=
|
| 41 |
-
num_inference_steps=
|
| 42 |
).images[0]
|
| 43 |
|
| 44 |
return result
|
| 45 |
|
| 46 |
-
#
|
| 47 |
gr.Interface(
|
| 48 |
fn=generate,
|
| 49 |
inputs=[
|
| 50 |
gr.Image(type="pil", label="Upload Room Image"),
|
| 51 |
-
gr.Textbox(label="Enter Interior Style Prompt"),
|
| 52 |
],
|
| 53 |
outputs=gr.Image(type="pil", label="Generated Room"),
|
| 54 |
-
title="ποΈ AI
|
| 55 |
-
description="Upload
|
| 56 |
).launch()
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
import cv2
|
| 8 |
|
| 9 |
+
# Detect GPU or fallback to CPU
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
# Load ControlNet model (depth conditioning)
|
| 13 |
controlnet = ControlNetModel.from_pretrained(
|
| 14 |
+
"lllyasviel/sd-controlnet-depth", torch_dtype=torch.float32
|
| 15 |
+
).to(device)
|
| 16 |
|
| 17 |
+
# Load Stable Diffusion with ControlNet
|
| 18 |
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 19 |
"runwayml/stable-diffusion-v1-5",
|
| 20 |
controlnet=controlnet,
|
| 21 |
+
torch_dtype=torch.float32
|
| 22 |
+
).to(device)
|
| 23 |
|
| 24 |
+
# Load depth estimation model
|
| 25 |
+
depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to(device)
|
| 26 |
depth_processor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
|
| 27 |
|
| 28 |
def generate(input_image, prompt):
|
| 29 |
+
# Convert image to RGB
|
| 30 |
image = input_image.convert("RGB")
|
| 31 |
+
|
| 32 |
+
# Prepare depth inputs
|
| 33 |
+
inputs = depth_processor(images=image, return_tensors="pt").to(device)
|
| 34 |
|
| 35 |
with torch.no_grad():
|
| 36 |
outputs = depth_model(**inputs)
|
| 37 |
depth = outputs.predicted_depth.squeeze().cpu().numpy()
|
| 38 |
|
| 39 |
+
# Normalize to 0β255 and convert to grayscale PIL
|
| 40 |
depth = cv2.normalize(depth, None, 0, 255, norm_type=cv2.NORM_MINMAX)
|
| 41 |
depth_image = Image.fromarray(depth.astype(np.uint8))
|
| 42 |
|
| 43 |
+
# Run image generation
|
| 44 |
result = pipe(
|
| 45 |
prompt=prompt,
|
| 46 |
image=depth_image,
|
| 47 |
+
height=512, # You can reduce this if slow
|
| 48 |
+
width=512,
|
| 49 |
+
num_inference_steps=10
|
| 50 |
).images[0]
|
| 51 |
|
| 52 |
return result
|
| 53 |
|
| 54 |
+
# Gradio interface
|
| 55 |
gr.Interface(
|
| 56 |
fn=generate,
|
| 57 |
inputs=[
|
| 58 |
gr.Image(type="pil", label="Upload Room Image"),
|
| 59 |
+
gr.Textbox(label="Enter Interior Style Prompt", placeholder="e.g. modern Japanese living room"),
|
| 60 |
],
|
| 61 |
outputs=gr.Image(type="pil", label="Generated Room"),
|
| 62 |
+
title="ποΈ AI Room Redesign (ControlNet + Depth)",
|
| 63 |
+
description="Upload a room image and get a redesigned version based on your style prompt using ControlNet Depth.",
|
| 64 |
).launch()
|