Manireddy1508 commited on
Commit
55c7101
Β·
verified Β·
1 Parent(s): 054edad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -36
app.py CHANGED
@@ -1,8 +1,9 @@
1
- # app.py
2
-
3
  import gradio as gr
4
  from PIL import Image
5
  import torch
 
 
 
6
  from diffusers import StableDiffusionXLImg2ImgPipeline
7
  from utils.planner import (
8
  extract_scene_plan,
@@ -11,50 +12,41 @@ from utils.planner import (
11
  )
12
 
13
  # ----------------------------
14
- # πŸ–₯️ Device Setup
15
  # ----------------------------
16
- use_gpu = torch.cuda.is_available()
17
- device = "cuda" if use_gpu else "cpu"
18
- dtype = torch.float16 if use_gpu else torch.float32
19
 
20
  # ----------------------------
21
- # 🧠 Load SDXL Img2Img Pipeline (no ControlNet)
22
  # ----------------------------
23
  pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
24
  "stabilityai/stable-diffusion-xl-base-1.0",
25
  torch_dtype=dtype,
26
- variant="fp16" if use_gpu 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("🧠 Prompt received:", prompt)
39
  if image is None:
40
  raise ValueError("🚫 Please upload an image.")
41
 
42
- # Step 1: Analyze image + prompt β†’ structured plan
43
  scene_plan = extract_scene_plan(prompt, image)
44
- print("πŸ“‹ Scene Plan:", scene_plan)
45
-
46
- # Step 2: Generate prompt variations
47
  enriched_prompts = generate_prompt_variations_from_scene(scene_plan, prompt, num_variations)
48
- print("βœ… Enriched Prompts:", enriched_prompts)
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 1024x1024
55
  image = image.resize((1024, 1024)).convert("RGB")
56
 
57
- # Step 5: Generate variations
58
  outputs = []
59
  for i, enriched_prompt in enumerate(enriched_prompts):
60
  print(f"✨ Generating Image {i + 1}...")
@@ -68,31 +60,68 @@ def process_image(prompt, image, num_variations):
68
  )
69
  outputs.append(result.images[0])
70
 
71
- return outputs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  except Exception as e:
74
  print("❌ Generation failed:", 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"),
88
- title="NewCrux Product Image Generator (SDXL Only)",
89
- description="Upload a product image and describe your vision. NewCrux will generate multiple enriched image variations using SDXL + AI prompt planning."
90
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  # ----------------------------
93
- # πŸš€ Launch
94
  # ----------------------------
95
  if __name__ == "__main__":
96
  demo.launch()
97
 
98
 
 
 
 
 
1
  import gradio as gr
2
  from PIL import Image
3
  import torch
4
+ import os
5
+ import json
6
+ from datetime import datetime
7
  from diffusers import StableDiffusionXLImg2ImgPipeline
8
  from utils.planner import (
9
  extract_scene_plan,
 
12
  )
13
 
14
  # ----------------------------
15
+ # πŸ’» Device Configuration
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 Stable Diffusion XL Img2Img Pipeline
22
  # ----------------------------
23
  pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
24
  "stabilityai/stable-diffusion-xl-base-1.0",
25
  torch_dtype=dtype,
 
26
  use_safetensors=True,
27
+ variant="fp16" if device == "cuda" else None,
28
  )
29
  pipe.to(device)
30
+
31
+ if device == "cuda":
32
+ pipe.enable_model_cpu_offload()
33
  pipe.enable_attention_slicing()
34
 
35
  # ----------------------------
36
+ # 🎨 Core Generation Function
37
  # ----------------------------
38
  def process_image(prompt, image, num_variations):
39
  try:
 
40
  if image is None:
41
  raise ValueError("🚫 Please upload an image.")
42
 
43
+ print("🧠 Prompt received:", prompt)
44
  scene_plan = extract_scene_plan(prompt, image)
 
 
 
45
  enriched_prompts = generate_prompt_variations_from_scene(scene_plan, prompt, num_variations)
 
 
 
46
  negative_prompt = generate_negative_prompt_from_scene(scene_plan)
 
47
 
 
48
  image = image.resize((1024, 1024)).convert("RGB")
49
 
 
50
  outputs = []
51
  for i, enriched_prompt in enumerate(enriched_prompts):
52
  print(f"✨ Generating Image {i + 1}...")
 
60
  )
61
  outputs.append(result.images[0])
62
 
63
+ # Save logs
64
+ log_data = {
65
+ "timestamp": datetime.now().isoformat(),
66
+ "prompt": prompt,
67
+ "scene_plan": scene_plan,
68
+ "enriched_prompts": enriched_prompts,
69
+ "negative_prompt": negative_prompt,
70
+ "device": device,
71
+ "num_variations": num_variations
72
+ }
73
+ os.makedirs("logs", exist_ok=True)
74
+ with open("logs/generation_logs.jsonl", "a") as log_file:
75
+ log_file.write(json.dumps(log_data) + "\n")
76
+
77
+ return outputs, "βœ… Generation complete. Images ready for download."
78
 
79
  except Exception as e:
80
  print("❌ Generation failed:", e)
81
+ return [Image.new("RGB", (512, 512), color="red")], f"❌ Error: {str(e)}"
82
 
83
  # ----------------------------
84
+ # πŸ§ͺ Gradio Interface
85
  # ----------------------------
86
+ with gr.Blocks(title="NewCrux Image-to-Image Generator") as demo:
87
+ gr.Markdown("### πŸ–ΌοΈ NewCrux: Product Lifestyle Visual Generator (SDXL)\nUpload a product image and describe the scene you'd like to see.")
88
+
89
+ with gr.Row():
90
+ prompt = gr.Textbox(label="Prompt")
91
+ input_image = gr.Image(type="pil", label="Product Image")
92
+ num_outputs = gr.Slider(1, 5, value=3, step=1, label="Number of Variations")
93
+
94
+ generate_btn = gr.Button("Generate Image(s)")
95
+
96
+ output_gallery = gr.Gallery(label="Generated Images", show_label=True, allow_preview=True, columns=[2], height="auto")
97
+ output_msg = gr.Textbox(label="Status", interactive=False)
98
+ download_btn = gr.File(label="Download Last Image", interactive=False)
99
+
100
+ last_images = []
101
+
102
+ def generate_and_save(prompt, image, num_outputs):
103
+ images, message = process_image(prompt, image, num_outputs)
104
+ last_images.clear()
105
+ last_images.extend(images)
106
+
107
+ # Save the last image for download
108
+ file_path = "outputs/last_generated.png"
109
+ os.makedirs("outputs", exist_ok=True)
110
+ images[0].save(file_path)
111
+
112
+ return images, message, file_path
113
+
114
+ generate_btn.click(
115
+ generate_and_save,
116
+ inputs=[prompt, input_image, num_outputs],
117
+ outputs=[output_gallery, output_msg, download_btn]
118
+ )
119
 
120
  # ----------------------------
121
+ # πŸš€ Launch App
122
  # ----------------------------
123
  if __name__ == "__main__":
124
  demo.launch()
125
 
126
 
127
+