openfree commited on
Commit
5a4874e
1 Parent(s): 28c0116

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -50
app.py CHANGED
@@ -125,23 +125,22 @@ def update_selection(evt: gr.SelectData, selected_indices, loras_state, width, h
125
  if selected_index in selected_indices:
126
  selected_indices.remove(selected_index)
127
  else:
128
- if len(selected_indices) < 3: # 변경: 2에서 3으로
129
  selected_indices.append(selected_index)
130
  else:
131
  gr.Warning("You can select up to 3 LoRAs, remove one to select a new one.")
132
  return gr.update(), gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), width, height, gr.update(), gr.update(), gr.update()
133
 
134
- selected_info_1 = gr.Markdown("LoRA 1 선택")
135
- selected_info_2 = gr.Markdown("LoRA 2 선택")
136
- selected_info_3 = gr.Markdown("LoRA 3 선택")
137
-
138
 
139
  lora_scale_1 = 1.15
140
  lora_scale_2 = 1.15
141
  lora_scale_3 = 1.15
142
  lora_image_1 = None
143
  lora_image_2 = None
144
- lora_image_2 = None
145
 
146
  if len(selected_indices) >= 1:
147
  lora1 = loras_state[selected_indices[0]]
@@ -164,63 +163,65 @@ def update_selection(evt: gr.SelectData, selected_indices, loras_state, width, h
164
 
165
  return gr.update(placeholder=new_placeholder), selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, width, height, lora_image_1, lora_image_2, lora_image_3
166
 
167
-
168
- def remove_lora_1(selected_indices, loras_state):
169
- if len(selected_indices) >= 1:
170
- selected_indices.pop(0)
171
- selected_info_1 = "Select a LoRA 1"
172
- selected_info_2 = "Select a LoRA 2"
173
- lora_scale_1 = 1.15
174
- lora_scale_2 = 1.15
175
- lora_image_1 = None
176
- lora_image_2 = None
177
- if len(selected_indices) >= 1:
178
- lora1 = loras_state[selected_indices[0]]
179
- selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
180
- lora_image_1 = lora1['image']
181
- if len(selected_indices) >= 2:
182
- lora2 = loras_state[selected_indices[1]]
183
- selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
184
- lora_image_2 = lora2['image']
185
- return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2
186
-
187
- def remove_lora_2(selected_indices, loras_state):
188
- if len(selected_indices) >= 2:
189
- selected_indices.pop(1)
190
  selected_info_1 = "Select LoRA 1"
191
  selected_info_2 = "Select LoRA 2"
 
192
  lora_scale_1 = 1.15
193
  lora_scale_2 = 1.15
 
194
  lora_image_1 = None
195
  lora_image_2 = None
196
- if len(selected_indices) >= 1:
197
- lora1 = loras_state[selected_indices[0]]
198
- selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
199
- lora_image_1 = lora1['image']
200
- if len(selected_indices) >= 2:
201
- lora2 = loras_state[selected_indices[1]]
202
- selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
203
- lora_image_2 = lora2['image']
204
- return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
 
206
  def randomize_loras(selected_indices, loras_state):
207
  try:
208
- if len(loras_state) < 2:
209
  raise gr.Error("Not enough LoRAs to randomize.")
210
- selected_indices = random.sample(range(len(loras_state)), 2)
211
  lora1 = loras_state[selected_indices[0]]
212
  lora2 = loras_state[selected_indices[1]]
 
213
  selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}](https://huggingface.co/{lora1['repo']}) ✨"
214
  selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}](https://huggingface.co/{lora2['repo']}) ✨"
 
215
  lora_scale_1 = 1.15
216
  lora_scale_2 = 1.15
 
217
  lora_image_1 = lora1['image']
218
  lora_image_2 = lora2['image']
 
219
  random_prompt = random.choice(prompt_values)
220
- return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, random_prompt
221
  except Exception as e:
222
  print(f"Error in randomize_loras: {str(e)}")
223
- return "Error", "Error", [], 1.15, 1.15, None, None, ""
224
 
225
  def add_custom_lora(custom_lora, selected_indices, current_loras):
226
  if custom_lora:
@@ -245,18 +246,21 @@ def add_custom_lora(custom_lora, selected_indices, current_loras):
245
  # Update gallery
246
  gallery_items = [(item["image"], item["title"]) for item in current_loras]
247
  # Update selected_indices if there's room
248
- if len(selected_indices) < 2:
249
  selected_indices.append(existing_item_index)
250
  else:
251
- gr.Warning("You can select up to 2 LoRAs, remove one to select a new one.")
252
 
253
  # Update selected_info and images
254
  selected_info_1 = "Select a LoRA 1"
255
  selected_info_2 = "Select a LoRA 2"
 
256
  lora_scale_1 = 1.15
257
  lora_scale_2 = 1.15
 
258
  lora_image_1 = None
259
  lora_image_2 = None
 
260
  if len(selected_indices) >= 1:
261
  lora1 = current_loras[selected_indices[0]]
262
  selected_info_1 = f"### LoRA 1 Selected: {lora1['title']} ✨"
@@ -265,24 +269,31 @@ def add_custom_lora(custom_lora, selected_indices, current_loras):
265
  lora2 = current_loras[selected_indices[1]]
266
  selected_info_2 = f"### LoRA 2 Selected: {lora2['title']} ✨"
267
  lora_image_2 = lora2['image'] if lora2['image'] else None
 
 
 
 
268
  print("Finished adding custom LoRA")
269
  return (
270
  current_loras,
271
  gr.update(value=gallery_items),
272
  selected_info_1,
273
  selected_info_2,
 
274
  selected_indices,
275
  lora_scale_1,
276
  lora_scale_2,
 
277
  lora_image_1,
278
- lora_image_2
 
279
  )
280
  except Exception as e:
281
  print(e)
282
  gr.Warning(str(e))
283
- return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
284
  else:
285
- return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
286
 
287
  def remove_custom_lora(selected_indices, current_loras):
288
  if current_loras:
@@ -298,10 +309,13 @@ def remove_custom_lora(selected_indices, current_loras):
298
  # Update selected_info and images
299
  selected_info_1 = "Select a LoRA 1"
300
  selected_info_2 = "Select a LoRA 2"
 
301
  lora_scale_1 = 1.15
302
  lora_scale_2 = 1.15
 
303
  lora_image_1 = None
304
  lora_image_2 = None
 
305
  if len(selected_indices) >= 1:
306
  lora1 = current_loras[selected_indices[0]]
307
  selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
@@ -310,16 +324,23 @@ def remove_custom_lora(selected_indices, current_loras):
310
  lora2 = current_loras[selected_indices[1]]
311
  selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
312
  lora_image_2 = lora2['image']
 
 
 
 
313
  return (
314
  current_loras,
315
  gr.update(value=gallery_items),
316
  selected_info_1,
317
  selected_info_2,
 
318
  selected_indices,
319
  lora_scale_1,
320
  lora_scale_2,
 
321
  lora_image_1,
322
- lora_image_2
 
323
  )
324
 
325
  @spaces.GPU(duration=75)
@@ -775,19 +796,21 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
775
  inputs=[selected_indices, loras_state],
776
  outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
777
  )
