Manireddy1508 commited on
Commit
ec84a8b
Β·
verified Β·
1 Parent(s): f63d065

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -6,46 +6,46 @@ import os
6
  import torch
7
  import numpy as np
8
  import cv2
9
- from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel
 
10
  from utils.planner import extract_scene_plan, generate_prompt_variations_from_scene # 🧠 Brain Layer
11
 
12
  # ----------------------------
13
  # πŸ”§ Device Setup
14
  # ----------------------------
15
- device = "cpu" # βœ… Force CPU since you're getting NVIDIA driver error
16
- dtype = torch.float32 # βœ… Avoid float16 on CPU
17
 
18
  # ----------------------------
19
- # βœ… Use Valid Public Models
20
  # ----------------------------
21
- # Valid ControlNet Canny model for SDXL
22
  controlnet = ControlNetModel.from_pretrained(
23
- "diffusers/controlnet-canny-sdxl-1.0", # ⚠️ Replace with actual model or use SD 1.5 version for now
24
  torch_dtype=dtype
25
  )
26
 
27
- # Base SDXL model
28
- pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
29
  "stabilityai/stable-diffusion-xl-base-1.0",
30
  controlnet=controlnet,
31
  torch_dtype=dtype
32
  ).to(device)
33
 
34
- # βœ… Comment out GPU-only features
35
- # pipe.enable_xformers_memory_efficient_attention() # Only for CUDA
36
- # pipe.enable_model_cpu_offload() # Not needed on CPU
37
-
38
  # ----------------------------
39
- # πŸ–Ό Canny Edge Generator
40
  # ----------------------------
41
  def generate_canny_map(image: Image.Image) -> Image.Image:
42
  print("πŸ” Generating Canny map...")
43
  if image is None:
44
  raise ValueError("🚫 No image passed to Canny generator")
 
45
  image = image.resize((1024, 1024)).convert("RGB")
46
  np_image = np.array(image)
47
  gray = cv2.cvtColor(np_image, cv2.COLOR_RGB2GRAY)
48
  edges = cv2.Canny(gray, 100, 200)
 
 
 
 
49
  return Image.fromarray(edges).convert("RGB")
50
 
51
  # ----------------------------
@@ -54,35 +54,39 @@ def generate_canny_map(image: Image.Image) -> Image.Image:
54
  def process_image(prompt, image, num_variations):
55
  try:
56
  print("🧠 Prompt received:", prompt)
 
57
  if image is None:
58
  raise ValueError("🚫 Uploaded image is missing or invalid.")
59
 
 
60
  scene_plan = extract_scene_plan(prompt)
61
  print("🧠 Scene plan extracted:", scene_plan)
62
 
 
63
  prompt_list = generate_prompt_variations_from_scene(scene_plan, prompt, num_variations)
64
  print("🧠 Enriched Prompts:")
65
  for i, p in enumerate(prompt_list):
66
  print(f" {i+1}: {p}")
67
 
 
68
  image = image.resize((1024, 1024)).convert("RGB")
69
  canny_map = generate_canny_map(image)
70
 
71
  outputs = []
72
  for i, enriched_prompt in enumerate(prompt_list):
73
- print(f"🎨 Generating image {i+1} with enriched prompt")
74
  try:
75
  result = pipe(
76
  prompt=enriched_prompt,
77
  image=image,
78
- controlnet_conditioning_image=canny_map,
79
  num_inference_steps=30,
80
  strength=0.5,
81
  guidance_scale=7.5
82
  )
83
  outputs.append(result.images[0])
84
- except Exception as inner:
85
- print(f"❌ Failed to generate image {i+1}:", inner)
86
  outputs.append(Image.new("RGB", (512, 512), color="red"))
87
 
88
  return outputs, scene_plan, canny_map
@@ -95,7 +99,7 @@ def process_image(prompt, image, num_variations):
95
  # πŸ–Ό Gradio UI
96
  # ----------------------------
97
  with gr.Blocks() as demo:
98
- gr.Markdown("## 🧠 NewCrux AI β€” SDXL + Canny (CPU Mode)\nUpload a product image, enter a prompt, and generate scenes using ControlNet.")
99
 
