Update app.py
Browse files
app.py
CHANGED
@@ -179,67 +179,66 @@ def randomize_loras(selected_indices, loras_state):
|
|
179 |
return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, random_prompt
|
180 |
|
181 |
def add_custom_lora(custom_lora, selected_indices, current_loras, gallery):
|
|
|
182 |
if custom_lora:
|
183 |
try:
|
184 |
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
|
185 |
print(f"Loaded custom LoRA: {repo}")
|
186 |
-
existing_item_index = next(
|
|
|
|
|
|
|
187 |
if existing_item_index is None:
|
|
|
188 |
if repo.endswith(".safetensors") and repo.startswith("http"):
|
189 |
repo = download_file(repo)
|
|
|
190 |
new_item = {
|
191 |
-
"image": image
|
192 |
"title": title,
|
193 |
"repo": repo,
|
194 |
"weights": path,
|
195 |
-
"trigger_word": trigger_word
|
196 |
}
|
197 |
-
print(f"New LoRA: {new_item}")
|
198 |
-
existing_item_index = len(current_loras)
|
199 |
current_loras.append(new_item)
|
200 |
-
|
201 |
-
|
|
|
202 |
gallery_items = [(item["image"], item["title"]) for item in current_loras]
|
203 |
-
# Update selected_indices if there's room
|
204 |
if len(selected_indices) < 2:
|
205 |
selected_indices.append(existing_item_index)
|
206 |
-
|
207 |
-
gr.Warning("You can select up to 2 LoRAs, remove one to select a new one.")
|
208 |
-
|
209 |
-
# Update selected_info and images
|
210 |
-
selected_info_1 = "Select a LoRA 1"
|
211 |
-
selected_info_2 = "Select a LoRA 2"
|
212 |
-
lora_scale_1 = 1.15
|
213 |
-
lora_scale_2 = 1.15
|
214 |
-
lora_image_1 = None
|
215 |
-
lora_image_2 = None
|
216 |
-
if len(selected_indices) >= 1:
|
217 |
-
lora1 = current_loras[selected_indices[0]]
|
218 |
-
selected_info_1 = f"### LoRA 1 Selected: {lora1['title']} ✨"
|
219 |
-
lora_image_1 = lora1['image'] if lora1['image'] else None
|
220 |
-
if len(selected_indices) >= 2:
|
221 |
-
lora2 = current_loras[selected_indices[1]]
|
222 |
-
selected_info_2 = f"### LoRA 2 Selected: {lora2['title']} ✨"
|
223 |
-
lora_image_2 = lora2['image'] if lora2['image'] else None
|
224 |
-
print("Finished adding custom LoRA")
|
225 |
return (
|
226 |
current_loras,
|
227 |
gr.update(value=gallery_items),
|
228 |
-
|
229 |
-
selected_info_2,
|
230 |
-
selected_indices,
|
231 |
-
lora_scale_1,
|
232 |
-
lora_scale_2,
|
233 |
-
lora_image_1,
|
234 |
-
lora_image_2
|
235 |
)
|
236 |
except Exception as e:
|
237 |
-
print(e)
|
238 |
-
gr.
|
239 |
-
return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
|
240 |
else:
|
241 |
return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
|
242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
def remove_custom_lora(selected_indices, current_loras, gallery):
|
244 |
if current_loras:
|
245 |
custom_lora_repo = current_loras[-1]['repo']
|
|
|
179 |
return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, random_prompt
|
180 |
|
181 |
def add_custom_lora(custom_lora, selected_indices, current_loras, gallery):
|
182 |
+
"""Add a custom LoRA to the current loras and update the gallery."""
|
183 |
if custom_lora:
|
184 |
try:
|
185 |
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
|
186 |
print(f"Loaded custom LoRA: {repo}")
|
187 |
+
existing_item_index = next(
|
188 |
+
(index for index, item in enumerate(current_loras) if item['repo'] == repo),
|
189 |
+
None
|
190 |
+
)
|
191 |
if existing_item_index is None:
|
192 |
+
# Download and save LoRA if necessary
|
193 |
if repo.endswith(".safetensors") and repo.startswith("http"):
|
194 |
repo = download_file(repo)
|
195 |
+
# Add the new LoRA to the current list
|
196 |
new_item = {
|
197 |
+
"image": image or "/path/to/valid/default_image.png", # Update this path
|
198 |
"title": title,
|
199 |
"repo": repo,
|
200 |
"weights": path,
|
201 |
+
"trigger_word": trigger_word,
|
202 |
}
|
|
|
|
|
203 |
current_loras.append(new_item)
|
204 |
+
existing_item_index = len(current_loras) - 1
|
205 |
+
|
206 |
+
# Update gallery and indices
|
207 |
gallery_items = [(item["image"], item["title"]) for item in current_loras]
|
|
|
208 |
if len(selected_indices) < 2:
|
209 |
selected_indices.append(existing_item_index)
|
210 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
return (
|
212 |
current_loras,
|
213 |
gr.update(value=gallery_items),
|
214 |
+
*update_selected_info(selected_indices, current_loras),
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
)
|
216 |
except Exception as e:
|
217 |
+
print(f"Error: {e}")
|
218 |
+
raise gr.Error(f"Failed to load custom LoRA: {str(e)}")
|
|
|
219 |
else:
|
220 |
return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
|
221 |
|
222 |
+
def update_selected_info(selected_indices, loras_state):
|
223 |
+
"""Update selection info and images for the UI."""
|
224 |
+
selected_info_1 = "Select a LoRA 1"
|
225 |
+
selected_info_2 = "Select a LoRA 2"
|
226 |
+
lora_scale_1 = 1.15
|
227 |
+
lora_scale_2 = 1.15
|
228 |
+
lora_image_1 = "/path/to/valid/default_image.png"
|
229 |
+
lora_image_2 = "/path/to/valid/default_image.png"
|
230 |
+
|
231 |
+
if len(selected_indices) >= 1:
|
232 |
+
lora1 = loras_state[selected_indices[0]]
|
233 |
+
selected_info_1 = f"### LoRA 1 Selected: {lora1['title']} ✨"
|
234 |
+
lora_image_1 = lora1.get("image", lora_image_1)
|
235 |
+
if len(selected_indices) >= 2:
|
236 |
+
lora2 = loras_state[selected_indices[1]]
|
237 |
+
selected_info_2 = f"### LoRA 2 Selected: {lora2['title']} ✨"
|
238 |
+
lora_image_2 = lora2.get("image", lora_image_2)
|
239 |
+
|
240 |
+
return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2
|
241 |
+
|
242 |
def remove_custom_lora(selected_indices, current_loras, gallery):
|
243 |
if current_loras:
|
244 |
custom_lora_repo = current_loras[-1]['repo']
|