Manireddy1508 commited on
Commit
b574e01
Β·
verified Β·
1 Parent(s): 0d5ecb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -93
app.py CHANGED
@@ -2,135 +2,92 @@
2
 
3
  import gradio as gr
4
  from PIL import Image
5
- import os
6
  import torch
7
- import numpy as np
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,
15
- save_generation_log
16
  )
17
 
18
  # ----------------------------
19
  # πŸ”§ Device Setup
20
  # ----------------------------
21
- device = "cpu"
22
- dtype = torch.float32
23
 
24
  # ----------------------------
25
- # βœ… Load ControlNet Canny + SDXL Model
26
  # ----------------------------
27
- controlnet = ControlNetModel.from_pretrained(
28
- "diffusers/controlnet-canny-sdxl-1.0",
29
- torch_dtype=dtype
30
- )
31
-
32
- pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
33
  "stabilityai/stable-diffusion-xl-base-1.0",
34
- controlnet=controlnet,
35
- torch_dtype=dtype
36
- ).to(device)
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
-
46
- image = image.resize((1024, 1024)).convert("RGB")
47
- np_image = np.array(image)
48
- gray = cv2.cvtColor(np_image, cv2.COLOR_RGB2GRAY)
49
- edges = cv2.Canny(gray, 100, 200)
50
-
51
- if edges is None:
52
- raise ValueError("🚫 OpenCV Canny failed to produce edge map")
53
-
54
- return Image.fromarray(edges).convert("RGB")
55
 
56
  # ----------------------------
57
  # 🎨 Image Generation Function
58
  # ----------------------------
59
  def process_image(prompt, image, num_variations):
60
  try:
61
- print("🧠 Prompt received:", prompt)
62
  if image is None:
63
- raise ValueError("🚫 Uploaded image is missing or invalid.")
64
 
65
- # Step 1: Scene Planning
66
  scene_plan = extract_scene_plan(prompt, image)
67
- print("🧠 Scene plan extracted:", scene_plan)
68
 
69
- # Step 2: Prompt Variations
70
  prompt_list = generate_prompt_variations_from_scene(scene_plan, prompt, num_variations)
71
- print("🧠 Enriched Prompts:")
72
- for i, p in enumerate(prompt_list):
73
- print(f" {i+1}: {p}")
74
 
75
- # Step 3: Negative Prompt
76
  negative_prompt = generate_negative_prompt_from_scene(scene_plan)
77
  print("🚫 Negative Prompt:", negative_prompt)
78
 
79
- # βœ… Save log
80
- caption = scene_plan.get("caption", "N/A")
81
- save_generation_log(caption, scene_plan, prompt_list, negative_prompt)
82
-
83
- # Step 4: Canny Edge Map
84
  image = image.resize((1024, 1024)).convert("RGB")
85
- canny_map = generate_canny_map(image)
86
 
87
- # Step 5: Generate Images
88
  outputs = []
89
  for i, enriched_prompt in enumerate(prompt_list):
90
- print(f"🎨 Generating image {i+1}...")
91
- try:
92
- result = pipe(
93
- prompt=enriched_prompt,
94
- negative_prompt=negative_prompt,
95
- image=image,
96
- control_image=canny_map,
97
- num_inference_steps=30,
98
- strength=0.2,
99
- guidance_scale=7.5
100
- )
101
- outputs.append(result.images[0])
102
- except Exception as err:
103
- print(f"❌ Failed to generate image {i+1}:", err)
104
- outputs.append(Image.new("RGB", (512, 512), color="red"))
105
-
106
- return outputs, scene_plan, canny_map
107
 
108
  except Exception as e:
109
- print("❌ Generation failed:", e)
110
- return ["❌ Error during generation"], {"error": str(e)}, None
111
 
112
  # ----------------------------
113
- # πŸ–Ό Gradio UI
114
  # ----------------------------
115
- with gr.Blocks() as demo:
116
- gr.Markdown("## 🧠 NewCrux AI β€” SDXL + ControlNet Canny (CPU Mode)\nUpload a product image, enter a prompt, and generate lifestyle visuals guided by edges.")
117
-
118
- with gr.Row():
119
- with gr.Column():
120
- prompt_input = gr.Textbox(label="Prompt")
121
- image_input = gr.Image(type="pil", label="Upload Product Image")
122
- variation_slider = gr.Slider(1, 4, step=1, value=1, label="Number of Variations")
123
- generate_btn = gr.Button("Generate")
124
-
125
- with gr.Column():
126
- output_gallery = gr.Gallery(label="Generated Variations", columns=2, rows=2, height="auto")
127
- json_output = gr.JSON(label="🧠 Brain Layer Reasoning")
128
- canny_preview = gr.Image(label="πŸ” Canny Edge Preview")
129
-
130
- generate_btn.click(
131
- fn=process_image,
132
- inputs=[prompt_input, image_input, variation_slider],
133
- outputs=[output_gallery, json_output, canny_preview]
134
- )
135
 
