Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -89,144 +89,70 @@ def calculate_optimal_dimensions(image):
|
|
| 89 |
return width, height
|
| 90 |
|
| 91 |
|
| 92 |
-
|
| 93 |
-
def inpaint(
|
| 94 |
-
image,
|
| 95 |
-
mask,
|
| 96 |
-
prompt="",
|
| 97 |
-
seed=0,
|
| 98 |
-
num_inference_steps=28,
|
| 99 |
-
guidance_scale=50,
|
| 100 |
-
flux_keywords: List[str] = None,
|
| 101 |
-
loras: List[LoRA] = None
|
| 102 |
-
):
|
| 103 |
-
"""
|
| 104 |
-
Runs inpainting with selected LoRAs and their keywords.
|
| 105 |
-
"""
|
| 106 |
-
|
| 107 |
-
# Step 1: Reset LoRAs
|
| 108 |
-
deactivate_loras(pipe)
|
| 109 |
-
|
| 110 |
-
# Step 2: Prepare selected LoRAs and load them
|
| 111 |
-
selected_loras = {}
|
| 112 |
-
for lora in loras:
|
| 113 |
-
if lora.url:
|
| 114 |
-
selected_loras[lora.url] = 1.0 # Default weight, could be made configurable
|
| 115 |
-
|
| 116 |
-
if selected_loras:
|
| 117 |
-
activate_loras(pipe, selected_loras)
|
| 118 |
-
|
| 119 |
-
print("ACTIVE ADAPTERS:", pipe.get_active_adapters())
|
| 120 |
-
|
| 121 |
-
# Step 3: Prepare prompt
|
| 122 |
-
image = image.convert("RGB")
|
| 123 |
-
mask = mask.convert("L")
|
| 124 |
-
width, height = calculate_optimal_dimensions(image)
|
| 125 |
-
|
| 126 |
-
final_prompt = ""
|
| 127 |
-
|
| 128 |
-
# Add selected flux keywords
|
| 129 |
-
for keyword in flux_keywords:
|
| 130 |
-
final_prompt += f"{keyword}, "
|
| 131 |
-
|
| 132 |
-
# Add keywords from active LoRAs
|
| 133 |
-
for lora in loras:
|
| 134 |
-
if lora.Keywords:
|
| 135 |
-
keywords_str = ", ".join(lora.Keywords)
|
| 136 |
-
final_prompt += f"{keywords_str}, "
|
| 137 |
-
|
| 138 |
-
if prompt:
|
| 139 |
-
final_prompt += "\n\n"
|
| 140 |
-
final_prompt += prompt
|
| 141 |
-
|
| 142 |
-
# Step 4: Seed handling
|
| 143 |
-
if not isinstance(seed, int) or seed <= 0:
|
| 144 |
-
seed = random.randint(0, MAX_SEED)
|
| 145 |
-
|
| 146 |
-
# Step 5: Run pipeline
|
| 147 |
-
result = pipe(
|
| 148 |
-
image=image,
|
| 149 |
-
mask_image=mask,
|
| 150 |
-
prompt=final_prompt,
|
| 151 |
-
width=width,
|
| 152 |
-
height=height,
|
| 153 |
-
num_inference_steps=num_inference_steps,
|
| 154 |
-
guidance_scale=guidance_scale,
|
| 155 |
-
generator=torch.Generator().manual_seed(seed)
|
| 156 |
-
).images[0]
|
| 157 |
-
|
| 158 |
-
return result.convert("RGBA"), final_prompt, seed
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
def process_ui_inputs(*ui_args):
|
| 162 |
"""
|
| 163 |
-
|
|
|
|
| 164 |
"""
|
| 165 |
-
# Extract main inputs
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
prompt = ui_args[2]
|
| 169 |
-
seed = ui_args[3]
|
| 170 |
-
num_inference_steps = ui_args[4]
|
| 171 |
-
guidance_scale = ui_args[5]
|
| 172 |
-
|
| 173 |
# Extract flux keyword selections
|
| 174 |
num_flux_keywords = len(flux_keywords_available)
|
| 175 |
flux_keyword_selections = ui_args[6:6 + num_flux_keywords]
|
| 176 |
-
|
| 177 |
# Extract LoRA selections and weights
|
| 178 |
lora_args_start = 6 + num_flux_keywords
|
| 179 |
lora_args = ui_args[lora_args_start:]
|
| 180 |
-
|
| 181 |
# Process selected flux keywords
|
| 182 |
selected_flux_keywords = []
|
| 183 |
for i, selected in enumerate(flux_keyword_selections):
|
| 184 |
if selected:
|
| 185 |
selected_flux_keywords.append(flux_keywords_available[i])
|
| 186 |
-
|
| 187 |
# Process selected LoRAs with weights
|
| 188 |
-
|
| 189 |
for i, lora_config in enumerate(loras):
|
| 190 |
checkbox_idx = i * 2
|
| 191 |
weight_idx = i * 2 + 1
|
| 192 |
-
|
| 193 |
if checkbox_idx < len(lora_args):
|
| 194 |
checked = lora_args[checkbox_idx]
|
| 195 |
weight = lora_args[weight_idx] if weight_idx < len(lora_args) else 0.5
|
| 196 |
-
|
| 197 |
if checked:
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
nombre=lora_config.nombre,
|
| 202 |
-
title=lora_config.title,
|
| 203 |
-
url=lora_config.url,
|
| 204 |
-
Keywords=lora_config.Keywords,
|
| 205 |
-
note=lora_config.note
|
| 206 |
-
)
|
| 207 |
-
# Store weight in a way that can be used by activate_loras
|
| 208 |
-
selected_loras.append((lora_copy, weight))
|
| 209 |
-
|
| 210 |
-
return inpaint_with_weights(
|
| 211 |
-
image, mask, prompt, seed, num_inference_steps, guidance_scale,
|
| 212 |
-
selected_flux_keywords, selected_loras
|
| 213 |
-
)
|
| 214 |
|
| 215 |
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
):
|
| 220 |
"""
|
| 221 |
-
|
| 222 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
# Step 1: Reset LoRAs
|
| 224 |
deactivate_loras(pipe)
|
| 225 |
|
| 226 |
# Step 2: Prepare selected LoRAs and load them
|
| 227 |
selected_loras = {}
|
| 228 |
active_loras = []
|
| 229 |
-
|
| 230 |
for lora, weight in loras_with_weights:
|
| 231 |
if lora.url:
|
| 232 |
selected_loras[lora.url] = round(weight, 1)
|
|
@@ -277,6 +203,26 @@ def inpaint_with_weights(
|
|
| 277 |
return result.convert("RGBA"), final_prompt, seed
|
| 278 |
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
def toggle_input(checked, current_value):
|
| 281 |
"""
|
| 282 |
Enables or disables the Number input based on checkbox status.
|
|
@@ -347,7 +293,7 @@ def create_lora_components():
|
|
| 347 |
|
| 348 |
|
| 349 |
# Create main interface
|
| 350 |
-
with gr.Blocks(title="Flux.1 Fill dev Inpainting with LoRAs
|
| 351 |
with gr.Row():
|
| 352 |
with gr.Column(scale=2):
|
| 353 |
prompt_input = gr.Text(
|
|
@@ -402,7 +348,7 @@ with gr.Blocks(title="Flux.1 Fill dev Inpainting with LoRAs from JSON", theme=gr
|
|
| 402 |
|
| 403 |
if all_components:
|
| 404 |
run_btn.click(
|
| 405 |
-
|
| 406 |
inputs=[
|
| 407 |
image_input,
|
| 408 |
mask_input,
|
|
|
|
| 89 |
return width, height
|
| 90 |
|
| 91 |
|
| 92 |
+
def parse_ui_inputs(*ui_args):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
"""
|
| 94 |
+
Parses UI inputs and extracts flux keywords and LoRAs with weights.
|
| 95 |
+
Returns: (main_inputs, selected_flux_keywords, selected_loras_with_weights)
|
| 96 |
"""
|
| 97 |
+
# Extract main inputs (first 6 arguments)
|
| 98 |
+
main_inputs = ui_args[:6] # image, mask, prompt, seed, steps, guidance
|
| 99 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# Extract flux keyword selections
|
| 101 |
num_flux_keywords = len(flux_keywords_available)
|
| 102 |
flux_keyword_selections = ui_args[6:6 + num_flux_keywords]
|
| 103 |
+
|
| 104 |
# Extract LoRA selections and weights
|
| 105 |
lora_args_start = 6 + num_flux_keywords
|
| 106 |
lora_args = ui_args[lora_args_start:]
|
| 107 |
+
|
| 108 |
# Process selected flux keywords
|
| 109 |
selected_flux_keywords = []
|
| 110 |
for i, selected in enumerate(flux_keyword_selections):
|
| 111 |
if selected:
|
| 112 |
selected_flux_keywords.append(flux_keywords_available[i])
|
| 113 |
+
|
| 114 |
# Process selected LoRAs with weights
|
| 115 |
+
selected_loras_with_weights = []
|
| 116 |
for i, lora_config in enumerate(loras):
|
| 117 |
checkbox_idx = i * 2
|
| 118 |
weight_idx = i * 2 + 1
|
| 119 |
+
|
| 120 |
if checkbox_idx < len(lora_args):
|
| 121 |
checked = lora_args[checkbox_idx]
|
| 122 |
weight = lora_args[weight_idx] if weight_idx < len(lora_args) else 0.5
|
| 123 |
+
|
| 124 |
if checked:
|
| 125 |
+
selected_loras_with_weights.append((lora_config, weight))
|
| 126 |
+
|
| 127 |
+
return main_inputs, selected_flux_keywords, selected_loras_with_weights
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
+
@spaces.GPU(duration=30)
|
| 131 |
+
def inpaint(
|
| 132 |
+
image,
|
| 133 |
+
mask,
|
| 134 |
+
prompt="",
|
| 135 |
+
seed=0,
|
| 136 |
+
num_inference_steps=28,
|
| 137 |
+
guidance_scale=50,
|
| 138 |
+
flux_keywords: List[str] = None,
|
| 139 |
+
loras_with_weights: List[tuple] = None # List of (LoRA, weight) tuples
|
| 140 |
):
|
| 141 |
"""
|
| 142 |
+
Main inpainting function with selected LoRAs and their keywords.
|
| 143 |
"""
|
| 144 |
+
if flux_keywords is None:
|
| 145 |
+
flux_keywords = []
|
| 146 |
+
if loras_with_weights is None:
|
| 147 |
+
loras_with_weights = []
|
| 148 |
+
|
| 149 |
# Step 1: Reset LoRAs
|
| 150 |
deactivate_loras(pipe)
|
| 151 |
|
| 152 |
# Step 2: Prepare selected LoRAs and load them
|
| 153 |
selected_loras = {}
|
| 154 |
active_loras = []
|
| 155 |
+
|
| 156 |
for lora, weight in loras_with_weights:
|
| 157 |
if lora.url:
|
| 158 |
selected_loras[lora.url] = round(weight, 1)
|
|
|
|
| 203 |
return result.convert("RGBA"), final_prompt, seed
|
| 204 |
|
| 205 |
|
| 206 |
+
def inpaint_ui_wrapper(*ui_args):
|
| 207 |
+
"""
|
| 208 |
+
UI wrapper that processes Gradio inputs and calls the main inpaint function.
|
| 209 |
+
"""
|
| 210 |
+
main_inputs, selected_flux_keywords, selected_loras_with_weights = parse_ui_inputs(*ui_args)
|
| 211 |
+
|
| 212 |
+
image, mask, prompt, seed, num_inference_steps, guidance_scale = main_inputs
|
| 213 |
+
|
| 214 |
+
return inpaint(
|
| 215 |
+
image=image,
|
| 216 |
+
mask=mask,
|
| 217 |
+
prompt=prompt,
|
| 218 |
+
seed=seed,
|
| 219 |
+
num_inference_steps=num_inference_steps,
|
| 220 |
+
guidance_scale=guidance_scale,
|
| 221 |
+
flux_keywords=selected_flux_keywords,
|
| 222 |
+
loras_with_weights=selected_loras_with_weights
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
|
| 226 |
def toggle_input(checked, current_value):
|
| 227 |
"""
|
| 228 |
Enables or disables the Number input based on checkbox status.
|
|
|
|
| 293 |
|
| 294 |
|
| 295 |
# Create main interface
|
| 296 |
+
with gr.Blocks(title="Flux.1 Fill dev Inpainting with LoRAs", theme=gr.themes.Soft()) as demo:
|
| 297 |
with gr.Row():
|
| 298 |
with gr.Column(scale=2):
|
| 299 |
prompt_input = gr.Text(
|
|
|
|
| 348 |
|
| 349 |
if all_components:
|
| 350 |
run_btn.click(
|
| 351 |
+
inpaint_ui_wrapper,
|
| 352 |
inputs=[
|
| 353 |
image_input,
|
| 354 |
mask_input,
|