Spaces:
Paused
Paused
Update app.py
Browse files
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
|
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
|
26 |
# ----------------------------
|
27 |
-
|
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 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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
|
62 |
if image is None:
|
63 |
-
raise ValueError("π« Uploaded image is missing
|
64 |
|
65 |
-
# Step 1:
|
66 |
scene_plan = extract_scene_plan(prompt, image)
|
67 |
-
print("
|
68 |
|
69 |
-
# Step 2:
|
70 |
prompt_list = generate_prompt_variations_from_scene(scene_plan, prompt, num_variations)
|
71 |
-
print("
|
72 |
-
for i, p in enumerate(prompt_list):
|
73 |
-
print(f" {i+1}: {p}")
|
74 |
|
75 |
-
# Step 3:
|
76 |
negative_prompt = generate_negative_prompt_from_scene(scene_plan)
|
77 |
print("π« Negative Prompt:", negative_prompt)
|
78 |
|
79 |
-
#
|
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
|
88 |
outputs = []
|
89 |
for i, enriched_prompt in enumerate(prompt_list):
|
90 |
-
print(f"π¨ Generating
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
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
|
110 |
-
return ["
|
111 |
|
112 |
# ----------------------------
|
113 |
-
#
|
114 |
# ----------------------------
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
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 |
-
|
|
|
|
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()
|