Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,53 @@
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import spaces
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
from diffusers import Flux2KleinKVPipeline
|
| 7 |
from transformers import T5EncoderModel, BitsAndBytesConfig
|
| 8 |
|
| 9 |
-
#
|
| 10 |
model_id = "black-forest-labs/FLUX.2-klein-9b-kv-fp8"
|
| 11 |
|
| 12 |
def load_pipeline():
|
| 13 |
-
# Quantize T5 to save VRAM and speed up loading
|
| 14 |
quant_config = BitsAndBytesConfig(load_in_8bit=True)
|
| 15 |
text_encoder_2 = T5EncoderModel.from_pretrained(
|
| 16 |
model_id, subfolder="text_encoder_2", quantization_config=quant_config
|
| 17 |
)
|
| 18 |
-
|
| 19 |
pipe = Flux2KleinKVPipeline.from_pretrained(
|
| 20 |
model_id, text_encoder_2=text_encoder_2, torch_dtype=torch.bfloat16
|
| 21 |
)
|
|
|
|
| 22 |
return pipe
|
| 23 |
|
| 24 |
pipe = load_pipeline()
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
@spaces.GPU(duration=
|
| 28 |
-
def
|
| 29 |
-
if not input_files:
|
| 30 |
return []
|
| 31 |
|
| 32 |
-
results = []
|
| 33 |
-
# Move pipe to GPU only during this function call
|
| 34 |
pipe.to("cuda")
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
init_image = Image.open(file_path).convert("RGB").resize((768, 768))
|
| 38 |
|
|
|
|
| 39 |
output = pipe(
|
| 40 |
-
prompt=
|
| 41 |
image=init_image,
|
| 42 |
strength=strength,
|
| 43 |
num_inference_steps=steps,
|
|
@@ -46,31 +56,41 @@ def generate_tech_packs(input_files, prompt, strength, steps):
|
|
| 46 |
height=768,
|
| 47 |
).images[0]
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
with gr.Blocks(theme=gr.themes.
|
| 55 |
-
gr.Markdown("#
|
| 56 |
-
gr.Markdown("Upload up to 40 images to generate redesigned tech pack variations.")
|
| 57 |
|
| 58 |
with gr.Row():
|
| 59 |
with gr.Column():
|
| 60 |
-
input_imgs = gr.File(label="Upload Images", file_count="multiple", type="filepath")
|
| 61 |
-
prompt_box = gr.Textbox(label="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
with gr.Row():
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
with gr.Column():
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
fn=
|
| 72 |
-
inputs=[input_imgs, prompt_box,
|
| 73 |
-
outputs=
|
| 74 |
)
|
| 75 |
|
| 76 |
demo.queue().launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import spaces
|
| 4 |
+
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 |
+
# --- MODEL INITIALIZATION ---
|
| 11 |
model_id = "black-forest-labs/FLUX.2-klein-9b-kv-fp8"
|
| 12 |
|
| 13 |
def load_pipeline():
|
|
|
|
| 14 |
quant_config = BitsAndBytesConfig(load_in_8bit=True)
|
| 15 |
text_encoder_2 = T5EncoderModel.from_pretrained(
|
| 16 |
model_id, subfolder="text_encoder_2", quantization_config=quant_config
|
| 17 |
)
|
|
|
|
| 18 |
pipe = Flux2KleinKVPipeline.from_pretrained(
|
| 19 |
model_id, text_encoder_2=text_encoder_2, torch_dtype=torch.bfloat16
|
| 20 |
)
|
| 21 |
+
pipe.enable_model_cpu_offload()
|
| 22 |
return pipe
|
| 23 |
|
| 24 |
pipe = load_pipeline()
|
| 25 |
|
| 26 |
+
# --- CORE LOGIC ---
|
| 27 |
+
@spaces.GPU(duration=180) # Increased duration for batch processing
|
| 28 |
+
def process_batch(input_files, base_prompt, selected_colors, selected_sizes, strength, steps):
|
| 29 |
+
if not input_files or not selected_colors or not selected_sizes:
|
| 30 |
return []
|
| 31 |
|
|
|
|
|
|
|
| 32 |
pipe.to("cuda")
|
| 33 |
+
generated_images = []
|
| 34 |
|
| 35 |
+
# Process up to 40 images
|
| 36 |
+
for i, file_path in enumerate(input_files[:40]):
|
| 37 |
+
# 1. Randomly pick a combination from user's selection
|
| 38 |
+
color = random.choice(selected_colors)
|
| 39 |
+
size = random.choice(selected_sizes)
|
| 40 |
+
|
| 41 |
+
# 2. Construct dynamic prompt
|
| 42 |
+
# Example: "A tech pack for a jacket, color: Indigo, size: Medium"
|
| 43 |
+
final_prompt = f"{base_prompt}, high-quality tech pack, color: {color}, fit: {size} size"
|
| 44 |
+
|
| 45 |
+
# 3. Load and prepare image
|
| 46 |
init_image = Image.open(file_path).convert("RGB").resize((768, 768))
|
| 47 |
|
| 48 |
+
# 4. Generate using KV-Cache for speed
|
| 49 |
output = pipe(
|
| 50 |
+
prompt=final_prompt,
|
| 51 |
image=init_image,
|
| 52 |
strength=strength,
|
| 53 |
num_inference_steps=steps,
|
|
|
|
| 56 |
height=768,
|
| 57 |
).images[0]
|
| 58 |
|
| 59 |
+
generated_images.append(output)
|
| 60 |
+
# Yield results one-by-one so the user sees progress
|
| 61 |
+
yield generated_images
|
| 62 |
|
| 63 |
+
# --- GRADIO UI ---
|
| 64 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
| 65 |
+
gr.Markdown("# 🎨 FLUX.2 Klein: Random Batch Tech-Pack Generator")
|
|
|
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
with gr.Column():
|
| 69 |
+
input_imgs = gr.File(label="Upload up to 40 Images", file_count="multiple", type="filepath")
|
| 70 |
+
prompt_box = gr.Textbox(label="Base Design Prompt", value="A professional apparel tech pack")
|
| 71 |
+
|
| 72 |
+
colors = gr.CheckboxGroup(
|
| 73 |
+
choices=["Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"],
|
| 74 |
+
value=["Blue", "Green"], label="Select Colors (VIBGYOR)"
|
| 75 |
+
)
|
| 76 |
+
sizes = gr.CheckboxGroup(
|
| 77 |
+
choices=["Small", "Medium", "Large", "Free-size"],
|
| 78 |
+
value=["Medium", "Free-size"], label="Select Sizes"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
with gr.Row():
|
| 82 |
+
strength = gr.Slider(0.1, 1.0, value=0.6, label="Redesign Strength")
|
| 83 |
+
steps = gr.Slider(1, 8, value=4, step=1, label="Steps")
|
| 84 |
+
|
| 85 |
+
btn = gr.Button("🚀 Generate Random Variations", variant="primary")
|
| 86 |
|
| 87 |
with gr.Column():
|
| 88 |
+
gallery = gr.Gallery(label="Generated Results", columns=4)
|
| 89 |
|
| 90 |
+
btn.click(
|
| 91 |
+
fn=process_batch,
|
| 92 |
+
inputs=[input_imgs, prompt_box, colors, sizes, strength, steps],
|
| 93 |
+
outputs=gallery
|
| 94 |
)
|
| 95 |
|
| 96 |
demo.queue().launch()
|