Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -102,16 +102,21 @@ class DataManager:
|
|
102 |
|
103 |
def add_character(self, character):
|
104 |
# Save image to disk and store the filename
|
105 |
-
image_data = character['image'] # This is
|
106 |
safe_name = "".join(c for c in character['name'] if c.isalnum() or c in (' ', '_', '-')).rstrip()
|
107 |
image_filename = f"{safe_name}.png"
|
108 |
image_path = os.path.join(self.images_folder, image_filename)
|
109 |
|
110 |
-
#
|
111 |
if image_data:
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
115 |
else:
|
116 |
character['image_path'] = None
|
117 |
|
@@ -131,15 +136,18 @@ class DataManager:
|
|
131 |
if char['name'] == original_name:
|
132 |
# Handle image update
|
133 |
if updated_character['image']:
|
134 |
-
|
135 |
safe_name = "".join(c for c in updated_character['name'] if c.isalnum() or c in (' ', '_', '-')).rstrip()
|
136 |
image_filename = f"{safe_name}.png"
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
143 |
else:
|
144 |
# If no new image provided, retain the old image_path
|
145 |
updated_character['image_path'] = char.get('image_path')
|
@@ -173,13 +181,13 @@ def character_creation_app(data_manager):
|
|
173 |
with gr.Row():
|
174 |
name_input = gr.Textbox(label="Character Name")
|
175 |
traits_input = gr.Textbox(label="Traits/Appearance Tags (comma separated)")
|
176 |
-
image_input = gr.Image(label="Upload Character Image", type="
|
177 |
# New gender selection input
|
178 |
gender_input = gr.Radio(choices=["Boy", "Girl"], label="Gender")
|
179 |
save_button = gr.Button("Save Character")
|
180 |
output = gr.Textbox(label="Status", interactive=False)
|
181 |
|
182 |
-
def save_character(name, traits,
|
183 |
if not name or not traits or not gender:
|
184 |
return "Please enter all fields."
|
185 |
# Check for duplicate names
|
@@ -187,7 +195,7 @@ def character_creation_app(data_manager):
|
|
187 |
if name in existing_names:
|
188 |
return f"Character with name '{name}' already exists. Please choose a different name."
|
189 |
|
190 |
-
character = {'name': name, 'traits': traits, 'gender': gender, 'image':
|
191 |
|
192 |
data_manager.add_character(character)
|
193 |
return f"Character '{name}' saved successfully."
|
@@ -210,19 +218,13 @@ def character_creation_app(data_manager):
|
|
210 |
|
211 |
components = []
|
212 |
for char in characters:
|
213 |
-
with gr.
|
214 |
with gr.Row():
|
215 |
if char['image']:
|
216 |
-
image
|
217 |
-
|
218 |
-
image_thumbnail.thumbnail((150, 150))
|
219 |
-
buffered = io.BytesIO()
|
220 |
-
image_thumbnail.save(buffered, format="PNG")
|
221 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
222 |
-
img_data = f"data:image/png;base64,{img_str}"
|
223 |
-
gr.Image(value=img_data, label="Character Image", width=150).render()
|
224 |
else:
|
225 |
-
gr.Markdown("**No Image Provided**")
|
226 |
with gr.Column():
|
227 |
gr.Markdown(f"**Name:** {char['name']}").render()
|
228 |
gr.Markdown(f"**Gender:** {char['gender']}").render()
|
@@ -233,21 +235,17 @@ def character_creation_app(data_manager):
|
|
233 |
edit_output = gr.Textbox(label="", interactive=False)
|
234 |
delete_output = gr.Textbox(label="", interactive=False)
|
235 |
|
236 |
-
#
|
237 |
-
|
238 |
-
return gr.update(visible=True), char_name
|
239 |
-
|
240 |
-
edit_interface = gr.Accordion(label=f"Edit {char['name']}", open=False, visible=False)
|
241 |
with edit_interface:
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
edit_image = gr.Image(label="Upload New Character Image", type="base64")
|
246 |
edit_gender = gr.Radio(choices=["Boy", "Girl"], label="Gender", value=char['gender'])
|
247 |
save_edit_btn = gr.Button("Save Changes")
|
248 |
save_edit_output = gr.Textbox(label="Edit Status", interactive=False)
|
249 |
|
250 |
-
def save_edit(original_name, new_name, new_traits,
|
251 |
if not new_name or not new_traits or not new_gender:
|
252 |
return "Please enter all fields."
|
253 |
# If the name has changed, check for duplicates
|
@@ -259,7 +257,7 @@ def character_creation_app(data_manager):
|
|
259 |
'name': new_name,
|
260 |
'traits': new_traits,
|
261 |
'gender': new_gender,
|
262 |
-
'image':
|
263 |
}
|
264 |
success = data_manager.update_character(original_name, updated_char)
|
265 |
if success:
|
@@ -273,6 +271,16 @@ def character_creation_app(data_manager):
|
|
273 |
outputs=save_edit_output
|
274 |
)
|
275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
# Define delete functionality
|
277 |
def confirm_delete(char_name):
|
278 |
return f"Are you sure you want to delete '{char_name}'?"
|
@@ -319,7 +327,7 @@ def character_creation_app(data_manager):
|
|
319 |
|
320 |
components.append("") # Placeholder for spacing
|
321 |
|
322 |
-
return
|
323 |
|
324 |
characters_container.load(fn=list_characters, outputs=characters_container)
|
325 |
|
@@ -371,7 +379,7 @@ def prompt_generator_app(data_manager):
|
|
371 |
|
372 |
def refresh_characters():
|
373 |
new_options = get_character_options()
|
374 |
-
return gr.update(choices=new_options)
|
375 |
|
376 |
refresh_characters_button.click(refresh_characters, outputs=character_select)
|
377 |
|
@@ -490,8 +498,8 @@ def prompt_generator_app(data_manager):
|
|
490 |
slider_value = min(1, max_tags)
|
491 |
# Update the tag display and slider
|
492 |
tag_display, tag_num = tag_displays[var_name]
|
493 |
-
updates.append(gr.update(value=f"**Tags:** {tags_string}"))
|
494 |
-
updates.append(gr.update(maximum=max_tags, value=slider_value))
|
495 |
return updates
|
496 |
|
497 |
# Prepare the outputs list
|
|
|
102 |
|
103 |
def add_character(self, character):
|
104 |
# Save image to disk and store the filename
|
105 |
+
image_data = character['image'] # This is the file path
|
106 |
safe_name = "".join(c for c in character['name'] if c.isalnum() or c in (' ', '_', '-')).rstrip()
|
107 |
image_filename = f"{safe_name}.png"
|
108 |
image_path = os.path.join(self.images_folder, image_filename)
|
109 |
|
110 |
+
# Save the image if provided
|
111 |
if image_data:
|
112 |
+
try:
|
113 |
+
# Copy the uploaded image to the images folder
|
114 |
+
image = Image.open(image_data)
|
115 |
+
image.save(image_path)
|
116 |
+
character['image_path'] = image_path
|
117 |
+
except Exception as e:
|
118 |
+
print(f"Error saving image: {e}")
|
119 |
+
character['image_path'] = None
|
120 |
else:
|
121 |
character['image_path'] = None
|
122 |
|
|
|
136 |
if char['name'] == original_name:
|
137 |
# Handle image update
|
138 |
if updated_character['image']:
|
139 |
+
image_path = updated_character['image'] # File path
|
140 |
safe_name = "".join(c for c in updated_character['name'] if c.isalnum() or c in (' ', '_', '-')).rstrip()
|
141 |
image_filename = f"{safe_name}.png"
|
142 |
+
new_image_path = os.path.join(self.images_folder, image_filename)
|
143 |
+
|
144 |
+
try:
|
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')
|
151 |
else:
|
152 |
# If no new image provided, retain the old image_path
|
153 |
updated_character['image_path'] = char.get('image_path')
|
|
|
181 |
with gr.Row():
|
182 |
name_input = gr.Textbox(label="Character Name")
|
183 |
traits_input = gr.Textbox(label="Traits/Appearance Tags (comma separated)")
|
184 |
+
image_input = gr.Image(label="Upload Character Image", type="filepath")
|
185 |
# New gender selection input
|
186 |
gender_input = gr.Radio(choices=["Boy", "Girl"], label="Gender")
|
187 |
save_button = gr.Button("Save Character")
|
188 |
output = gr.Textbox(label="Status", interactive=False)
|
189 |
|
190 |
+
def save_character(name, traits, image_path, gender):
|
191 |
if not name or not traits or not gender:
|
192 |
return "Please enter all fields."
|
193 |
# Check for duplicate names
|
|
|
195 |
if name in existing_names:
|
196 |
return f"Character with name '{name}' already exists. Please choose a different name."
|
197 |
|
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."
|
|
|
218 |
|
219 |
components = []
|
220 |
for char in characters:
|
221 |
+
with gr.Accordion(label=char['name'], open=False):
|
222 |
with gr.Row():
|
223 |
if char['image']:
|
224 |
+
# Display the image using filepath
|
225 |
+
img_display = gr.Image(value=char['image'], label="Character Image", type="filepath", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
else:
|
227 |
+
img_display = gr.Markdown("**No Image Provided**")
|
228 |
with gr.Column():
|
229 |
gr.Markdown(f"**Name:** {char['name']}").render()
|
230 |
gr.Markdown(f"**Gender:** {char['gender']}").render()
|
|
|
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.Row(visible=False)
|
|
|
|
|
|
|
240 |
with edit_interface:
|
241 |
+
edit_name = gr.Textbox(label="Character Name", value=char['name'])
|
242 |
+
edit_traits = gr.Textbox(label="Traits/Appearance Tags (comma separated)", value=', '.join(char['traits']))
|
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")
|
246 |
save_edit_output = gr.Textbox(label="Edit Status", interactive=False)
|
247 |
|
248 |
+
def save_edit(original_name, new_name, new_traits, new_image_path, new_gender):
|
249 |
if not new_name or not new_traits or not new_gender:
|
250 |
return "Please enter all fields."
|
251 |
# If the name has changed, check for duplicates
|
|
|
257 |
'name': new_name,
|
258 |
'traits': new_traits,
|
259 |
'gender': new_gender,
|
260 |
+
'image': new_image_path
|
261 |
}
|
262 |
success = data_manager.update_character(original_name, updated_char)
|
263 |
if success:
|
|
|
271 |
outputs=save_edit_output
|
272 |
)
|
273 |
|
274 |
+
# Define edit functionality
|
275 |
+
def toggle_edit_interface(visible, *args):
|
276 |
+
return gr.update(visible=visible)
|
277 |
+
|
278 |
+
edit_btn.click(
|
279 |
+
fn=lambda: gr.update(visible=True),
|
280 |
+
inputs=None,
|
281 |
+
outputs=edit_interface
|
282 |
+
)
|
283 |
+
|
284 |
# Define delete functionality
|
285 |
def confirm_delete(char_name):
|
286 |
return f"Are you sure you want to delete '{char_name}'?"
|
|
|
327 |
|
328 |
components.append("") # Placeholder for spacing
|
329 |
|
330 |
+
return components
|
331 |
|
332 |
characters_container.load(fn=list_characters, outputs=characters_container)
|
333 |
|
|
|
379 |
|
380 |
def refresh_characters():
|
381 |
new_options = get_character_options()
|
382 |
+
return gr.CheckboxGroup.update(choices=new_options)
|
383 |
|
384 |
refresh_characters_button.click(refresh_characters, outputs=character_select)
|
385 |
|
|
|
498 |
slider_value = min(1, max_tags)
|
499 |
# Update the tag display and slider
|
500 |
tag_display, tag_num = tag_displays[var_name]
|
501 |
+
updates.append(gr.Markdown.update(value=f"**Tags:** {tags_string}"))
|
502 |
+
updates.append(gr.Slider.update(maximum=max_tags, value=slider_value))
|
503 |
return updates
|
504 |
|
505 |
# Prepare the outputs list
|