Manireddy1508 commited on
Commit
52b4e6d
Β·
verified Β·
1 Parent(s): 1a69598

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -20
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 (rembg)
32
  # ----------------------------
33
  def auto_generate_mask(image: Image.Image) -> Image.Image:
34
- print("πŸͺ„ Generating mask using rembg")
35
- no_bg = remove(image)
36
- mask = Image.open(BytesIO(no_bg)).convert("L")
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
- # 🧠 Step 1: Brain Layer
47
  reasoning_json = extract_scene_plan(prompt)
48
  print("🧠 Scene plan extracted:", reasoning_json)
49
 
50
- # Step 2: Resize & Prepare Inputs
51
  image = image.resize((512, 512)).convert("RGB")
52
  mask = auto_generate_mask(image).resize((512, 512)).convert("L")
53
 
54
- results = []
55
  for i in range(num_variations):
56
- print(f"🎨 Generating variation {i + 1}...")
57
- output = pipe(
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
- ).images[0]
65
- results.append(output)
 
66
 
67
- return results, reasoning_json
68
 
69
  except Exception as e:
70
- print("❌ Error during generation:", e)
71
- return ["❌ Generation failed"], {"error": str(e)}
72
 
73
  # ----------------------------
74
- # πŸ–ΌοΈ Gradio UI
75
  # ----------------------------
76
  with gr.Blocks() as demo:
77
- gr.Markdown("## 🧠 NewCrux AI - Auto Inpainting Demo\nUpload a product image, describe your vision, and we’ll do the magic πŸͺ„")
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