VectorV17 commited on
Commit
fd85ca1
·
verified ·
1 Parent(s): d31f4ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -28
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
- # 1. Initialization (Run once when Space starts)
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
- # 2. Inference Function (Requesting ZeroGPU)
27
- @spaces.GPU(duration=120)
28
- def generate_tech_packs(input_files, prompt, strength, steps):
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
- for file_path in input_files:
 
 
 
 
 
 
 
 
 
 
37
  init_image = Image.open(file_path).convert("RGB").resize((768, 768))
38
 
 
39
  output = pipe(
40
- prompt=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
- results.append(output)
50
-
51
- return results
52
 
53
- # 3. Gradio Interface
54
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
55
- gr.Markdown("# 👗 FLUX.2 Klein Tech-Pack Generator")
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="Redesign Prompt", placeholder="e.g., convert to a futuristic tech-wear tech pack")
 
 
 
 
 
 
 
 
 
 
62
  with gr.Row():
63
- strength_slider = gr.Slider(0.1, 1.0, value=0.6, label="Redesign Strength")
64
- steps_slider = gr.Slider(1, 8, value=4, step=1, label="Steps (4 recommended)")
65
- generate_btn = gr.Button("🚀 Generate Batch", variant="primary")
 
66
 
67
  with gr.Column():
68
- output_gallery = gr.Gallery(label="Generated Designs", columns=4)
69
 
70
- generate_btn.click(
71
- fn=generate_tech_packs,
72
- inputs=[input_imgs, prompt_box, strength_slider, steps_slider],
73
- outputs=output_gallery
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()