Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
#
|
15 |
# ----------------------------
|
16 |
-
|
17 |
-
|
18 |
-
dtype = torch.float16 if use_gpu else torch.float32
|
19 |
|
20 |
# ----------------------------
|
21 |
-
# π§ Load
|
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 |
-
|
|
|
|
|
31 |
pipe.enable_attention_slicing()
|
32 |
|
33 |
# ----------------------------
|
34 |
-
#
|
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
except Exception as e:
|
74 |
print("β Generation failed:", e)
|
75 |
-
return [Image.new("RGB", (512, 512), color="red")]
|
76 |
|
77 |
# ----------------------------
|
78 |
-
#
|
79 |
# ----------------------------
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
gr.
|
85 |
-
gr.
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
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 |
+
|