Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -138,37 +138,59 @@ def prompt_generator_app(data_manager):
|
|
138 |
with gr.Group():
|
139 |
gr.Markdown("### Character Selection")
|
140 |
|
141 |
-
# Initialize selected_characters_state
|
142 |
selected_characters_state = gr.State([])
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
else:
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
image_url = image_path
|
156 |
-
else:
|
157 |
-
image_url = None # Or a placeholder image
|
158 |
-
character_images.append(image_url)
|
159 |
-
character_labels.append(f"{char['name']} ({char['gender']})")
|
160 |
-
character_select = gr.Gallery(label="Select Characters").style(grid=[3], height='auto')
|
161 |
-
|
162 |
-
# Function to update selected characters
|
163 |
-
def update_selected_characters(selected_indices):
|
164 |
-
selected_indices = selected_indices or []
|
165 |
-
selected_chars = [characters[i] for i in selected_indices]
|
166 |
-
return selected_chars
|
167 |
-
|
168 |
-
# Update the gallery
|
169 |
-
character_select.render(character_images)
|
170 |
-
|
171 |
-
character_select.select(fn=update_selected_characters, inputs=[character_select], outputs=[selected_characters_state])
|
172 |
|
173 |
random_characters = gr.Checkbox(label="Select Random Characters")
|
174 |
num_characters = gr.Slider(minimum=0, maximum=10, step=1, value=1, label="Number of Characters (if random)")
|
|
|
138 |
with gr.Group():
|
139 |
gr.Markdown("### Character Selection")
|
140 |
|
141 |
+
# Initialize selected_characters_state
|
142 |
selected_characters_state = gr.State([])
|
143 |
|
144 |
+
# Add the Refresh Character List button
|
145 |
+
refresh_characters_button = gr.Button("Refresh Character List")
|
146 |
+
|
147 |
+
# Create a placeholder for the character_select component
|
148 |
+
character_select = gr.Gallery(label="Select Characters").style(grid=[3], height='auto')
|
149 |
+
|
150 |
+
# Load and display characters
|
151 |
+
def load_characters():
|
152 |
+
characters = data_manager.get_characters()
|
153 |
+
if not characters:
|
154 |
+
return [], [], []
|
155 |
+
else:
|
156 |
+
# Build character images and labels
|
157 |
+
character_images = []
|
158 |
+
character_indices = []
|
159 |
+
for idx, char in enumerate(characters):
|
160 |
+
image_path = char.get('image_path', '')
|
161 |
+
if image_path and os.path.exists(image_path):
|
162 |
+
image_url = image_path
|
163 |
+
else:
|
164 |
+
image_url = None # Or a placeholder image
|
165 |
+
character_images.append(image_url)
|
166 |
+
character_indices.append(idx)
|
167 |
+
return character_images, character_indices, characters
|
168 |
+
|
169 |
+
# Function to update selected characters
|
170 |
+
def update_selected_characters(selected_indices):
|
171 |
+
characters = data_manager.get_characters()
|
172 |
+
selected_indices = selected_indices or []
|
173 |
+
selected_chars = [characters[i] for i in selected_indices]
|
174 |
+
return selected_chars
|
175 |
+
|
176 |
+
# Function to refresh characters
|
177 |
+
def refresh_characters():
|
178 |
+
character_images, character_indices, characters = load_characters()
|
179 |
+
# Update character_select with new images
|
180 |
+
return gr.update(value=character_images)
|
181 |
+
|
182 |
+
# Load initial characters
|
183 |
+
character_images, character_indices, characters = load_characters()
|
184 |
+
if character_images:
|
185 |
+
character_select.update(value=character_images)
|
186 |
else:
|
187 |
+
gr.Markdown("No characters available. Please add characters in the Character Creation tab.")
|
188 |
+
|
189 |
+
# Update selected characters when selection changes
|
190 |
+
character_select.select(fn=update_selected_characters, inputs=None, outputs=selected_characters_state)
|
191 |
+
|
192 |
+
# Update character gallery when refresh button is clicked
|
193 |
+
refresh_characters_button.click(fn=refresh_characters, inputs=None, outputs=character_select)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
random_characters = gr.Checkbox(label="Select Random Characters")
|
196 |
num_characters = gr.Slider(minimum=0, maximum=10, step=1, value=1, label="Number of Characters (if random)")
|