778
-
779
  remove_button_2.click(
780
  remove_lora_2,
781
  inputs=[selected_indices, loras_state],
782
  outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
783
  )
784
-
785
  remove_button_3.click(
786
  remove_lora_3,
787
  inputs=[selected_indices, loras_state],
788
  outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
789
  )
790
 
 
 
791
  randomize_button.click(
792
  randomize_loras,
793
  inputs=[selected_indices, loras_state],
 
125
  if selected_index in selected_indices:
126
  selected_indices.remove(selected_index)
127
  else:
128
+ if len(selected_indices) < 3:
129
  selected_indices.append(selected_index)
130
  else:
131
  gr.Warning("You can select up to 3 LoRAs, remove one to select a new one.")
132
  return gr.update(), gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), width, height, gr.update(), gr.update(), gr.update()
133
 
134
+ selected_info_1 = "Select LoRA 1"
135
+ selected_info_2 = "Select LoRA 2"
136
+ selected_info_3 = "Select LoRA 3"
 
137
 
138
  lora_scale_1 = 1.15
139
  lora_scale_2 = 1.15
140
  lora_scale_3 = 1.15
141
  lora_image_1 = None
142
  lora_image_2 = None
143
+ lora_image_3 = None
144
 
145
  if len(selected_indices) >= 1:
