Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ 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 (
|
|
@@ -48,6 +49,10 @@ def process_image(prompt, image, num_variations):
|
|
| 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}...")
|
| 53 |
result = pipe(
|
|
@@ -58,11 +63,13 @@ def process_image(prompt, image, num_variations):
|
|
| 58 |
guidance_scale=7.5,
|
| 59 |
num_inference_steps=30,
|
| 60 |
)
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
# Save
|
| 64 |
log_data = {
|
| 65 |
-
"timestamp":
|
| 66 |
"prompt": prompt,
|
| 67 |
"scene_plan": scene_plan,
|
| 68 |
"enriched_prompts": enriched_prompts,
|
|
@@ -74,47 +81,45 @@ def process_image(prompt, image, num_variations):
|
|
| 74 |
with open("logs/generation_logs.jsonl", "a") as log_file:
|
| 75 |
log_file.write(json.dumps(log_data) + "\n")
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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 |
-
|
|
|
|
|
|
|
| 113 |
|
| 114 |
generate_btn.click(
|
| 115 |
-
|
| 116 |
inputs=[prompt, input_image, num_outputs],
|
| 117 |
-
outputs=[output_gallery, output_msg,
|
| 118 |
)
|
| 119 |
|
| 120 |
# ----------------------------
|
|
|
|
| 3 |
import torch
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
+
import zipfile
|
| 7 |
from datetime import datetime
|
| 8 |
from diffusers import StableDiffusionXLImg2ImgPipeline
|
| 9 |
from utils.planner import (
|
|
|
|
| 49 |
image = image.resize((1024, 1024)).convert("RGB")
|
| 50 |
|
| 51 |
outputs = []
|
| 52 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 53 |
+
out_dir = f"outputs/session_{timestamp}"
|
| 54 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 55 |
+
|
| 56 |
for i, enriched_prompt in enumerate(enriched_prompts):
|
| 57 |
print(f"✨ Generating Image {i + 1}...")
|
| 58 |
result = pipe(
|
|
|
|
| 63 |
guidance_scale=7.5,
|
| 64 |
num_inference_steps=30,
|
| 65 |
)
|
| 66 |
+
output_img = result.images[0]
|
| 67 |
+
output_img.save(f"{out_dir}/generated_{i+1}.png")
|
| 68 |
+
outputs.append(output_img)
|
| 69 |
|
| 70 |
+
# Save log
|
| 71 |
log_data = {
|
| 72 |
+
"timestamp": timestamp,
|
| 73 |
"prompt": prompt,
|
| 74 |
"scene_plan": scene_plan,
|
| 75 |
"enriched_prompts": enriched_prompts,
|
|
|
|
| 81 |
with open("logs/generation_logs.jsonl", "a") as log_file:
|
| 82 |
log_file.write(json.dumps(log_data) + "\n")
|
| 83 |
|
| 84 |
+
# Create ZIP of outputs
|
| 85 |
+
# Handle single or multiple image download
|
| 86 |
+
if num_variations == 1:
|
| 87 |
+
single_img_path = f"{out_dir}/generated_1.png"
|
| 88 |
+
return outputs, "✅ Generated one image. Ready for download.", single_img_path
|
| 89 |
+
else:
|
| 90 |
+
zip_path = f"{out_dir}/all_images.zip"
|
| 91 |
+
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 92 |
+
for i in range(len(outputs)):
|
| 93 |
+
img_path = f"{out_dir}/generated_{i+1}.png"
|
| 94 |
+
zipf.write(img_path, os.path.basename(img_path))
|
| 95 |
+
return outputs, f"✅ Generated {num_variations} images. Download below.", zip_path
|
| 96 |
+
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
print("❌ Generation failed:", e)
|
| 100 |
+
return [Image.new("RGB", (512, 512), color="red")], f"❌ Error: {str(e)}", None
|
| 101 |
|
| 102 |
# ----------------------------
|
| 103 |
# 🧪 Gradio Interface
|
| 104 |
# ----------------------------
|
| 105 |
with gr.Blocks(title="NewCrux Image-to-Image Generator") as demo:
|
| 106 |
+
gr.Markdown("### 🖼️ NewCrux: Product Lifestyle Visual Generator (SDXL + Prompt AI)\nUpload a product image and describe the visual you want. The system will generate realistic marketing images using AI.")
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
+
prompt = gr.Textbox(label="Prompt", placeholder="e.g., A person running on the beach wearing the product")
|
| 110 |
input_image = gr.Image(type="pil", label="Product Image")
|
| 111 |
num_outputs = gr.Slider(1, 5, value=3, step=1, label="Number of Variations")
|
| 112 |
|
| 113 |
+
generate_btn = gr.Button("🚀 Generate Image(s)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
output_gallery = gr.Gallery(label="Generated Images", show_label=True, columns=[2], height="auto")
|
| 116 |
+
output_msg = gr.Textbox(label="Generation Status", interactive=False)
|
| 117 |
+
download_zip = gr.File(label="⬇️ Download All Images (.zip)", interactive=False)
|
| 118 |
|
| 119 |
generate_btn.click(
|
| 120 |
+
fn=process_image,
|
| 121 |
inputs=[prompt, input_image, num_outputs],
|
| 122 |
+
outputs=[output_gallery, output_msg, download_zip]
|
| 123 |
)
|
| 124 |
|
| 125 |
# ----------------------------
|