Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -107,7 +107,7 @@ class DataManager:
|
|
107 |
image_path = os.path.join(self.images_folder, image_filename)
|
108 |
|
109 |
# Save the image if provided
|
110 |
-
if image_data:
|
111 |
try:
|
112 |
# Open and save the uploaded image to the images folder
|
113 |
image = Image.open(image_data)
|
@@ -135,13 +135,13 @@ class DataManager:
|
|
135 |
if char['name'] == original_name:
|
136 |
# Handle image update
|
137 |
if updated_character['image']:
|
138 |
-
|
139 |
safe_name = "".join(c for c in updated_character['name'] if c.isalnum() or c in (' ', '_', '-')).rstrip()
|
140 |
image_filename = f"{safe_name}.png"
|
141 |
new_image_path = os.path.join(self.images_folder, image_filename)
|
142 |
|
143 |
try:
|
144 |
-
image = Image.open(
|
145 |
image.save(new_image_path)
|
146 |
updated_character['image_path'] = new_image_path
|
147 |
# Remove old image if name has changed
|
@@ -354,37 +354,49 @@ def prompt_generator_app(data_manager):
|
|
354 |
|
355 |
def character_creation_app(data_manager):
|
356 |
with gr.Tab("Character Creation"):
|
357 |
-
gr.Markdown("## Create
|
|
|
358 |
with gr.Row():
|
359 |
name_input = gr.Textbox(label="Character Name", placeholder="Enter unique character name")
|
360 |
traits_input = gr.Textbox(label="Traits/Appearance Tags (comma separated)", placeholder="e.g., blue hair, green eyes, tall")
|
361 |
image_input = gr.Image(label="Upload Character Image", type="filepath")
|
362 |
gender_input = gr.Radio(choices=["Boy", "Girl"], label="Gender")
|
363 |
save_button = gr.Button("Save Character")
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
384 |
def save_character(name, traits, image_path, gender):
|
385 |
if not name.strip() or not traits.strip() or not gender:
|
386 |
return "Please enter all fields."
|
387 |
-
|
388 |
# Check for duplicate names
|
389 |
existing_names = [char['name'] for char in data_manager.get_characters()]
|
390 |
if name in existing_names:
|
@@ -392,25 +404,90 @@ def character_creation_app(data_manager):
|
|
392 |
|
393 |
character = {'name': name, 'traits': traits, 'gender': gender, 'image': image_path}
|
394 |
data_manager.add_character(character)
|
395 |
-
|
396 |
return f"Character '{name}' saved successfully."
|
397 |
|
398 |
save_button.click(
|
399 |
save_character,
|
400 |
inputs=[name_input, traits_input, image_input, gender_input],
|
401 |
-
outputs=
|
402 |
)
|
403 |
|
404 |
-
#
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
410 |
)
|
411 |
|
412 |
-
#
|
413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
414 |
|
415 |
def tags_app(data_manager):
|
416 |
with gr.Tab("Tags"):
|
@@ -441,12 +518,12 @@ def tags_app(data_manager):
|
|
441 |
save_persistent_tags_button = gr.Button("Save Persistent Tags")
|
442 |
persistent_status_output = gr.Textbox(label="", interactive=False)
|
443 |
|
444 |
-
def
|
445 |
tags_list = [t.strip() for t in tags_string.split(',') if t.strip()]
|
446 |
data_manager.set_persistent_tags(tags_list)
|
447 |
return "Persistent tags saved successfully."
|
448 |
|
449 |
-
save_persistent_tags_button.click(
|
450 |
|
451 |
def main():
|
452 |
data_manager = DataManager(base_dir='/data')
|
@@ -455,7 +532,6 @@ def main():
|
|
455 |
prompt_generator_app(data_manager)
|
456 |
character_creation_app(data_manager)
|
457 |
tags_app(data_manager)
|
458 |
-
|
459 |
demo.launch()
|
460 |
|
461 |
if __name__ == "__main__":
|
|
|
107 |
image_path = os.path.join(self.images_folder, image_filename)
|
108 |
|
109 |
# Save the image if provided
|
110 |
+
if image_path and os.path.exists(image_data):
|
111 |
try:
|
112 |
# Open and save the uploaded image to the images folder
|
113 |
image = Image.open(image_data)
|
|
|
135 |
if char['name'] == original_name:
|
136 |
# Handle image update
|
137 |
if updated_character['image']:
|
138 |
+
image_data = updated_character['image'] # File path
|
139 |
safe_name = "".join(c for c in updated_character['name'] if c.isalnum() or c in (' ', '_', '-')).rstrip()
|
140 |
image_filename = f"{safe_name}.png"
|
141 |
new_image_path = os.path.join(self.images_folder, image_filename)
|
142 |
|
143 |
try:
|
144 |
+
image = Image.open(image_data)
|
145 |
image.save(new_image_path)
|
146 |
updated_character['image_path'] = new_image_path
|
147 |
# Remove old image if name has changed
|
|
|
354 |
|
355 |
def character_creation_app(data_manager):
|
356 |
with gr.Tab("Character Creation"):
|
357 |
+
gr.Markdown("## Create and Manage Characters")
|
358 |
+
# **Section 1: Create a New Character**
|
359 |
with gr.Row():
|
360 |
name_input = gr.Textbox(label="Character Name", placeholder="Enter unique character name")
|
361 |
traits_input = gr.Textbox(label="Traits/Appearance Tags (comma separated)", placeholder="e.g., blue hair, green eyes, tall")
|
362 |
image_input = gr.Image(label="Upload Character Image", type="filepath")
|
363 |
gender_input = gr.Radio(choices=["Boy", "Girl"], label="Gender")
|
364 |
save_button = gr.Button("Save Character")
|
365 |
+
save_output = gr.Textbox(label="Status", interactive=False)
|
366 |
+
|
367 |
+
# **Section 2: Existing Characters**
|
368 |
+
gr.Markdown("---")
|
369 |
+
gr.Markdown("## Existing Characters")
|
370 |
+
|
371 |
+
# Dropdown to select a character for editing or deleting
|
372 |
+
characters = data_manager.get_characters()
|
373 |
+
character_names = [char['name'] for char in characters]
|
374 |
+
selected_character = gr.Dropdown(choices=character_names, label="Select a Character to Edit/Delete", interactive=True)
|
375 |
+
|
376 |
+
# Buttons for Edit and Delete
|
377 |
+
edit_button = gr.Button("Edit Selected Character")
|
378 |
+
delete_button = gr.Button("Delete Selected Character")
|
379 |
+
action_output = gr.Textbox(label="Action Status", interactive=False)
|
380 |
+
|
381 |
+
# **Section 3: Edit Character**
|
382 |
+
edit_section = gr.Row(visible=False)
|
383 |
+
with edit_section:
|
384 |
+
edit_name = gr.Textbox(label="New Character Name", placeholder="Enter new character name")
|
385 |
+
edit_traits = gr.Textbox(label="New Traits/Appearance Tags (comma separated)", placeholder="e.g., red hair, blue eyes")
|
386 |
+
edit_image = gr.Image(label="Upload New Character Image", type="filepath")
|
387 |
+
edit_gender = gr.Radio(choices=["Boy", "Girl"], label="Gender")
|
388 |
+
update_button = gr.Button("Update Character")
|
389 |
+
update_output = gr.Textbox(label="Update Status", interactive=False)
|
390 |
+
|
391 |
+
# **Section 4: Delete Confirmation**
|
392 |
+
delete_confirmation = gr.Textbox(label="Are you sure you want to delete the selected character? Type 'YES' to confirm.", interactive=True, visible=False)
|
393 |
+
confirm_delete_button = gr.Button("Confirm Delete", visible=False)
|
394 |
+
cancel_delete_button = gr.Button("Cancel Delete", visible=False)
|
395 |
+
|
396 |
+
# **Function to Save New Character**
|
397 |
def save_character(name, traits, image_path, gender):
|
398 |
if not name.strip() or not traits.strip() or not gender:
|
399 |
return "Please enter all fields."
|
|
|
400 |
# Check for duplicate names
|
401 |
existing_names = [char['name'] for char in data_manager.get_characters()]
|
402 |
if name in existing_names:
|
|
|
404 |
|
405 |
character = {'name': name, 'traits': traits, 'gender': gender, 'image': image_path}
|
406 |
data_manager.add_character(character)
|
|
|
407 |
return f"Character '{name}' saved successfully."
|
408 |
|
409 |
save_button.click(
|
410 |
save_character,
|
411 |
inputs=[name_input, traits_input, image_input, gender_input],
|
412 |
+
outputs=save_output
|
413 |
)
|
414 |
|
415 |
+
# **Function to Show Edit Section**
|
416 |
+
def show_edit(selected_name):
|
417 |
+
if not selected_name:
|
418 |
+
return "Please select a character to edit.", gr.update(visible=False)
|
419 |
+
return "", gr.update(visible=True)
|
420 |
+
|
421 |
+
edit_button.click(
|
422 |
+
show_edit,
|
423 |
+
inputs=selected_character,
|
424 |
+
outputs=[action_output, edit_section]
|
425 |
+
)
|
426 |
+
|
427 |
+
# **Function to Update Character**
|
428 |
+
def update_character(original_name, new_name, new_traits, new_image_path, new_gender):
|
429 |
+
if not new_name.strip() or not new_traits.strip() or not new_gender:
|
430 |
+
return "Please enter all fields.", gr.update(visible=False)
|
431 |
+
# If the name has changed, check for duplicates
|
432 |
+
if new_name != original_name:
|
433 |
+
existing_names = [char['name'] for char in data_manager.get_characters()]
|
434 |
+
if new_name in existing_names:
|
435 |
+
return f"Character with name '{new_name}' already exists. Please choose a different name.", gr.update(visible=True)
|
436 |
+
updated_char = {
|
437 |
+
'name': new_name,
|
438 |
+
'traits': new_traits,
|
439 |
+
'gender': new_gender,
|
440 |
+
'image': new_image_path if new_image_path else [char for char in data_manager.get_characters() if char['name'] == original_name][0]['image']
|
441 |
+
}
|
442 |
+
success = data_manager.update_character(original_name, updated_char)
|
443 |
+
if success:
|
444 |
+
return f"Character '{new_name}' updated successfully.", gr.update(visible=False)
|
445 |
+
else:
|
446 |
+
return "Failed to update character.", gr.update(visible=True)
|
447 |
+
|
448 |
+
update_button.click(
|
449 |
+
update_character,
|
450 |
+
inputs=[selected_character, edit_name, edit_traits, edit_image, edit_gender],
|
451 |
+
outputs=[update_output, edit_section]
|
452 |
)
|
453 |
|
454 |
+
# **Function to Show Delete Confirmation**
|
455 |
+
def show_delete_confirmation(selected_name):
|
456 |
+
if not selected_name:
|
457 |
+
return "Please select a character to delete.", gr.update(visible=False), gr.update(visible=False)
|
458 |
+
return f"Are you sure you want to delete '{selected_name}'? Type 'YES' to confirm.", gr.update(visible=True), gr.update(visible=True)
|
459 |
+
|
460 |
+
delete_button.click(
|
461 |
+
show_delete_confirmation,
|
462 |
+
inputs=selected_character,
|
463 |
+
outputs=[action_output, delete_confirmation, confirm_delete_button]
|
464 |
+
)
|
465 |
+
|
466 |
+
# **Function to Perform Deletion**
|
467 |
+
def perform_deletion(selected_name, confirmation):
|
468 |
+
if confirmation != "YES":
|
469 |
+
return "Deletion cancelled.", gr.update(visible=False), gr.update(visible=False)
|
470 |
+
success = data_manager.delete_character(selected_name)
|
471 |
+
if success:
|
472 |
+
return f"Character '{selected_name}' deleted successfully.", gr.update(visible=False), gr.update(visible=False)
|
473 |
+
else:
|
474 |
+
return f"Failed to delete character '{selected_name}'.", gr.update(visible=False), gr.update(visible=False)
|
475 |
+
|
476 |
+
confirm_delete_button.click(
|
477 |
+
perform_deletion,
|
478 |
+
inputs=[selected_character, delete_confirmation],
|
479 |
+
outputs=[action_output, delete_confirmation, confirm_delete_button]
|
480 |
+
)
|
481 |
+
|
482 |
+
# **Function to Cancel Deletion**
|
483 |
+
def cancel_deletion():
|
484 |
+
return "Deletion cancelled.", gr.update(visible=False), gr.update(visible=False)
|
485 |
+
|
486 |
+
cancel_delete_button.click(
|
487 |
+
cancel_deletion,
|
488 |
+
inputs=None,
|
489 |
+
outputs=[action_output, delete_confirmation, confirm_delete_button]
|
490 |
+
)
|
491 |
|
492 |
def tags_app(data_manager):
|
493 |
with gr.Tab("Tags"):
|
|
|
518 |
save_persistent_tags_button = gr.Button("Save Persistent Tags")
|
519 |
persistent_status_output = gr.Textbox(label="", interactive=False)
|
520 |
|
521 |
+
def save_persistent_tags_fn(tags_string):
|
522 |
tags_list = [t.strip() for t in tags_string.split(',') if t.strip()]
|
523 |
data_manager.set_persistent_tags(tags_list)
|
524 |
return "Persistent tags saved successfully."
|
525 |
|
526 |
+
save_persistent_tags_button.click(save_persistent_tags_fn, inputs=persistent_tags_input, outputs=persistent_status_output)
|
527 |
|
528 |
def main():
|
529 |
data_manager = DataManager(base_dir='/data')
|
|
|
532 |
prompt_generator_app(data_manager)
|
533 |
character_creation_app(data_manager)
|
534 |
tags_app(data_manager)
|
|
|
535 |
demo.launch()
|
536 |
|
537 |
if __name__ == "__main__":
|