136
- demo.launch()
 
 
2
 
3
  import gradio as gr
4
  from PIL import Image
 
5
  import torch
 
 
6
 
7
+ from diffusers import StableDiffusionXLImg2ImgPipeline
8
  from utils.planner import (
9
  extract_scene_plan,
10
  generate_prompt_variations_from_scene,
11
+ generate_negative_prompt_from_scene
 
12
  )
13
 
14
  # ----------------------------
15
  # πŸ”§ Device Setup
16
  # ----------------------------
17
+ device = "cuda" if torch.cuda.is_available() else "cpu"
18
+ dtype = torch.float16 if device == "cuda" else torch.float32
19
 
20
  # ----------------------------
21
+ # βœ… Load SDXL Only Pipeline
22
  # ----------------------------
23
+ pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
 
 
 
 
 
24
  "stabilityai/stable-diffusion-xl-base-1.0",
25
+ torch_dtype=dtype,
26
+ variant="fp16" if device == "cuda" else None,
27
+ use_safetensors=True,
28
+ )
29
+ pipe.to(device)
30
+ pipe.enable_model_cpu_offload()
31
+ pipe.enable_attention_slicing()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  # ----------------------------
34
  # 🎨 Image Generation Function
35
  # ----------------------------
36
  def process_image(prompt, image, num_variations):
37
  try:
38
+ print("🧠 User Prompt:", prompt)
39
  if image is None:
40
+ raise ValueError("🚫 Uploaded image is missing.")
41
 
42
+ # Step 1: Extract scene plan
43
  scene_plan = extract_scene_plan(prompt, image)
44
+ print("πŸ“‹ Scene Plan:", scene_plan)
45
 
46
+ # Step 2: Generate enriched prompts
47
  prompt_list = generate_prompt_variations_from_scene(scene_plan, prompt, num_variations)
48
+ print("βœ… Enriched Prompts:", prompt_list)
 
 
49
 
50
+ # Step 3: Generate negative prompt
51
  negative_prompt = generate_negative_prompt_from_scene(scene_plan)
52
  print("🚫 Negative Prompt:", negative_prompt)
53
 
54
+ # Step 4: Resize image to SDXL resolution
 
 
 
 
55
  image = image.resize((1024, 1024)).convert("RGB")
 
56
 
57
+ # Step 5: Generate outputs with SDXL only
58
  outputs = []
59
  for i, enriched_prompt in enumerate(prompt_list):
60
+ print(f"🎨 Generating variation {i+1}...")
61
+ result = pipe(
62
+ prompt=enriched_prompt,
63
+ negative_prompt=negative_prompt,
64
+ image=image,
65
+ strength=0.7, # ← You can fine-tune this
66
+ guidance_scale=7.5,
67
+ num_inference_steps=30,
68
+ )
69
+ outputs.append(result.images[0])
70
+
71
+ return outputs
 
 
 
 
 
72
 
73
  except Exception as e:
74
+ print("❌ Generation Error:", e)
75
+ return [Image.new("RGB", (512, 512), color="red")]
76
 
77
  # ----------------------------
78
+ # πŸ–ΌοΈ Gradio Interface
79
  # ----------------------------
80
+ demo = gr.Interface(
81
+ fn=process_image,
82
+ inputs=[
83
+ gr.Textbox(label="Prompt"),
84
+ gr.Image(type="pil", label="Product Image"),
85
+ gr.Slider(1, 5, value=3, step=1, label="Number of Variations")
86
+ ],
87
+ outputs=gr.Gallery(label="Generated Images").style(grid=[2], height="auto"),
88
+ title="NewCrux Product Image Generator (SDXL Only)",
89
+ description="Upload a product image and enter a prompt. SDXL will generate enriched variations using AI."
90
+ )
 
 
 
 
 
 
 
 
 
91
 
92
+ if __name__ == "__main__":
93
+ demo.launch()