146
  lora1 = loras_state[selected_indices[0]]
 
163
 
164
  return gr.update(placeholder=new_placeholder), selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, width, height, lora_image_1, lora_image_2, lora_image_3
165
 
166
+ def remove_lora(selected_indices, loras_state, index_to_remove):
167
+ if len(selected_indices) > index_to_remove:
168
+ selected_indices.pop(index_to_remove)
169
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  selected_info_1 = "Select LoRA 1"
171
  selected_info_2 = "Select LoRA 2"
172
+ selected_info_3 = "Select LoRA 3"
173
  lora_scale_1 = 1.15
174
  lora_scale_2 = 1.15
175
+ lora_scale_3 = 1.15
176
  lora_image_1 = None
177
  lora_image_2 = None
178
+ lora_image_3 = None
179
+
180
+ for i, idx in enumerate(selected_indices):
181
+ lora = loras_state[idx]
182
+ if i == 0:
183
+ selected_info_1 = f"### LoRA 1 Selected: [{lora['title']}]({lora['repo']}) ✨"
184
+ lora_image_1 = lora['image']
185
+ elif i == 1:
186
+ selected_info_2 = f"### LoRA 2 Selected: [{lora['title']}]({lora['repo']}) ✨"
187
+ lora_image_2 = lora['image']
188
+ elif i == 2:
189
+ selected_info_3 = f"### LoRA 3 Selected: [{lora['title']}]({lora['repo']}) ✨"
190
+ lora_image_3 = lora['image']
191
+
192
+ return selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3
193
+
194
+ def remove_lora_1(selected_indices, loras_state):
195
+ return remove_lora(selected_indices, loras_state, 0)
196
+
197
+ def remove_lora_2(selected_indices, loras_state):
198
+ return remove_lora(selected_indices, loras_state, 1)
199
+
200
+ def remove_lora_3(selected_indices, loras_state):
201
+ return remove_lora(selected_indices, loras_state, 2)
202
 
203
  def randomize_loras(selected_indices, loras_state):
204
  try:
205
+ if len(loras_state) < 3:
206
  raise gr.Error("Not enough LoRAs to randomize.")
207
+ selected_indices = random.sample(range(len(loras_state)), 3)
208
  lora1 = loras_state[selected_indices[0]]
209
  lora2 = loras_state[selected_indices[1]]
