throaway2854 commited on
Commit
93e965c
1 Parent(s): 7251b41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -55
app.py CHANGED
@@ -7,7 +7,9 @@ import base64
7
  import io
8
 
9
  class DataManager:
10
- def __init__(self, characters_file='characters.json', persistent_tags_file='persistent_tags.json', images_folder='character_images'):
 
 
11
  self.characters_file = characters_file
12
  self.persistent_tags_file = persistent_tags_file
13
  self.images_folder = images_folder
@@ -138,62 +140,28 @@ 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
- # 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", columns=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
- for idx, char in enumerate(characters):
159
- image_path = char.get('image_path', '')
160
- if image_path and os.path.exists(image_path):
161
- image_url = image_path
162
- else:
163
- image_url = None # Or a placeholder image
164
- # Adding the character's name and gender as a caption
165
- caption = f"{char['name']} ({char['gender']})"
166
- character_images.append((image_url, caption))
167
- return character_images, 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, 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, characters = load_characters()
184
- if character_images:
185
- character_select.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)")
197
 
198
  generate_button = gr.Button("Generate Prompt")
199
  prompt_output = gr.Textbox(label="Generated Prompt", lines=5)
@@ -213,7 +181,7 @@ def prompt_generator_app(data_manager):
213
  prompt_tags.extend(selected_tags)
214
 
215
  # Handle Characters
216
- selected_chars = args[arg_idx]
217
  random_chars = args[arg_idx + 1]
218
  num_random_chars = args[arg_idx + 2]
219
 
@@ -225,8 +193,14 @@ def prompt_generator_app(data_manager):
225
  num = min(len(characters), int(num_random_chars))
226
  selected_chars = random.sample(characters, num)
227
  else:
228
- # selected_chars is already set from the selected characters in the gallery
229
- selected_chars = selected_chars or []
 
 
 
 
 
 
230
 
231
  # Determine the number of boys and girls
232
  num_girls = sum(1 for char in selected_chars if char.get('gender') == 'Girl')
@@ -283,8 +257,8 @@ def prompt_generator_app(data_manager):
283
  inputs_list = []
284
  for var_name in inputs:
285
  inputs_list.append(inputs[var_name])
286
- # Add selected characters state to inputs
287
- inputs_list.extend([selected_characters_state, random_characters, num_characters])
288
 
289
  generate_button.click(generate_prompt, inputs=inputs_list, outputs=prompt_output)
290
 
 
7
  import io
8
 
9
  class DataManager:
10
+ def __init__(self, characters_file='characters.json',
11
+ persistent_tags_file='persistent_tags.json',
12
+ images_folder='character_images'):
13
  self.characters_file = characters_file
14
  self.persistent_tags_file = persistent_tags_file
15
  self.images_folder = images_folder
 
140
  with gr.Group():
141
  gr.Markdown("### Character Selection")
142
 
143
+ # Get the list of characters
144
+ def get_character_options():
145
+ characters = data_manager.get_characters()
146
+ character_options = []
147
+ for char in characters:
148
+ option_label = f"{char['name']} ({char['gender']})"
149
+ character_options.append(option_label)
150
+ return character_options
151
 
152
+ character_options = get_character_options()
153
+ character_select = gr.CheckboxGroup(choices=character_options, label="Select Characters", interactive=True)
154
 
155
+ refresh_characters_button = gr.Button("Refresh Character List")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
 
157
  def refresh_characters():
158
+ new_options = get_character_options()
159
+ return gr.update(choices=new_options)
 
 
 
 
 
 
 
 
 
 
 
160
 
161
+ refresh_characters_button.click(refresh_characters, outputs=character_select)
 
162
 
163
  random_characters = gr.Checkbox(label="Select Random Characters")
164
+ num_characters = gr.Slider(minimum=1, maximum=10, step=1, value=1, label="Number of Characters (if random)")
165
 
166
  generate_button = gr.Button("Generate Prompt")
167
  prompt_output = gr.Textbox(label="Generated Prompt", lines=5)
 
181
  prompt_tags.extend(selected_tags)
182
 
183
  # Handle Characters
184
+ selected_character_options = args[arg_idx]
185
  random_chars = args[arg_idx + 1]
186
  num_random_chars = args[arg_idx + 2]
187
 
 
193
  num = min(len(characters), int(num_random_chars))
194
  selected_chars = random.sample(characters, num)
195
  else:
196
+ # Extract selected character names from options
197
+ selected_chars = []
198
+ for option in selected_character_options:
199
+ name = option.split(' (')[0]
200
+ for char in characters:
201
+ if char['name'] == name:
202
+ selected_chars.append(char)
203
+ break
204
 
205
  # Determine the number of boys and girls
206
  num_girls = sum(1 for char in selected_chars if char.get('gender') == 'Girl')
 
257
  inputs_list = []
258
  for var_name in inputs:
259
  inputs_list.append(inputs[var_name])
260
+ # Add character_select directly to inputs
261
+ inputs_list.extend([character_select, random_characters, num_characters])
262
 
263
  generate_button.click(generate_prompt, inputs=inputs_list, outputs=prompt_output)
264