Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import random
|
|
5 |
from PIL import Image
|
6 |
import base64
|
7 |
import io
|
8 |
-
from
|
9 |
|
10 |
# Define categories at the top so they are accessible throughout the code
|
11 |
categories = [
|
@@ -58,11 +58,11 @@ class DataManager:
|
|
58 |
|
59 |
def save_characters(self):
|
60 |
with open(self.characters_file, 'w') as f:
|
61 |
-
json.dump(self.characters, f)
|
62 |
|
63 |
def save_persistent_tags(self):
|
64 |
with open(self.persistent_tags_file, 'w') as f:
|
65 |
-
json.dump(self.persistent_tags, f)
|
66 |
|
67 |
def load_category_tags(self):
|
68 |
if os.path.exists(self.category_tags_file):
|
@@ -73,7 +73,7 @@ class DataManager:
|
|
73 |
|
74 |
def save_category_tags(self):
|
75 |
with open(self.category_tags_file, 'w') as f:
|
76 |
-
json.dump(self.category_tags, f)
|
77 |
|
78 |
def get_category_tags(self, category_var_name):
|
79 |
# Return the tags list for the given category variable name
|
@@ -110,7 +110,7 @@ class DataManager:
|
|
110 |
# Save the image if provided
|
111 |
if image_data:
|
112 |
try:
|
113 |
-
#
|
114 |
image = Image.open(image_data)
|
115 |
image.save(image_path)
|
116 |
character['image_path'] = image_path
|
@@ -145,6 +145,11 @@ class DataManager:
|
|
145 |
image = Image.open(image_path)
|
146 |
image.save(new_image_path)
|
147 |
updated_character['image_path'] = new_image_path
|
|
|
|
|
|
|
|
|
|
|
148 |
except Exception as e:
|
149 |
print(f"Error updating image: {e}")
|
150 |
updated_character['image_path'] = char.get('image_path')
|
@@ -176,8 +181,9 @@ class DataManager:
|
|
176 |
return True
|
177 |
return False
|
178 |
|
179 |
-
def character_creation_app(data_manager):
|
180 |
with gr.Tab("Character Creation"):
|
|
|
181 |
with gr.Row():
|
182 |
name_input = gr.Textbox(label="Character Name")
|
183 |
traits_input = gr.Textbox(label="Traits/Appearance Tags (comma separated)")
|
@@ -198,6 +204,7 @@ def character_creation_app(data_manager):
|
|
198 |
character = {'name': name, 'traits': traits, 'gender': gender, 'image': image_path}
|
199 |
|
200 |
data_manager.add_character(character)
|
|
|
201 |
return f"Character '{name}' saved successfully."
|
202 |
|
203 |
save_button.click(save_character, inputs=[name_input, traits_input, image_input, gender_input], outputs=output)
|
@@ -214,8 +221,8 @@ def character_creation_app(data_manager):
|
|
214 |
def list_characters():
|
215 |
characters = data_manager.get_characters()
|
216 |
if not characters:
|
217 |
-
return "No characters created yet."
|
218 |
-
|
219 |
components = []
|
220 |
for char in characters:
|
221 |
with gr.Accordion(label=char['name'], open=False):
|
@@ -232,14 +239,15 @@ def character_creation_app(data_manager):
|
|
232 |
with gr.Row():
|
233 |
edit_btn = gr.Button("Edit", variant="primary")
|
234 |
delete_btn = gr.Button("Delete", variant="secondary")
|
235 |
-
edit_output = gr.Textbox(label="", interactive=False)
|
236 |
-
delete_output = gr.Textbox(label="", interactive=False)
|
237 |
|
238 |
# Edit Interface (initially hidden)
|
239 |
-
edit_interface = gr.
|
240 |
with edit_interface:
|
241 |
-
|
242 |
-
|
|
|
243 |
edit_image = gr.Image(label="Upload New Character Image", type="filepath")
|
244 |
edit_gender = gr.Radio(choices=["Boy", "Girl"], label="Gender", value=char['gender'])
|
245 |
save_edit_btn = gr.Button("Save Changes")
|
@@ -261,6 +269,7 @@ def character_creation_app(data_manager):
|
|
261 |
}
|
262 |
success = data_manager.update_character(original_name, updated_char)
|
263 |
if success:
|
|
|
264 |
return f"Character '{new_name}' updated successfully."
|
265 |
else:
|
266 |
return "Failed to update character."
|
@@ -272,11 +281,11 @@ def character_creation_app(data_manager):
|
|
272 |
)
|
273 |
|
274 |
# Define edit functionality
|
275 |
-
def
|
276 |
-
return
|
277 |
|
278 |
edit_btn.click(
|
279 |
-
|
280 |
inputs=None,
|
281 |
outputs=edit_interface
|
282 |
)
|
@@ -288,29 +297,28 @@ def character_creation_app(data_manager):
|
|
288 |
def perform_delete(char_name):
|
289 |
success = data_manager.delete_character(char_name)
|
290 |
if success:
|
|
|
291 |
return f"Character '{char_name}' deleted successfully."
|
292 |
else:
|
293 |
return f"Failed to delete character '{char_name}'."
|
294 |
|
|
|
|
|
|
|
295 |
delete_btn.click(
|
296 |
lambda name: confirm_delete(name),
|
297 |
inputs=[char['name']],
|
298 |
-
outputs=delete_output
|
299 |
)
|
300 |
|
301 |
delete_confirm = gr.Button("Confirm Delete", visible=False)
|
302 |
delete_cancel = gr.Button("Cancel Delete", visible=False)
|
303 |
|
304 |
# Show confirmation buttons when delete is clicked
|
305 |
-
def show_delete_buttons(message):
|
306 |
-
if "Are you sure" in message:
|
307 |
-
return gr.update(visible=True), message
|
308 |
-
return gr.update(visible=False), message
|
309 |
-
|
310 |
delete_btn.click(
|
311 |
-
|
312 |
-
inputs=
|
313 |
-
outputs=[delete_confirm
|
314 |
)
|
315 |
|
316 |
# Handle delete confirmation
|
@@ -325,188 +333,195 @@ def character_creation_app(data_manager):
|
|
325 |
outputs=delete_output
|
326 |
)
|
327 |
|
328 |
-
|
329 |
-
|
330 |
return components
|
331 |
|
332 |
-
|
|
|
|
|
|
|
333 |
|
334 |
# Refresh Button to reload characters
|
335 |
refresh_button = gr.Button("Refresh Characters")
|
336 |
-
refresh_button.click(fn=list_characters, outputs=characters_container)
|
337 |
|
338 |
-
|
339 |
-
with gr.Tab("Prompt Generator"):
|
340 |
-
gr.Markdown("## Prompt Generator")
|
341 |
|
342 |
-
|
343 |
-
|
|
|
344 |
|
345 |
-
|
346 |
-
|
347 |
-
for category_name, var_name in categories:
|
348 |
-
tags_list = data_manager.get_category_tags(var_name)
|
349 |
-
tags_string = ', '.join(tags_list)
|
350 |
-
max_tags = len(tags_list)
|
351 |
-
if max_tags == 0:
|
352 |
-
default_value = 0
|
353 |
-
else:
|
354 |
-
default_value = min(1, max_tags)
|
355 |
-
with gr.Group():
|
356 |
-
gr.Markdown(f"### {category_name}")
|
357 |
-
tag_display = gr.Markdown(f"**Tags:** {tags_string}")
|
358 |
-
tag_num = gr.Slider(minimum=0, maximum=max_tags, step=1, value=default_value, label=f"Number of {category_name} Tags to Select")
|
359 |
-
inputs[f"{var_name}_num"] = tag_num
|
360 |
-
tag_displays[var_name] = (tag_display, tag_num)
|
361 |
-
|
362 |
-
# For Character Selection
|
363 |
-
with gr.Group():
|
364 |
-
gr.Markdown("### Character Selection")
|
365 |
-
|
366 |
-
# Get the list of characters
|
367 |
-
def get_character_options():
|
368 |
-
characters = data_manager.get_characters()
|
369 |
-
character_options = []
|
370 |
-
for char in characters:
|
371 |
-
option_label = f"{char['name']} ({char['gender']})"
|
372 |
-
character_options.append(option_label)
|
373 |
-
return character_options
|
374 |
|
375 |
-
|
376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
377 |
|
378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
379 |
|
380 |
-
|
381 |
-
|
382 |
-
return gr.CheckboxGroup.update(choices=new_options)
|
383 |
|
384 |
-
|
385 |
|
386 |
-
|
387 |
-
|
|
|
388 |
|
389 |
-
|
390 |
-
|
|
|
|
|
391 |
|
392 |
-
|
393 |
-
|
394 |
|
395 |
-
|
396 |
-
|
397 |
-
tags_list = data_manager.get_category_tags(var_name)
|
398 |
-
tags_num = args[arg_idx]
|
399 |
-
arg_idx += 1
|
400 |
|
401 |
-
|
402 |
-
|
403 |
-
prompt_tags.extend(selected_tags)
|
404 |
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
|
|
409 |
|
410 |
-
|
|
|
|
|
411 |
|
412 |
-
|
|
|
|
|
|
|
413 |
|
414 |
-
|
415 |
-
num = min(len(characters), int(num_random_chars))
|
416 |
-
selected_chars = random.sample(characters, num)
|
417 |
-
else:
|
418 |
-
# Extract selected character names from options
|
419 |
-
selected_chars = []
|
420 |
-
for option in selected_character_options:
|
421 |
-
name = option.split(' (')[0]
|
422 |
-
for char in characters:
|
423 |
-
if char['name'] == name:
|
424 |
-
selected_chars.append(char)
|
425 |
-
break
|
426 |
-
|
427 |
-
# Determine the number of boys and girls
|
428 |
-
num_girls = sum(1 for char in selected_chars if char.get('gender') == 'Girl')
|
429 |
-
num_boys = sum(1 for char in selected_chars if char.get('gender') == 'Boy')
|
430 |
-
|
431 |
-
# Build the initial character count tags
|
432 |
-
character_count_tags = []
|
433 |
-
if num_girls > 0:
|
434 |
-
character_count_tags.append(f"{num_girls}girl" if num_girls == 1 else f"{num_girls}girls")
|
435 |
-
if num_boys > 0:
|
436 |
-
character_count_tags.append(f"{num_boys}boy" if num_boys == 1 else f"{num_boys}boys")
|
437 |
-
|
438 |
-
prompt_parts = []
|
439 |
-
|
440 |
-
if character_count_tags:
|
441 |
-
prompt_parts.append(', '.join(character_count_tags))
|
442 |
-
|
443 |
-
# Build character descriptions
|
444 |
-
character_descriptions = []
|
445 |
-
for idx, char in enumerate(selected_chars):
|
446 |
-
# Get traits for the character
|
447 |
-
traits = ', '.join(char['traits'])
|
448 |
-
# Create a description for each character
|
449 |
-
# For SDXL models, use the format "[char1 description] AND [char2 description]"
|
450 |
-
# Each character's description is enclosed in parentheses
|
451 |
-
character_description = f"({traits})"
|
452 |
-
character_descriptions.append(character_description)
|
453 |
-
|
454 |
-
# Join character descriptions appropriately for SDXL models
|
455 |
-
if character_descriptions:
|
456 |
-
character_descriptions_str = ' AND '.join(character_descriptions)
|
457 |
-
prompt_parts.append(character_descriptions_str)
|
458 |
-
|
459 |
-
# Append selected prompt tags from categories
|
460 |
-
if prompt_tags:
|
461 |
-
prompt_tags_str = ', '.join(prompt_tags)
|
462 |
-
prompt_parts.append(prompt_tags_str)
|
463 |
-
|
464 |
-
# Load persistent tags
|
465 |
-
persistent_tags = data_manager.get_persistent_tags()
|
466 |
-
if persistent_tags:
|
467 |
-
persistent_tags_str = ', '.join(persistent_tags)
|
468 |
-
prompt_parts.append(persistent_tags_str)
|
469 |
-
|
470 |
-
# Add ending tags
|
471 |
-
ending_tags = "source_anime, score_9, score_8_up, score_7_up, masterpiece, best quality, very aesthetic, absurdres, anime artwork, anime style, vibrant, studio anime, highly detailed"
|
472 |
-
prompt_parts.append(ending_tags)
|
473 |
-
|
474 |
-
prompt_string = ', '.join(prompt_parts)
|
475 |
-
|
476 |
-
return prompt_string
|
477 |
-
|
478 |
-
# Prepare the list of inputs for the generate_prompt function
|
479 |
-
inputs_list = []
|
480 |
-
for category_name, var_name in categories:
|
481 |
-
inputs_list.append(inputs[f"{var_name}_num"])
|
482 |
-
# Add character_select directly to inputs
|
483 |
-
inputs_list.extend([character_select, random_characters, num_characters])
|
484 |
|
485 |
-
|
486 |
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
for category_name, var_name in categories:
|
491 |
-
# Reload tags from data_manager
|
492 |
-
tags_list = data_manager.get_category_tags(var_name)
|
493 |
-
tags_string = ', '.join(tags_list)
|
494 |
-
max_tags = len(tags_list)
|
495 |
-
if max_tags == 0:
|
496 |
-
slider_value = 0
|
497 |
else:
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
510 |
|
511 |
def tags_app(data_manager):
|
512 |
with gr.Tab("Tags"):
|
@@ -549,9 +564,11 @@ def main():
|
|
549 |
data_manager = DataManager(base_dir='/data')
|
550 |
with gr.Blocks() as demo:
|
551 |
with gr.Tabs():
|
552 |
-
|
553 |
-
|
554 |
-
|
|
|
|
|
555 |
|
556 |
demo.launch()
|
557 |
|
|
|
5 |
from PIL import Image
|
6 |
import base64
|
7 |
import io
|
8 |
+
from functools import partial
|
9 |
|
10 |
# Define categories at the top so they are accessible throughout the code
|
11 |
categories = [
|
|
|
58 |
|
59 |
def save_characters(self):
|
60 |
with open(self.characters_file, 'w') as f:
|
61 |
+
json.dump(self.characters, f, indent=4)
|
62 |
|
63 |
def save_persistent_tags(self):
|
64 |
with open(self.persistent_tags_file, 'w') as f:
|
65 |
+
json.dump(self.persistent_tags, f, indent=4)
|
66 |
|
67 |
def load_category_tags(self):
|
68 |
if os.path.exists(self.category_tags_file):
|
|
|
73 |
|
74 |
def save_category_tags(self):
|
75 |
with open(self.category_tags_file, 'w') as f:
|
76 |
+
json.dump(self.category_tags, f, indent=4)
|
77 |
|
78 |
def get_category_tags(self, category_var_name):
|
79 |
# Return the tags list for the given category variable name
|
|
|
110 |
# Save the image if provided
|
111 |
if image_data:
|
112 |
try:
|
113 |
+
# Open and save the uploaded image to the images folder
|
114 |
image = Image.open(image_data)
|
115 |
image.save(image_path)
|
116 |
character['image_path'] = image_path
|
|
|
145 |
image = Image.open(image_path)
|
146 |
image.save(new_image_path)
|
147 |
updated_character['image_path'] = new_image_path
|
148 |
+
# Remove old image if name has changed
|
149 |
+
if original_name != updated_character['name']:
|
150 |
+
old_image_path = char.get('image_path')
|
151 |
+
if old_image_path and os.path.exists(old_image_path):
|
152 |
+
os.remove(old_image_path)
|
153 |
except Exception as e:
|
154 |
print(f"Error updating image: {e}")
|
155 |
updated_character['image_path'] = char.get('image_path')
|
|
|
181 |
return True
|
182 |
return False
|
183 |
|
184 |
+
def character_creation_app(data_manager, refresh_characters_fn):
|
185 |
with gr.Tab("Character Creation"):
|
186 |
+
gr.Markdown("## Create a New Character")
|
187 |
with gr.Row():
|
188 |
name_input = gr.Textbox(label="Character Name")
|
189 |
traits_input = gr.Textbox(label="Traits/Appearance Tags (comma separated)")
|
|
|
204 |
character = {'name': name, 'traits': traits, 'gender': gender, 'image': image_path}
|
205 |
|
206 |
data_manager.add_character(character)
|
207 |
+
refresh_characters_fn()
|
208 |
return f"Character '{name}' saved successfully."
|
209 |
|
210 |
save_button.click(save_character, inputs=[name_input, traits_input, image_input, gender_input], outputs=output)
|
|
|
221 |
def list_characters():
|
222 |
characters = data_manager.get_characters()
|
223 |
if not characters:
|
224 |
+
return [gr.Markdown("No characters created yet.")]
|
225 |
+
|
226 |
components = []
|
227 |
for char in characters:
|
228 |
with gr.Accordion(label=char['name'], open=False):
|
|
|
239 |
with gr.Row():
|
240 |
edit_btn = gr.Button("Edit", variant="primary")
|
241 |
delete_btn = gr.Button("Delete", variant="secondary")
|
242 |
+
edit_output = gr.Textbox(label="", interactive=False, visible=False)
|
243 |
+
delete_output = gr.Textbox(label="", interactive=False, visible=False)
|
244 |
|
245 |
# Edit Interface (initially hidden)
|
246 |
+
edit_interface = gr.Column(visible=False)
|
247 |
with edit_interface:
|
248 |
+
with gr.Row():
|
249 |
+
edit_name = gr.Textbox(label="Character Name", value=char['name'])
|
250 |
+
edit_traits = gr.Textbox(label="Traits/Appearance Tags (comma separated)", value=', '.join(char['traits']))
|
251 |
edit_image = gr.Image(label="Upload New Character Image", type="filepath")
|
252 |
edit_gender = gr.Radio(choices=["Boy", "Girl"], label="Gender", value=char['gender'])
|
253 |
save_edit_btn = gr.Button("Save Changes")
|
|
|
269 |
}
|
270 |
success = data_manager.update_character(original_name, updated_char)
|
271 |
if success:
|
272 |
+
refresh_characters_fn()
|
273 |
return f"Character '{new_name}' updated successfully."
|
274 |
else:
|
275 |
return "Failed to update character."
|
|
|
281 |
)
|
282 |
|
283 |
# Define edit functionality
|
284 |
+
def toggle_edit_visibility(event, current_visibility=False):
|
285 |
+
return not current_visibility
|
286 |
|
287 |
edit_btn.click(
|
288 |
+
toggle_edit_visibility,
|
289 |
inputs=None,
|
290 |
outputs=edit_interface
|
291 |
)
|
|
|
297 |
def perform_delete(char_name):
|
298 |
success = data_manager.delete_character(char_name)
|
299 |
if success:
|
300 |
+
refresh_characters_fn()
|
301 |
return f"Character '{char_name}' deleted successfully."
|
302 |
else:
|
303 |
return f"Failed to delete character '{char_name}'."
|
304 |
|
305 |
+
def show_confirmation(message):
|
306 |
+
return message, True
|
307 |
+
|
308 |
delete_btn.click(
|
309 |
lambda name: confirm_delete(name),
|
310 |
inputs=[char['name']],
|
311 |
+
outputs=[delete_output, delete_output]
|
312 |
)
|
313 |
|
314 |
delete_confirm = gr.Button("Confirm Delete", visible=False)
|
315 |
delete_cancel = gr.Button("Cancel Delete", visible=False)
|
316 |
|
317 |
# Show confirmation buttons when delete is clicked
|
|
|
|
|
|
|
|
|
|
|
318 |
delete_btn.click(
|
319 |
+
lambda: gr.update(visible=True),
|
320 |
+
inputs=None,
|
321 |
+
outputs=[delete_confirm]
|
322 |
)
|
323 |
|
324 |
# Handle delete confirmation
|
|
|
333 |
outputs=delete_output
|
334 |
)
|
335 |
|
336 |
+
# Add spacing or separator if needed
|
337 |
+
components.append("")
|
338 |
return components
|
339 |
|
340 |
+
def refresh_characters():
|
341 |
+
return list_characters()
|
342 |
+
|
343 |
+
characters_container.render(list_characters())
|
344 |
|
345 |
# Refresh Button to reload characters
|
346 |
refresh_button = gr.Button("Refresh Characters")
|
|
|
347 |
|
348 |
+
refresh_button.click(fn=refresh_characters, outputs=characters_container)
|
|
|
|
|
349 |
|
350 |
+
def prompt_generator_app(data_manager):
|
351 |
+
with gr.Tab("Prompt Generator"):
|
352 |
+
gr.Markdown("## Prompt Generator")
|
353 |
|
354 |
+
# Add a refresh tags button
|
355 |
+
refresh_tags_button = gr.Button("Refresh Tags")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
356 |
|
357 |
+
inputs = {}
|
358 |
+
tag_displays = {}
|
359 |
+
for category_name, var_name in categories:
|
360 |
+
tags_list = data_manager.get_category_tags(var_name)
|
361 |
+
tags_string = ', '.join(tags_list)
|
362 |
+
max_tags = len(tags_list)
|
363 |
+
if max_tags == 0:
|
364 |
+
default_value = 0
|
365 |
+
else:
|
366 |
+
default_value = min(1, max_tags)
|
367 |
+
with gr.Group():
|
368 |
+
gr.Markdown(f"### {category_name}")
|
369 |
+
tag_display = gr.Markdown(f"**Tags:** {tags_string}")
|
370 |
+
tag_num = gr.Slider(minimum=0, maximum=max_tags, step=1, value=default_value, label=f"Number of {category_name} Tags to Select")
|
371 |
+
inputs[f"{var_name}_num"] = tag_num
|
372 |
+
tag_displays[var_name] = (tag_display, tag_num)
|
373 |
+
|
374 |
+
# For Character Selection
|
375 |
+
with gr.Group():
|
376 |
+
gr.Markdown("### Character Selection")
|
377 |
|
378 |
+
# Get the list of characters
|
379 |
+
def get_character_options():
|
380 |
+
characters = data_manager.get_characters()
|
381 |
+
character_options = []
|
382 |
+
for char in characters:
|
383 |
+
option_label = f"{char['name']} ({char['gender']})"
|
384 |
+
character_options.append(option_label)
|
385 |
+
return character_options
|
386 |
|
387 |
+
character_options = get_character_options()
|
388 |
+
character_select = gr.CheckboxGroup(choices=character_options, label="Select Characters", interactive=True)
|
|
|
389 |
|
390 |
+
refresh_characters_button = gr.Button("Refresh Character List")
|
391 |
|
392 |
+
def refresh_characters_fn():
|
393 |
+
new_options = get_character_options()
|
394 |
+
return new_options
|
395 |
|
396 |
+
refresh_characters_button.click(
|
397 |
+
lambda: get_character_options(),
|
398 |
+
outputs=character_select
|
399 |
+
)
|
400 |
|
401 |
+
random_characters = gr.Checkbox(label="Select Random Characters")
|
402 |
+
num_characters = gr.Slider(minimum=1, maximum=10, step=1, value=1, label="Number of Characters (if random)")
|
403 |
|
404 |
+
generate_button = gr.Button("Generate Prompt")
|
405 |
+
prompt_output = gr.Textbox(label="Generated Prompt", lines=5)
|
|
|
|
|
|
|
406 |
|
407 |
+
def generate_prompt(*args):
|
408 |
+
arg_idx = 0
|
|
|
409 |
|
410 |
+
prompt_tags = []
|
411 |
+
for category_name, var_name in categories:
|
412 |
+
tags_list = data_manager.get_category_tags(var_name)
|
413 |
+
tags_num = args[arg_idx]
|
414 |
+
arg_idx += 1
|
415 |
|
416 |
+
if tags_list and tags_num > 0:
|
417 |
+
selected_tags = random.sample(tags_list, min(len(tags_list), int(tags_num)))
|
418 |
+
prompt_tags.extend(selected_tags)
|
419 |
|
420 |
+
# Handle Characters
|
421 |
+
selected_character_options = args[arg_idx]
|
422 |
+
random_chars = args[arg_idx + 1]
|
423 |
+
num_random_chars = args[arg_idx + 2]
|
424 |
|
425 |
+
arg_idx += 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
426 |
|
427 |
+
characters = data_manager.get_characters()
|
428 |
|
429 |
+
if random_chars:
|
430 |
+
num = min(len(characters), int(num_random_chars))
|
431 |
+
selected_chars = random.sample(characters, num)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
432 |
else:
|
433 |
+
# Extract selected character names from options
|
434 |
+
selected_chars = []
|
435 |
+
for option in selected_character_options:
|
436 |
+
name = option.split(' (')[0]
|
437 |
+
for char in characters:
|
438 |
+
if char['name'] == name:
|
439 |
+
selected_chars.append(char)
|
440 |
+
break
|
441 |
+
|
442 |
+
# Determine the number of boys and girls
|
443 |
+
num_girls = sum(1 for char in selected_chars if char.get('gender') == 'Girl')
|
444 |
+
num_boys = sum(1 for char in selected_chars if char.get('gender') == 'Boy')
|
445 |
+
|
446 |
+
# Build the initial character count tags
|
447 |
+
character_count_tags = []
|
448 |
+
if num_girls > 0:
|
449 |
+
character_count_tags.append(f"{num_girls}girl" if num_girls == 1 else f"{num_girls}girls")
|
450 |
+
if num_boys > 0:
|
451 |
+
character_count_tags.append(f"{num_boys}boy" if num_boys == 1 else f"{num_boys}boys")
|
452 |
+
|
453 |
+
prompt_parts = []
|
454 |
+
|
455 |
+
if character_count_tags:
|
456 |
+
prompt_parts.append(', '.join(character_count_tags))
|
457 |
+
|
458 |
+
# Build character descriptions
|
459 |
+
character_descriptions = []
|
460 |
+
for idx, char in enumerate(selected_chars):
|
461 |
+
# Get traits for the character
|
462 |
+
traits = ', '.join(char['traits'])
|
463 |
+
# Create a description for each character
|
464 |
+
# For SDXL models, use the format "[char1 description] AND [char2 description]"
|
465 |
+
# Each character's description is enclosed in parentheses
|
466 |
+
character_description = f"({traits})"
|
467 |
+
character_descriptions.append(character_description)
|
468 |
+
|
469 |
+
# Join character descriptions appropriately for SDXL models
|
470 |
+
if character_descriptions:
|
471 |
+
character_descriptions_str = ' AND '.join(character_descriptions)
|
472 |
+
prompt_parts.append(character_descriptions_str)
|
473 |
+
|
474 |
+
# Append selected prompt tags from categories
|
475 |
+
if prompt_tags:
|
476 |
+
prompt_tags_str = ', '.join(prompt_tags)
|
477 |
+
prompt_parts.append(prompt_tags_str)
|
478 |
+
|
479 |
+
# Load persistent tags
|
480 |
+
persistent_tags = data_manager.get_persistent_tags()
|
481 |
+
if persistent_tags:
|
482 |
+
persistent_tags_str = ', '.join(persistent_tags)
|
483 |
+
prompt_parts.append(persistent_tags_str)
|
484 |
+
|
485 |
+
# Add ending tags
|
486 |
+
ending_tags = "source_anime, score_9, score_8_up, score_7_up, masterpiece, best quality, very aesthetic, absurdres, anime artwork, anime style, vibrant, studio anime, highly detailed"
|
487 |
+
prompt_parts.append(ending_tags)
|
488 |
+
|
489 |
+
prompt_string = ', '.join(prompt_parts)
|
490 |
+
|
491 |
+
return prompt_string
|
492 |
+
|
493 |
+
# Prepare the list of inputs for the generate_prompt function
|
494 |
+
inputs_list = []
|
495 |
+
for category_name, var_name in categories:
|
496 |
+
inputs_list.append(inputs[f"{var_name}_num"])
|
497 |
+
# Add character_select directly to inputs
|
498 |
+
inputs_list.extend([character_select, random_characters, num_characters])
|
499 |
+
|
500 |
+
generate_button.click(generate_prompt, inputs=inputs_list, outputs=prompt_output)
|
501 |
+
|
502 |
+
# Function to refresh tags display and sliders
|
503 |
+
def refresh_tags():
|
504 |
+
updates = []
|
505 |
+
for category_name, var_name in categories:
|
506 |
+
# Reload tags from data_manager
|
507 |
+
tags_list = data_manager.get_category_tags(var_name)
|
508 |
+
tags_string = ', '.join(tags_list)
|
509 |
+
max_tags = len(tags_list)
|
510 |
+
if max_tags == 0:
|
511 |
+
slider_value = 0
|
512 |
+
else:
|
513 |
+
slider_value = min(1, max_tags)
|
514 |
+
# Update the tag display and slider
|
515 |
+
tag_display, tag_num = tag_displays[var_name]
|
516 |
+
updates.append(gr.Markdown.update(value=f"**Tags:** {tags_string}"))
|
517 |
+
updates.append(gr.Slider.update(maximum=max_tags, value=slider_value))
|
518 |
+
return updates
|
519 |
+
|
520 |
+
# Prepare the outputs list
|
521 |
+
outputs = [component for pair in tag_displays.values() for component in pair]
|
522 |
+
|
523 |
+
# Connect the refresh_tags function to the refresh_tags_button
|
524 |
+
refresh_tags_button.click(refresh_tags, outputs=outputs)
|
525 |
|
526 |
def tags_app(data_manager):
|
527 |
with gr.Tab("Tags"):
|
|
|
564 |
data_manager = DataManager(base_dir='/data')
|
565 |
with gr.Blocks() as demo:
|
566 |
with gr.Tabs():
|
567 |
+
with gr.Row():
|
568 |
+
with gr.Column():
|
569 |
+
prompt_generator_app(data_manager)
|
570 |
+
character_creation_app(data_manager, refresh_characters_fn=lambda: None)
|
571 |
+
tags_app(data_manager)
|
572 |
|
573 |
demo.launch()
|
574 |
|