Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,7 @@ categories = [
|
|
21 |
]
|
22 |
|
23 |
class DataManager:
|
24 |
-
def __init__(self, base_dir='
|
25 |
self.base_dir = base_dir
|
26 |
# Ensure the base directory exists
|
27 |
if not os.path.exists(self.base_dir):
|
@@ -235,11 +235,11 @@ def prompt_generator_app(data_manager):
|
|
235 |
|
236 |
def refresh_character_selection():
|
237 |
new_options = get_character_options()
|
238 |
-
return gr.CheckboxGroup.update(choices=new_options)
|
239 |
|
240 |
refresh_characters_button.click(
|
241 |
refresh_character_selection,
|
242 |
-
outputs=character_select
|
243 |
)
|
244 |
|
245 |
random_characters = gr.Checkbox(label="Select Random Characters")
|
@@ -347,11 +347,14 @@ def prompt_generator_app(data_manager):
|
|
347 |
# Add character_select directly to inputs
|
348 |
inputs_list.extend([character_select, random_characters, num_characters])
|
349 |
|
350 |
-
generate_button.click(
|
|
|
|
|
|
|
|
|
351 |
|
352 |
# Function to refresh tags display and sliders
|
353 |
def refresh_tags():
|
354 |
-
updates = []
|
355 |
for category_name, var_name in categories:
|
356 |
# Reload tags from data_manager
|
357 |
tags_list = data_manager.get_category_tags(var_name)
|
@@ -363,15 +366,16 @@ def prompt_generator_app(data_manager):
|
|
363 |
slider_value = min(1, max_tags)
|
364 |
# Update the tag display and slider
|
365 |
tag_display, tag_num = tag_displays[var_name]
|
366 |
-
|
367 |
-
|
368 |
-
return
|
369 |
-
|
370 |
-
# Prepare the outputs list
|
371 |
-
outputs = [component for pair in tag_displays.values() for component in pair]
|
372 |
|
373 |
# Connect the refresh_tags function to the refresh_tags_button
|
374 |
-
refresh_tags_button.click(
|
|
|
|
|
|
|
|
|
375 |
|
376 |
def character_creation_app(data_manager):
|
377 |
with gr.Tab("Character Creation"):
|
@@ -397,10 +401,12 @@ def character_creation_app(data_manager):
|
|
397 |
gr.Markdown("## Existing Characters")
|
398 |
|
399 |
# Dropdown to select a character for editing or deleting
|
400 |
-
|
401 |
-
|
|
|
|
|
402 |
selected_character = gr.Dropdown(
|
403 |
-
choices=
|
404 |
label="Select a Character to Edit/Delete",
|
405 |
interactive=True
|
406 |
)
|
@@ -411,7 +417,7 @@ def character_creation_app(data_manager):
|
|
411 |
action_output = gr.Textbox(label="Action Status", interactive=False)
|
412 |
|
413 |
# **Section 3: Edit Character**
|
414 |
-
edit_section = gr.
|
415 |
with edit_section:
|
416 |
with gr.Row():
|
417 |
edit_name = gr.Textbox(
|
@@ -465,9 +471,11 @@ def character_creation_app(data_manager):
|
|
465 |
# Fetch character details
|
466 |
character = next((char for char in data_manager.get_characters() if char['name'] == selected_name), None)
|
467 |
if character:
|
468 |
-
|
469 |
-
|
470 |
-
|
|
|
|
|
471 |
return "", gr.update(visible=True)
|
472 |
else:
|
473 |
return f"Character '{selected_name}' not found.", gr.update(visible=False)
|
@@ -492,10 +500,11 @@ def character_creation_app(data_manager):
|
|
492 |
'traits': new_traits,
|
493 |
'gender': new_gender,
|
494 |
'image': new_image_path if new_image_path else \
|
495 |
-
next((char
|
496 |
}
|
497 |
success = data_manager.update_character(original_name, updated_char)
|
498 |
if success:
|
|
|
499 |
return f"Character '{new_name}' updated successfully.", gr.update(visible=False)
|
500 |
else:
|
501 |
return "Failed to update character.", gr.update(visible=True)
|
@@ -591,7 +600,7 @@ def tags_app(data_manager):
|
|
591 |
)
|
592 |
|
593 |
def main():
|
594 |
-
data_manager = DataManager(base_dir='
|
595 |
with gr.Blocks() as demo:
|
596 |
with gr.Tabs():
|
597 |
prompt_generator_app(data_manager)
|
|
|
21 |
]
|
22 |
|
23 |
class DataManager:
|
24 |
+
def __init__(self, base_dir='data'):
|
25 |
self.base_dir = base_dir
|
26 |
# Ensure the base directory exists
|
27 |
if not os.path.exists(self.base_dir):
|
|
|
235 |
|
236 |
def refresh_character_selection():
|
237 |
new_options = get_character_options()
|
238 |
+
return [gr.CheckboxGroup.update(choices=new_options)]
|
239 |
|
240 |
refresh_characters_button.click(
|
241 |
refresh_character_selection,
|
242 |
+
outputs=[character_select]
|
243 |
)
|
244 |
|
245 |
random_characters = gr.Checkbox(label="Select Random Characters")
|
|
|
347 |
# Add character_select directly to inputs
|
348 |
inputs_list.extend([character_select, random_characters, num_characters])
|
349 |
|
350 |
+
generate_button.click(
|
351 |
+
generate_prompt,
|
352 |
+
inputs=inputs_list,
|
353 |
+
outputs=prompt_output
|
354 |
+
)
|
355 |
|
356 |
# Function to refresh tags display and sliders
|
357 |
def refresh_tags():
|
|
|
358 |
for category_name, var_name in categories:
|
359 |
# Reload tags from data_manager
|
360 |
tags_list = data_manager.get_category_tags(var_name)
|
|
|
366 |
slider_value = min(1, max_tags)
|
367 |
# Update the tag display and slider
|
368 |
tag_display, tag_num = tag_displays[var_name]
|
369 |
+
tag_display.update(value=f"**Tags:** {tags_string}")
|
370 |
+
tag_num.update(maximum=max_tags, value=slider_value)
|
371 |
+
return None # No outputs to return
|
|
|
|
|
|
|
372 |
|
373 |
# Connect the refresh_tags function to the refresh_tags_button
|
374 |
+
refresh_tags_button.click(
|
375 |
+
refresh_tags,
|
376 |
+
inputs=None,
|
377 |
+
outputs=None
|
378 |
+
)
|
379 |
|
380 |
def character_creation_app(data_manager):
|
381 |
with gr.Tab("Character Creation"):
|
|
|
401 |
gr.Markdown("## Existing Characters")
|
402 |
|
403 |
# Dropdown to select a character for editing or deleting
|
404 |
+
def get_character_names():
|
405 |
+
characters = data_manager.get_characters()
|
406 |
+
return [char['name'] for char in characters]
|
407 |
+
|
408 |
selected_character = gr.Dropdown(
|
409 |
+
choices=get_character_names(),
|
410 |
label="Select a Character to Edit/Delete",
|
411 |
interactive=True
|
412 |
)
|
|
|
417 |
action_output = gr.Textbox(label="Action Status", interactive=False)
|
418 |
|
419 |
# **Section 3: Edit Character**
|
420 |
+
edit_section = gr.Row(visible=False)
|
421 |
with edit_section:
|
422 |
with gr.Row():
|
423 |
edit_name = gr.Textbox(
|
|
|
471 |
# Fetch character details
|
472 |
character = next((char for char in data_manager.get_characters() if char['name'] == selected_name), None)
|
473 |
if character:
|
474 |
+
# Pre-fill the edit fields
|
475 |
+
edit_name.value = character['name']
|
476 |
+
edit_traits.value = ', '.join(character['traits'])
|
477 |
+
edit_gender.value = character['gender']
|
478 |
+
edit_image.value = None # Reset image
|
479 |
return "", gr.update(visible=True)
|
480 |
else:
|
481 |
return f"Character '{selected_name}' not found.", gr.update(visible=False)
|
|
|
500 |
'traits': new_traits,
|
501 |
'gender': new_gender,
|
502 |
'image': new_image_path if new_image_path else \
|
503 |
+
next((char for char in data_manager.get_characters() if char['name'] == original_name), {}).get('image')
|
504 |
}
|
505 |
success = data_manager.update_character(original_name, updated_char)
|
506 |
if success:
|
507 |
+
# Update the dropdown choices
|
508 |
return f"Character '{new_name}' updated successfully.", gr.update(visible=False)
|
509 |
else:
|
510 |
return "Failed to update character.", gr.update(visible=True)
|
|
|
600 |
)
|
601 |
|
602 |
def main():
|
603 |
+
data_manager = DataManager(base_dir='data')
|
604 |
with gr.Blocks() as demo:
|
605 |
with gr.Tabs():
|
606 |
prompt_generator_app(data_manager)
|