Manireddy1508 commited on
Commit
e074d8e
Β·
verified Β·
1 Parent(s): 8d8a928

Update app.py

Browse files

adding blip and negative prompt

Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -8,19 +8,23 @@ 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
 
@@ -54,30 +58,35 @@ 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
-
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,
@@ -99,7 +108,7 @@ def process_image(prompt, image, num_variations):
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():
@@ -120,4 +129,3 @@ with gr.Blocks() as demo:
120
  )
121
 
122
  demo.launch()
123
-
 
8
  import cv2
9
 
10
  from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel
11
+ from utils.planner import (
12
+ extract_scene_plan,
13
+ generate_prompt_variations_from_scene,
14
+ generate_negative_prompt_from_scene # βœ… Added negative prompt generator
15
+ )
16
 
17
  # ----------------------------
18
  # πŸ”§ Device Setup
19
  # ----------------------------
20
+ device = "cpu"
21
  dtype = torch.float32
22
 
23
  # ----------------------------
24
+ # βœ… Load ControlNet + SDXL Model
25
  # ----------------------------
26
  controlnet = ControlNetModel.from_pretrained(
27
+ "diffusers/controlnet-canny-sdxl-1.0",
28
  torch_dtype=dtype
29
  )
30
 
 
58
  def process_image(prompt, image, num_variations):
59
  try:
60
  print("🧠 Prompt received:", prompt)
 
61
  if image is None:
62
  raise ValueError("🚫 Uploaded image is missing or invalid.")
63
 
64
+ # Step 1: Scene Planning
65
+ scene_plan = extract_scene_plan(prompt, image)
66
  print("🧠 Scene plan extracted:", scene_plan)
67
 
68
+ # Step 2: Prompt Variations
69
  prompt_list = generate_prompt_variations_from_scene(scene_plan, prompt, num_variations)
70
  print("🧠 Enriched Prompts:")
71
  for i, p in enumerate(prompt_list):
72
  print(f" {i+1}: {p}")
73
 
74
+ # Step 3: Negative Prompt (auto-generated)
75
+ negative_prompt = generate_negative_prompt_from_scene(scene_plan)
76
+ print("🚫 Negative Prompt:", negative_prompt)
77
+
78
+ # Step 4: Prepare image & canny
79
  image = image.resize((1024, 1024)).convert("RGB")
80
  canny_map = generate_canny_map(image)
81
 
82
+ # Step 5: Generate images
83
  outputs = []
84
  for i, enriched_prompt in enumerate(prompt_list):
85
  print(f"🎨 Generating image {i+1}...")
86
  try:
87
  result = pipe(
88
  prompt=enriched_prompt,
89
+ negative_prompt=negative_prompt,
90
  image=image,
91
  control_image=canny_map,
92
  num_inference_steps=30,
 
108
  # πŸ–Ό Gradio UI
109
  # ----------------------------
110
  with gr.Blocks() as demo:
111
+ gr.Markdown("## 🧠 NewCrux AI β€” SDXL + ControlNet Canny (CPU Mode)\nUpload a product image, enter a prompt, and generate enhanced visuals with GPT + BLIP + SDXL.")
112
 
113
  with gr.Row():
114
  with gr.Column():
 
129
  )
130
 
131
  demo.launch()