Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,87 +5,103 @@ import random
|
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
| 7 |
from diffusers import Flux2KleinKVPipeline
|
| 8 |
-
from transformers import
|
| 9 |
|
| 10 |
# --- MODEL INITIALIZATION ---
|
|
|
|
| 11 |
model_id = "black-forest-labs/FLUX.2-klein-9b-kv-fp8"
|
| 12 |
|
| 13 |
def load_pipeline():
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
)
|
|
|
|
|
|
|
|
|
|
| 18 |
pipe = Flux2KleinKVPipeline.from_pretrained(
|
| 19 |
-
model_id,
|
|
|
|
|
|
|
| 20 |
)
|
|
|
|
|
|
|
| 21 |
pipe.enable_model_cpu_offload()
|
| 22 |
return pipe
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# --- CORE LOGIC ---
|
| 27 |
-
@spaces.GPU(duration=180)
|
| 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 |
-
#
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
color = random.choice(selected_colors)
|
| 39 |
size = random.choice(selected_sizes)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 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,
|
| 54 |
guidance_scale=3.5,
|
| 55 |
width=768,
|
| 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.
|
| 65 |
-
gr.Markdown("#
|
|
|
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
-
with gr.Column():
|
| 69 |
-
input_imgs = gr.File(label="Upload
|
| 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="
|
| 75 |
)
|
| 76 |
sizes = gr.CheckboxGroup(
|
| 77 |
choices=["Small", "Medium", "Large", "Free-size"],
|
| 78 |
-
value=["Medium", "Free-size"], label="
|
| 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
|
| 86 |
|
| 87 |
-
with gr.Column():
|
| 88 |
-
gallery = gr.Gallery(label="Generated
|
| 89 |
|
| 90 |
btn.click(
|
| 91 |
fn=process_batch,
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
| 7 |
from diffusers import Flux2KleinKVPipeline
|
| 8 |
+
from transformers import BitsAndBytesConfig
|
| 9 |
|
| 10 |
# --- MODEL INITIALIZATION ---
|
| 11 |
+
# Using the specific FP8/KV repository
|
| 12 |
model_id = "black-forest-labs/FLUX.2-klein-9b-kv-fp8"
|
| 13 |
|
| 14 |
def load_pipeline():
|
| 15 |
+
# Define 8-bit quantization for the heavy T5 text encoder
|
| 16 |
+
quant_config = BitsAndBytesConfig(
|
| 17 |
+
load_in_8bit=True,
|
| 18 |
+
bnb_8bit_compute_dtype=torch.bfloat16
|
| 19 |
)
|
| 20 |
+
|
| 21 |
+
# Load the full pipeline directly.
|
| 22 |
+
# This automatically finds the correct files in the subfolders.
|
| 23 |
pipe = Flux2KleinKVPipeline.from_pretrained(
|
| 24 |
+
model_id,
|
| 25 |
+
text_encoder_2_quantization_config=quant_config,
|
| 26 |
+
torch_dtype=torch.bfloat16
|
| 27 |
)
|
| 28 |
+
|
| 29 |
+
# Offload to CPU to keep the Space from crashing during initialization
|
| 30 |
pipe.enable_model_cpu_offload()
|
| 31 |
return pipe
|
| 32 |
|
| 33 |
+
# Initialize globally
|
| 34 |
+
try:
|
| 35 |
+
pipe = load_pipeline()
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"Error loading pipeline: {e}")
|
| 38 |
+
# Fallback for debugging if the specific KV repo has folder naming issues
|
| 39 |
+
pipe = None
|
| 40 |
|
| 41 |
# --- CORE LOGIC ---
|
| 42 |
+
@spaces.GPU(duration=180)
|
| 43 |
def process_batch(input_files, base_prompt, selected_colors, selected_sizes, strength, steps):
|
| 44 |
+
if pipe is None:
|
| 45 |
+
return "Error: Pipeline failed to load. Check logs."
|
| 46 |
if not input_files or not selected_colors or not selected_sizes:
|
| 47 |
return []
|
| 48 |
|
| 49 |
+
# Move the transformer to GPU for the duration of the ZeroGPU session
|
| 50 |
pipe.to("cuda")
|
| 51 |
generated_images = []
|
| 52 |
|
| 53 |
+
# Ensure we don't exceed the batch limit
|
| 54 |
+
files_to_process = input_files[:40]
|
| 55 |
+
|
| 56 |
+
for file_path in files_to_process:
|
| 57 |
color = random.choice(selected_colors)
|
| 58 |
size = random.choice(selected_sizes)
|
| 59 |
|
| 60 |
+
# Enhanced prompt for tech pack clarity
|
| 61 |
+
final_prompt = f"{base_prompt}, clothing tech pack, professional flat sketch, color: {color}, size: {size}, detailed labels, white background"
|
|
|
|
| 62 |
|
|
|
|
| 63 |
init_image = Image.open(file_path).convert("RGB").resize((768, 768))
|
| 64 |
|
|
|
|
| 65 |
output = pipe(
|
| 66 |
prompt=final_prompt,
|
| 67 |
image=init_image,
|
| 68 |
strength=strength,
|
| 69 |
+
num_inference_steps=int(steps),
|
| 70 |
guidance_scale=3.5,
|
| 71 |
width=768,
|
| 72 |
height=768,
|
| 73 |
).images[0]
|
| 74 |
|
| 75 |
generated_images.append(output)
|
|
|
|
| 76 |
yield generated_images
|
| 77 |
|
| 78 |
# --- GRADIO UI ---
|
| 79 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 80 |
+
gr.Markdown("# 👗 FLUX.2 Klein: Random Batch Tech-Pack Generator")
|
| 81 |
+
gr.Markdown("Upload up to 40 images. The app will randomly assign selected colors and sizes to each.")
|
| 82 |
|
| 83 |
with gr.Row():
|
| 84 |
+
with gr.Column(scale=1):
|
| 85 |
+
input_imgs = gr.File(label="Upload Images", file_count="multiple", type="filepath")
|
| 86 |
prompt_box = gr.Textbox(label="Base Design Prompt", value="A professional apparel tech pack")
|
| 87 |
|
| 88 |
colors = gr.CheckboxGroup(
|
| 89 |
choices=["Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"],
|
| 90 |
+
value=["Blue", "Green"], label="VIBGYOR Colors"
|
| 91 |
)
|
| 92 |
sizes = gr.CheckboxGroup(
|
| 93 |
choices=["Small", "Medium", "Large", "Free-size"],
|
| 94 |
+
value=["Medium", "Free-size"], label="Sizes"
|
| 95 |
)
|
| 96 |
|
| 97 |
with gr.Row():
|
| 98 |
strength = gr.Slider(0.1, 1.0, value=0.6, label="Redesign Strength")
|
| 99 |
steps = gr.Slider(1, 8, value=4, step=1, label="Steps")
|
| 100 |
|
| 101 |
+
btn = gr.Button("🚀 Generate Batch Variations", variant="primary")
|
| 102 |
|
| 103 |
+
with gr.Column(scale=2):
|
| 104 |
+
gallery = gr.Gallery(label="Generated Designs", columns=4, height="auto")
|
| 105 |
|
| 106 |
btn.click(
|
| 107 |
fn=process_batch,
|