Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,143 +5,110 @@ import random
|
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
| 7 |
from diffusers import Flux2KleinKVPipeline
|
| 8 |
-
from transformers import T5EncoderModel, BitsAndBytesConfig
|
| 9 |
|
| 10 |
-
# ---
|
| 11 |
-
# Using the
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
print("⏳ Loading Pipeline from non-gated mirror...")
|
| 16 |
-
# Loading in bfloat16 is essential for T4/A100 efficiency
|
| 17 |
-
pipe = Flux2KleinKVPipeline.from_pretrained(
|
| 18 |
-
model_id,
|
| 19 |
-
torch_dtype=torch.bfloat16
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
# This ensures the model stays off the GPU until the actual 'generate' call
|
| 23 |
-
pipe.enable_model_cpu_offload()
|
| 24 |
-
return pipe
|
| 25 |
-
|
| 26 |
-
# Initialize the pipeline globally
|
| 27 |
-
try:
|
| 28 |
-
pipe = load_pipeline()
|
| 29 |
-
except Exception as e:
|
| 30 |
-
print(f"Error loading pipeline: {e}")
|
| 31 |
-
pipe = None
|
| 32 |
-
|
| 33 |
-
# --- COLOR & SIZE OPTIONS ---
|
| 34 |
-
COLOR_OPTIONS = ["Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"]
|
| 35 |
-
SIZE_OPTIONS = ["Small", "Medium", "Large", "Free-size"]
|
| 36 |
-
|
| 37 |
-
# --- CORE LOGIC ---
|
| 38 |
@spaces.GPU(duration=180)
|
| 39 |
-
def
|
| 40 |
if not input_files or not selected_colors or not selected_sizes:
|
| 41 |
-
return None, "
|
| 42 |
-
|
| 43 |
-
if pipe is None:
|
| 44 |
-
return None, "Pipe not initialized."
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
final_prompt = f"{base_prompt}, apparel tech pack design, color: {color}, fit: {size} size, technical drawing, market-ready standard presentation, industry consumer trend style"
|
| 69 |
-
|
| 70 |
-
init_image = Image.open(source_file).convert("RGB").resize((768, 768))
|
| 71 |
|
| 72 |
-
#
|
| 73 |
output = pipe(
|
| 74 |
-
prompt=
|
| 75 |
image=init_image,
|
| 76 |
strength=strength,
|
| 77 |
-
num_inference_steps=steps,
|
| 78 |
guidance_scale=3.5,
|
| 79 |
width=768,
|
| 80 |
height=768,
|
| 81 |
).images[0]
|
| 82 |
|
| 83 |
-
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
composite_image = Image.new("RGB", (combined_width, grid_h * 512), (255, 255, 255))
|
| 92 |
|
| 93 |
-
for i, img in enumerate(
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
pipe.enable_model_cpu_offload()
|
| 99 |
|
| 100 |
-
return composite_image,
|
| 101 |
|
| 102 |
-
# --- GRADIO
|
| 103 |
-
with gr.Blocks(theme=gr.themes.
|
| 104 |
-
gr.Markdown("# 👗
|
| 105 |
-
gr.Markdown("Upload your input sketches, describe the base style, and select the colors/sizes to generate random variations combined into a single downloadable image.")
|
| 106 |
|
| 107 |
with gr.Row():
|
| 108 |
with gr.Column():
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
# Input image(s) gallery component
|
| 113 |
-
input_imgs = gr.File(label="Upload Source Images (up to 40)", file_count="multiple", type="filepath")
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
-
|
| 126 |
-
num_var = gr.Slider(5, 8, value=5, step=1, label="Number of Variations required (5-8)")
|
| 127 |
|
| 128 |
with gr.Accordion("Advanced Settings", open=False):
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
btn = gr.Button("🚀 Generate Batch Tech Packs", variant="primary")
|
| 135 |
|
| 136 |
with gr.Column():
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
log_box = gr.Textbox(label="Status Logs")
|
| 140 |
|
| 141 |
-
|
| 142 |
-
fn=
|
| 143 |
-
inputs=[input_imgs,
|
| 144 |
-
outputs=[
|
| 145 |
)
|
| 146 |
|
| 147 |
-
demo.
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
| 7 |
from diffusers import Flux2KleinKVPipeline
|
|
|
|
| 8 |
|
| 9 |
+
# --- CONFIGURATION ---
|
| 10 |
+
# Using the optimized Klein model
|
| 11 |
+
MODEL_ID = "black-forest-labs/FLUX.2-klein-9b-kv"
|
| 12 |
|
| 13 |
+
# --- CORE GENERATION LOGIC ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@spaces.GPU(duration=180)
|
| 15 |
+
def generate_batch(input_files, base_prompt, selected_colors, selected_sizes, num_variations, strength, steps):
|
| 16 |
if not input_files or not selected_colors or not selected_sizes:
|
| 17 |
+
return None, "Please upload images and select at least one color and size."
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# 1. Initialize Pipeline INSIDE the GPU function to avoid CPU RAM limits
|
| 20 |
+
# This prevents the "Pipe not initialized" error
|
| 21 |
+
try:
|
| 22 |
+
pipe = Flux2KleinKVPipeline.from_pretrained(
|
| 23 |
+
MODEL_ID,
|
| 24 |
+
torch_dtype=torch.bfloat16
|
| 25 |
+
).to("cuda")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return None, f"Model loading failed: {e}. Ensure you have accepted the license on the model card."
|
| 28 |
|
| 29 |
+
# 2. Permutation & Logic
|
| 30 |
+
possible_combinations = [(c, s) for c in selected_colors for s in selected_sizes]
|
| 31 |
|
| 32 |
+
# Selection logic: Picking the best combinations based on the requested count
|
| 33 |
+
# We prioritize variety in colors first
|
| 34 |
+
random.shuffle(possible_combinations)
|
| 35 |
+
final_selections = possible_combinations[:num_variations]
|
| 36 |
+
|
| 37 |
+
individual_images = []
|
| 38 |
|
| 39 |
+
for color, size in final_selections:
|
| 40 |
+
# Pick a random image from the uploaded batch
|
| 41 |
+
source_img_path = random.choice(input_files)
|
| 42 |
+
init_image = Image.open(source_img_path).convert("RGB").resize((768, 768))
|
| 43 |
+
|
| 44 |
+
# Construct trend-aware prompt
|
| 45 |
+
full_prompt = f"{base_prompt}, tech pack drawing, color: {color}, size: {size}, professional apparel industry standard, flat lay mockup"
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Generate variation
|
| 48 |
output = pipe(
|
| 49 |
+
prompt=full_prompt,
|
| 50 |
image=init_image,
|
| 51 |
strength=strength,
|
| 52 |
+
num_inference_steps=steps,
|
| 53 |
guidance_scale=3.5,
|
| 54 |
width=768,
|
| 55 |
height=768,
|
| 56 |
).images[0]
|
| 57 |
|
| 58 |
+
individual_images.append(output)
|
| 59 |
|
| 60 |
+
# 3. Create Single Composite Image (Grid Layout)
|
| 61 |
+
# We create a canvas that fits 5-8 images (4 columns x 2 rows max)
|
| 62 |
+
cols = 4
|
| 63 |
+
rows = (num_variations + cols - 1) // cols
|
| 64 |
+
canvas_w = 2048
|
| 65 |
+
canvas_h = rows * 512
|
| 66 |
|
| 67 |
+
composite_image = Image.new("RGB", (canvas_w, canvas_h), (255, 255, 255))
|
|
|
|
| 68 |
|
| 69 |
+
for i, img in enumerate(individual_images):
|
| 70 |
+
img_res = img.resize((512, 512))
|
| 71 |
+
x = (i % cols) * 512
|
| 72 |
+
y = (i // cols) * 512
|
| 73 |
+
composite_image.paste(img_res, (x, y))
|
|
|
|
| 74 |
|
| 75 |
+
return composite_image, f"Generated {num_variations} variations using market-standard combinations."
|
| 76 |
|
| 77 |
+
# --- GRADIO INTERFACE ---
|
| 78 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
| 79 |
+
gr.Markdown("# 👗 Automated Tech-Pack Batch Generator")
|
|
|
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
with gr.Column():
|
| 83 |
+
prompt = gr.Textbox(label="Text Prompt", value="high quality tech pack images")
|
| 84 |
+
input_imgs = gr.File(label="Input Images (Upload up to 40)", file_count="multiple", type="filepath")
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
with gr.Row():
|
| 87 |
+
colors = gr.CheckboxGroup(
|
| 88 |
+
choices=["Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"],
|
| 89 |
+
value=["Blue", "Green", "Red"], label="Colors (VIBGYOR)"
|
| 90 |
+
)
|
| 91 |
+
sizes = gr.CheckboxGroup(
|
| 92 |
+
choices=["Small", "Medium", "Large", "Free-size"],
|
| 93 |
+
value=["Medium", "Free-size"], label="Sizes"
|
| 94 |
+
)
|
| 95 |
|
| 96 |
+
num_var = gr.Slider(5, 8, value=5, step=1, label="Number of Variations Required")
|
|
|
|
| 97 |
|
| 98 |
with gr.Accordion("Advanced Settings", open=False):
|
| 99 |
+
strength = gr.Slider(0.1, 1.0, value=0.6, label="Redesign Strength")
|
| 100 |
+
steps = gr.Slider(1, 10, value=4, step=1, label="Inference Steps")
|
| 101 |
+
|
| 102 |
+
run_btn = gr.Button("🚀 Generate Batch", variant="primary")
|
|
|
|
|
|
|
| 103 |
|
| 104 |
with gr.Column():
|
| 105 |
+
output_img = gr.Image(label="Combined Tech-Pack Variations", type="pil")
|
| 106 |
+
status_log = gr.Textbox(label="Status Logs")
|
|
|
|
| 107 |
|
| 108 |
+
run_btn.click(
|
| 109 |
+
fn=generate_batch,
|
| 110 |
+
inputs=[input_imgs, prompt, colors, sizes, num_var, strength, steps],
|
| 111 |
+
outputs=[output_img, status_log]
|
| 112 |
)
|
| 113 |
|
| 114 |
+
demo.launch()
|