Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -24,34 +24,46 @@ controlnet = ControlNetModel.from_pretrained(
|
|
24 |
)
|
25 |
|
26 |
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
27 |
-
"stabilityai/stable-diffusion-xl-base-1.0",
|
28 |
controlnet=controlnet,
|
29 |
-
torch_dtype=dtype
|
|
|
30 |
).to(device)
|
31 |
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
# ----------------------------
|
35 |
# πΌ Canny Edge Generator
|
36 |
# ----------------------------
|
37 |
def generate_canny_map(image: Image.Image) -> Image.Image:
|
38 |
print("π Generating Canny map...")
|
|
|
|
|
|
|
|
|
39 |
image = image.resize((1024, 1024)).convert("RGB")
|
40 |
np_image = np.array(image)
|
41 |
-
|
42 |
-
edges = cv2.Canny(
|
43 |
-
canny_image = Image.fromarray(edges).convert("RGB")
|
44 |
-
return canny_image
|
45 |
|
|
|
|
|
|
|
|
|
46 |
|
47 |
# ----------------------------
|
48 |
# π¨ Image Generation Function
|
49 |
# ----------------------------
|
50 |
-
|
51 |
def process_image(prompt, image, num_variations):
|
52 |
try:
|
53 |
print("π§ Prompt received:", prompt)
|
54 |
|
|
|
|
|
|
|
55 |
# Step 1: Brain Layer (Scene Plan)
|
56 |
scene_plan = extract_scene_plan(prompt)
|
57 |
print("π§ Scene plan extracted:", scene_plan)
|
@@ -67,6 +79,9 @@ def process_image(prompt, image, num_variations):
|
|
67 |
image = image.resize((1024, 1024)).convert("RGB")
|
68 |
canny_map = generate_canny_map(image)
|
69 |
|
|
|
|
|
|
|
70 |
# Step 4: Generate images
|
71 |
outputs = []
|
72 |
for i, enriched_prompt in enumerate(prompt_list):
|
@@ -75,7 +90,7 @@ def process_image(prompt, image, num_variations):
|
|
75 |
result = pipe(
|
76 |
prompt=enriched_prompt,
|
77 |
image=image,
|
78 |
-
controlnet_conditioning_image=canny_map,
|
79 |
num_inference_steps=40,
|
80 |
strength=0.5,
|
81 |
guidance_scale=7.5
|
@@ -96,8 +111,6 @@ def process_image(prompt, image, num_variations):
|
|
96 |
print("β Generation failed:", e)
|
97 |
return ["β Error during generation"], {"error": str(e)}, None
|
98 |
|
99 |
-
|
100 |
-
|
101 |
# ----------------------------
|
102 |
# πΌ Gradio UI
|
103 |
# ----------------------------
|
@@ -129,4 +142,3 @@ with gr.Blocks() as demo:
|
|
129 |
|
130 |
demo.launch()
|
131 |
|
132 |
-
|
|
|
24 |
)
|
25 |
|
26 |
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
27 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
28 |
controlnet=controlnet,
|
29 |
+
torch_dtype=dtype,
|
30 |
+
variant="fp16" if dtype == torch.float16 else None
|
31 |
).to(device)
|
32 |
|
33 |
+
if device == "cuda":
|
34 |
+
pipe.enable_xformers_memory_efficient_attention()
|
35 |
+
else:
|
36 |
+
pipe.enable_model_cpu_offload()
|
37 |
|
38 |
# ----------------------------
|
39 |
# πΌ Canny Edge Generator
|
40 |
# ----------------------------
|
41 |
def generate_canny_map(image: Image.Image) -> Image.Image:
|
42 |
print("π Generating Canny map...")
|
43 |
+
|
44 |
+
if image is None:
|
45 |
+
raise ValueError("π« No image passed to Canny generator")
|
46 |
+
|
47 |
image = image.resize((1024, 1024)).convert("RGB")
|
48 |
np_image = np.array(image)
|
49 |
+
gray = cv2.cvtColor(np_image, cv2.COLOR_RGB2GRAY)
|
50 |
+
edges = cv2.Canny(gray, 100, 200)
|
|
|
|
|
51 |
|
52 |
+
if edges is None:
|
53 |
+
raise ValueError("π« OpenCV Canny failed to produce edge map")
|
54 |
+
|
55 |
+
return Image.fromarray(edges).convert("RGB")
|
56 |
|
57 |
# ----------------------------
|
58 |
# π¨ Image Generation Function
|
59 |
# ----------------------------
|
|
|
60 |
def process_image(prompt, image, num_variations):
|
61 |
try:
|
62 |
print("π§ Prompt received:", prompt)
|
63 |
|
64 |
+
if image is None:
|
65 |
+
raise ValueError("π« Uploaded image is missing or invalid.")
|
66 |
+
|
67 |
# Step 1: Brain Layer (Scene Plan)
|
68 |
scene_plan = extract_scene_plan(prompt)
|
69 |
print("π§ Scene plan extracted:", scene_plan)
|
|
|
79 |
image = image.resize((1024, 1024)).convert("RGB")
|
80 |
canny_map = generate_canny_map(image)
|
81 |
|
82 |
+
if canny_map is None:
|
83 |
+
raise ValueError("π« Canny map generation failed.")
|
84 |
+
|
85 |
# Step 4: Generate images
|
86 |
outputs = []
|
87 |
for i, enriched_prompt in enumerate(prompt_list):
|
|
|
90 |
result = pipe(
|
91 |
prompt=enriched_prompt,
|
92 |
image=image,
|
93 |
+
controlnet_conditioning_image=canny_map,
|
94 |
num_inference_steps=40,
|
95 |
strength=0.5,
|
96 |
guidance_scale=7.5
|
|
|
111 |
print("β Generation failed:", e)
|
112 |
return ["β Error during generation"], {"error": str(e)}, None
|
113 |
|
|
|
|
|
114 |
# ----------------------------
|
115 |
# πΌ Gradio UI
|
116 |
# ----------------------------
|
|
|
142 |
|
143 |
demo.launch()
|
144 |
|
|