100
  with gr.Row():
101
  with gr.Column():
@@ -117,4 +121,3 @@ with gr.Blocks() as demo:
117
 
118
  demo.launch()
119
 
120
-
 
6
  import torch
7
  import numpy as np
8
  import cv2
9
+
10
+ from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel
11
  from utils.planner import extract_scene_plan, generate_prompt_variations_from_scene # 🧠 Brain Layer
12
 
13
  # ----------------------------
14
  # πŸ”§ Device Setup
15
  # ----------------------------
16
+ device = "cpu" # βœ… Using CPU for now
17
+ dtype = torch.float32
18
 
19
  # ----------------------------
20
+ # βœ… Load ControlNet + SDXL Model (Corrected)
21
  # ----------------------------
 
22
  controlnet = ControlNetModel.from_pretrained(
23
+ "diffusers/controlnet-canny-sdxl-1.0", # Use official Canny + SDXL ControlNet model
24
  torch_dtype=dtype
25
  )
26
 
27
+ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
 
28
  "stabilityai/stable-diffusion-xl-base-1.0",
29
  controlnet=controlnet,
30
  torch_dtype=dtype
31
  ).to(device)
32
 
 
 
 
 
33
  # ----------------------------
34
+ # πŸ” Canny Edge Generator
35
  # ----------------------------
36
  def generate_canny_map(image: Image.Image) -> Image.Image:
37
  print("πŸ” Generating Canny map...")
38
  if image is None:
39
  raise ValueError("🚫 No image passed to Canny generator")
40
+
41
  image = image.resize((1024, 1024)).convert("RGB")
42
  np_image = np.array(image)
43
  gray = cv2.cvtColor(np_image, cv2.COLOR_RGB2GRAY)
44
  edges = cv2.Canny(gray, 100, 200)
45
+
46
+ if edges is None:
47
+ raise ValueError("🚫 OpenCV Canny failed to produce edge map")
48
+
49
  return Image.fromarray(edges).convert("RGB")
50
 
51
  # ----------------------------
 
54
  def process_image(prompt, image, num_variations):
55
  try:
56
  print("🧠 Prompt received:", prompt)
57
+
58
  if image is None:
59
  raise ValueError("🚫 Uploaded image is missing or invalid.")
60
 
61
+ # Step 1: Extract scene plan
62
  scene_plan = extract_scene_plan(prompt)
63
  print("🧠 Scene plan extracted:", scene_plan)
64
 
65
+ # Step 2: Generate enriched prompt variations
66
  prompt_list = generate_prompt_variations_from_scene(scene_plan, prompt, num_variations)
67
  print("🧠 Enriched Prompts:")
68
  for i, p in enumerate(prompt_list):
69
  print(f" {i+1}: {p}")
70
 
71
+ # Step 3: Prepare image and Canny edge
72
  image = image.resize((1024, 1024)).convert("RGB")
73
  canny_map = generate_canny_map(image)
74
 
75
  outputs = []
76
  for i, enriched_prompt in enumerate(prompt_list):
77
+ print(f"🎨 Generating image {i+1}...")
78
  try:
79
  result = pipe(
80
  prompt=enriched_prompt,
81
  image=image,
82
+ control_image=canny_map,
83
  num_inference_steps=30,
84
  strength=0.5,
85
  guidance_scale=7.5
86
  )
87
  outputs.append(result.images[0])
88
+ except Exception as err:
89
+ print(f"❌ Failed to generate image {i+1}:", err)
90
  outputs.append(Image.new("RGB", (512, 512), color="red"))
91
 
92
  return outputs, scene_plan, canny_map
 
99
  # πŸ–Ό Gradio UI
100
  # ----------------------------
101
  with gr.Blocks() as demo:
102
+ gr.Markdown("## 🧠 NewCrux AI β€” SDXL + Canny (CPU Mode)\nUpload a product image, enter a prompt, and generate enhanced visuals using ControlNet.")
103
 
104
  with gr.Row():
105
  with gr.Column():
 
121
 
122
  demo.launch()
123