210
+ lora3 = loras_state[selected_indices[2]]
211
  selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}](https://huggingface.co/{lora1['repo']}) ✨"
212
  selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}](https://huggingface.co/{lora2['repo']}) ✨"
213
+ selected_info_3 = f"### LoRA 3 Selected: [{lora3['title']}](https://huggingface.co/{lora3['repo']}) ✨"
214
  lora_scale_1 = 1.15
215
  lora_scale_2 = 1.15
216
+ lora_scale_3 = 1.15
217
  lora_image_1 = lora1['image']
218
  lora_image_2 = lora2['image']
219
+ lora_image_3 = lora3['image']
220
  random_prompt = random.choice(prompt_values)
221
+ return selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3, random_prompt
222
  except Exception as e:
223
  print(f"Error in randomize_loras: {str(e)}")
224
+ return "Error", "Error", "Error", [], 1.15, 1.15, 1.15, None, None, None, ""
225
 
226
  def add_custom_lora(custom_lora, selected_indices, current_loras):
227
  if custom_lora:
 
246
  # Update gallery
247
  gallery_items = [(item["image"], item["title"]) for item in current_loras]
248
  # Update selected_indices if there's room
249
+ if len(selected_indices) < 3:
250
  selected_indices.append(existing_item_index)
251
  else:
252
+ gr.Warning("You can select up to 3 LoRAs, remove one to select a new one.")
253
 
254
  # Update selected_info and images
255
  selected_info_1 = "Select a LoRA 1"
256
  selected_info_2 = "Select a LoRA 2"
257
+ selected_info_3 = "Select a LoRA 3"
258
  lora_scale_1 = 1.15
259
  lora_scale_2 = 1.15
260
+ lora_scale_3 = 1.15
261
  lora_image_1 = None
262
  lora_image_2 = None
263
+ lora_image_3 = None
264
  if len(selected_indices) >= 1:
265
  lora1 = current_loras[selected_indices[0]]
266
  selected_info_1 = f"### LoRA 1 Selected: {lora1['title']} ✨"
 
269
  lora2 = current_loras[selected_indices[1]]
270
  selected_info_2 = f"### LoRA 2 Selected: {lora2['title']} ✨"
271
  lora_image_2 = lora2['image'] if lora2['image'] else None
272
+ if len(selected_indices) >= 3:
273
+ lora3 = current_loras[selected_indices[2]]
274
+ selected_info_3 = f"### LoRA 3 Selected: {lora3['title']} ✨"
275
+ lora_image_3 = lora3['image'] if lora3['image'] else None
276
  print("Finished adding custom LoRA")
277
  return (
278
  current_loras,
279
  gr.update(value=gallery_items),
280
  selected_info_1,
281
  selected_info_2,
282
+ selected_info_3,
283
  selected_indices,
284
  lora_scale_1,
285
  lora_scale_2,
286
+ lora_scale_3,
287
  lora_image_1,
288
+ lora_image_2,
289
+ lora_image_3
290
  )
291
  except Exception as e:
292
  print(e)
293
  gr.Warning(str(e))
294
+ return current_loras, gr.update(), gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
295
  else:
296
+ return current_loras, gr.update(), gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
297
 
298
  def remove_custom_lora(selected_indices, current_loras):
299
  if current_loras:
 
309
  # Update selected_info and images
310
  selected_info_1 = "Select a LoRA 1"
311
  selected_info_2 = "Select a LoRA 2"
312
+ selected_info_3 = "Select a LoRA 3"
313
  lora_scale_1 = 1.15
314
  lora_scale_2 = 1.15
315
+ lora_scale_3 = 1.15
316
  lora_image_1 = None
317
  lora_image_2 = None
318
+ lora_image_3 = None
319
  if len(selected_indices) >= 1:
320
  lora1 = current_loras[selected_indices[0]]
321
  selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
 
324
  lora2 = current_loras[selected_indices[1]]
325
  selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
326
  lora_image_2 = lora2['image']
327
+ if len(selected_indices) >= 3:
328
+ lora3 = current_loras[selected_indices[2]]
329
+ selected_info_3 = f"### LoRA 3 Selected: [{lora3['title']}]({lora3['repo']}) ✨"
330
+ lora_image_3 = lora3['image']
331
  return (
332
  current_loras,
333
  gr.update(value=gallery_items),
334
  selected_info_1,
335
  selected_info_2,
336
+ selected_info_3,
337
  selected_indices,
338
  lora_scale_1,
339
  lora_scale_2,
340
+ lora_scale_3,
341
  lora_image_1,
342
+ lora_image_2,
343
+ lora_image_3
344
  )
345
 
346
  @spaces.GPU(duration=75)
 
796
  inputs=[selected_indices, loras_state],
797
  outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
798
  )
799
+
800
  remove_button_2.click(
801
  remove_lora_2,
802
  inputs=[selected_indices, loras_state],
803
  outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
804
  )
805
+
806
  remove_button_3.click(
807
  remove_lora_3,
808
  inputs=[selected_indices, loras_state],
809
  outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
810
  )
811
 
812
+
813
+
814
  randomize_button.click(
815
  randomize_loras,
816
  inputs=[selected_indices, loras_state],