Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import gradio as gr
|
|
4 |
from PIL import Image
|
5 |
import os
|
6 |
import torch
|
7 |
-
from io import BytesIO
|
8 |
from diffusers import StableDiffusionInpaintPipeline
|
9 |
from rembg import remove
|
10 |
from utils.planner import extract_scene_plan # π§ Brain Layer
|
@@ -28,13 +27,12 @@ if device == "cuda":
|
|
28 |
pipe.enable_model_cpu_offload()
|
29 |
|
30 |
# ----------------------------
|
31 |
-
# πͺ Auto-Generate Mask
|
32 |
# ----------------------------
|
33 |
def auto_generate_mask(image: Image.Image) -> Image.Image:
|
34 |
-
print("πͺ Generating mask using rembg")
|
35 |
-
no_bg = remove(image)
|
36 |
-
|
37 |
-
return mask
|
38 |
|
39 |
# ----------------------------
|
40 |
# π¨ Image Generation Function
|
@@ -43,38 +41,39 @@ def process_image(prompt, image, num_variations):
|
|
43 |
try:
|
44 |
print("π§ Prompt received:", prompt)
|
45 |
|
46 |
-
#
|
47 |
reasoning_json = extract_scene_plan(prompt)
|
48 |
print("π§ Scene plan extracted:", reasoning_json)
|
49 |
|
50 |
-
# Step 2:
|
51 |
image = image.resize((512, 512)).convert("RGB")
|
52 |
mask = auto_generate_mask(image).resize((512, 512)).convert("L")
|
53 |
|
54 |
-
|
55 |
for i in range(num_variations):
|
56 |
-
print(f"π¨ Generating variation {i
|
57 |
-
|
58 |
prompt=prompt,
|
59 |
image=image,
|
60 |
mask_image=mask,
|
61 |
strength=0.98,
|
62 |
guidance_scale=7.5,
|
63 |
num_inference_steps=40
|
64 |
-
)
|
65 |
-
|
|
|
66 |
|
67 |
-
return
|
68 |
|
69 |
except Exception as e:
|
70 |
-
print("β
|
71 |
-
return ["β
|
72 |
|
73 |
# ----------------------------
|
74 |
-
#
|
75 |
# ----------------------------
|
76 |
with gr.Blocks() as demo:
|
77 |
-
gr.Markdown("## π§ NewCrux AI
|
78 |
|
79 |
with gr.Row():
|
80 |
with gr.Column():
|
@@ -100,5 +99,3 @@ with gr.Blocks() as demo:
|
|
100 |
|
101 |
demo.launch()
|
102 |
|
103 |
-
|
104 |
-
|
|
|
4 |
from PIL import Image
|
5 |
import os
|
6 |
import torch
|
|
|
7 |
from diffusers import StableDiffusionInpaintPipeline
|
8 |
from rembg import remove
|
9 |
from utils.planner import extract_scene_plan # π§ Brain Layer
|
|
|
27 |
pipe.enable_model_cpu_offload()
|
28 |
|
29 |
# ----------------------------
|
30 |
+
# πͺ Auto-Generate Mask
|
31 |
# ----------------------------
|
32 |
def auto_generate_mask(image: Image.Image) -> Image.Image:
|
33 |
+
print("πͺ Generating mask using rembg...")
|
34 |
+
no_bg = remove(image) # Already a PIL image
|
35 |
+
return no_bg.convert("L") # Convert to grayscale for inpainting
|
|
|
36 |
|
37 |
# ----------------------------
|
38 |
# π¨ Image Generation Function
|
|
|
41 |
try:
|
42 |
print("π§ Prompt received:", prompt)
|
43 |
|
44 |
+
# Step 1: Brain Layer
|
45 |
reasoning_json = extract_scene_plan(prompt)
|
46 |
print("π§ Scene plan extracted:", reasoning_json)
|
47 |
|
48 |
+
# Step 2: Prepare image + auto-mask
|
49 |
image = image.resize((512, 512)).convert("RGB")
|
50 |
mask = auto_generate_mask(image).resize((512, 512)).convert("L")
|
51 |
|
52 |
+
outputs = []
|
53 |
for i in range(num_variations):
|
54 |
+
print(f"π¨ Generating variation {i+1}")
|
55 |
+
result = pipe(
|
56 |
prompt=prompt,
|
57 |
image=image,
|
58 |
mask_image=mask,
|
59 |
strength=0.98,
|
60 |
guidance_scale=7.5,
|
61 |
num_inference_steps=40
|
62 |
+
)
|
63 |
+
generated_image = result.images[0]
|
64 |
+
outputs.append(generated_image)
|
65 |
|
66 |
+
return outputs, reasoning_json
|
67 |
|
68 |
except Exception as e:
|
69 |
+
print("β Generation failed:", e)
|
70 |
+
return ["β Error during generation"], {"error": str(e)}
|
71 |
|
72 |
# ----------------------------
|
73 |
+
# πΌ Gradio UI
|
74 |
# ----------------------------
|
75 |
with gr.Blocks() as demo:
|
76 |
+
gr.Markdown("## π§ NewCrux AI β Auto Inpainting\nUpload a product image, enter a prompt, and let us generate magic!")
|
77 |
|
78 |
with gr.Row():
|
79 |
with gr.Column():
|
|
|
99 |
|
100 |
demo.launch()
|
101 |
|
|
|
|