Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -134,18 +134,76 @@ def prompt_generator_app(data_manager):
|
|
134 |
# For Character Selection
|
135 |
with gr.Group():
|
136 |
gr.Markdown("### Character Selection")
|
137 |
-
|
138 |
-
|
139 |
-
character_select = gr.
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
143 |
# Number of people in the scene
|
144 |
num_people = gr.Slider(minimum=0, maximum=10, step=1, value=1, label="Number of People in the Scene")
|
145 |
|
|
|
|
|
|
|
146 |
generate_button = gr.Button("Generate Prompt")
|
147 |
prompt_output = gr.Textbox(label="Generated Prompt", lines=5)
|
148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
def generate_prompt(*args):
|
150 |
# args correspond to inputs in the order they are defined
|
151 |
# Need to map args to variables
|
@@ -163,21 +221,23 @@ def prompt_generator_app(data_manager):
|
|
163 |
prompt_tags.extend(selected_tags)
|
164 |
|
165 |
# Handle Characters
|
166 |
-
|
167 |
random_chars = args[arg_idx + 1]
|
168 |
num_random_chars = args[arg_idx + 2]
|
169 |
num_people_in_scene = args[arg_idx + 3]
|
170 |
|
171 |
-
characters = data_manager.get_characters()
|
172 |
-
|
173 |
arg_idx += 4
|
174 |
|
|
|
|
|
|
|
|
|
175 |
selected_chars = []
|
176 |
if random_chars:
|
177 |
num = min(len(characters), int(num_random_chars))
|
178 |
selected_chars = random.sample(characters, num)
|
179 |
else:
|
180 |
-
selected_chars = [char for char in characters if char['name'] in
|
181 |
|
182 |
# Adjust the number of people in the scene
|
183 |
# You can modify how additional characters are added if needed
|
@@ -201,7 +261,7 @@ def prompt_generator_app(data_manager):
|
|
201 |
inputs_list = []
|
202 |
for var_name in inputs:
|
203 |
inputs_list.append(inputs[var_name])
|
204 |
-
inputs_list.extend([
|
205 |
|
206 |
generate_button.click(generate_prompt, inputs=inputs_list, outputs=prompt_output)
|
207 |
|
|
|
134 |
# For Character Selection
|
135 |
with gr.Group():
|
136 |
gr.Markdown("### Character Selection")
|
137 |
+
|
138 |
+
# Create placeholders for dynamic components
|
139 |
+
character_select = gr.State([])
|
140 |
+
selected_character_names = gr.State([])
|
141 |
+
|
142 |
+
characters_display = gr.HTML()
|
143 |
+
refresh_characters_button = gr.Button("Refresh Character List")
|
144 |
+
|
145 |
# Number of people in the scene
|
146 |
num_people = gr.Slider(minimum=0, maximum=10, step=1, value=1, label="Number of People in the Scene")
|
147 |
|
148 |
+
random_characters = gr.Checkbox(label="Select Random Characters")
|
149 |
+
num_characters = gr.Slider(minimum=0, maximum=10, step=1, value=1, label="Number of Characters (if random)")
|
150 |
+
|
151 |
generate_button = gr.Button("Generate Prompt")
|
152 |
prompt_output = gr.Textbox(label="Generated Prompt", lines=5)
|
153 |
|
154 |
+
# Function to display characters with images and selection checkboxes
|
155 |
+
def display_characters():
|
156 |
+
characters = data_manager.get_characters()
|
157 |
+
if not characters:
|
158 |
+
return "", []
|
159 |
+
|
160 |
+
# Build HTML content for characters display
|
161 |
+
html_content = "<div style='display: flex; flex-wrap: wrap;'>"
|
162 |
+
for idx, char in enumerate(characters):
|
163 |
+
image_path = char.get('image_path', '')
|
164 |
+
image_url = ''
|
165 |
+
if image_path and os.path.exists(image_path):
|
166 |
+
with open(image_path, "rb") as img_file:
|
167 |
+
image_data = base64.b64encode(img_file.read()).decode('utf-8')
|
168 |
+
image_url = f"data:image/png;base64,{image_data}"
|
169 |
+
else:
|
170 |
+
image_url = ""
|
171 |
+
|
172 |
+
# Each character card
|
173 |
+
html_content += f"""
|
174 |
+
<div style='margin: 10px; text-align: center; width: 150px;'>
|
175 |
+
<img src='{image_url}' style='width: 100%; height: auto;'/>
|
176 |
+
<p>{char['name']}</p>
|
177 |
+
<input type='checkbox' name='character_select' value='{char['name']}' onchange='updateSelectedCharacters()'/>
|
178 |
+
</div>
|
179 |
+
"""
|
180 |
+
html_content += "</div>"
|
181 |
+
|
182 |
+
# JavaScript function to update selected characters
|
183 |
+
html_content += """
|
184 |
+
<script>
|
185 |
+
function updateSelectedCharacters() {
|
186 |
+
let selected = [];
|
187 |
+
let checkboxes = document.getElementsByName('character_select');
|
188 |
+
for (let i = 0; i < checkboxes.length; i++) {
|
189 |
+
if (checkboxes[i].checked) {
|
190 |
+
selected.push(checkboxes[i].value);
|
191 |
+
}
|
192 |
+
}
|
193 |
+
gradioApp().querySelector('#selected_character_names').value = JSON.stringify(selected);
|
194 |
+
}
|
195 |
+
</script>
|
196 |
+
"""
|
197 |
+
|
198 |
+
return html_content, []
|
199 |
+
|
200 |
+
# Display characters on load and when refresh is clicked
|
201 |
+
characters_display.update(display_characters)
|
202 |
+
refresh_characters_button.click(display_characters, outputs=characters_display)
|
203 |
+
|
204 |
+
# Hidden input to store selected character names
|
205 |
+
hidden_selected_characters = gr.Textbox(visible=False, elem_id='selected_character_names')
|
206 |
+
|
207 |
def generate_prompt(*args):
|
208 |
# args correspond to inputs in the order they are defined
|
209 |
# Need to map args to variables
|
|
|
221 |
prompt_tags.extend(selected_tags)
|
222 |
|
223 |
# Handle Characters
|
224 |
+
selected_characters_json = args[arg_idx]
|
225 |
random_chars = args[arg_idx + 1]
|
226 |
num_random_chars = args[arg_idx + 2]
|
227 |
num_people_in_scene = args[arg_idx + 3]
|
228 |
|
|
|
|
|
229 |
arg_idx += 4
|
230 |
|
231 |
+
selected_character_names_list = json.loads(selected_characters_json) if selected_characters_json else []
|
232 |
+
|
233 |
+
characters = data_manager.get_characters()
|
234 |
+
|
235 |
selected_chars = []
|
236 |
if random_chars:
|
237 |
num = min(len(characters), int(num_random_chars))
|
238 |
selected_chars = random.sample(characters, num)
|
239 |
else:
|
240 |
+
selected_chars = [char for char in characters if char['name'] in selected_character_names_list]
|
241 |
|
242 |
# Adjust the number of people in the scene
|
243 |
# You can modify how additional characters are added if needed
|
|
|
261 |
inputs_list = []
|
262 |
for var_name in inputs:
|
263 |
inputs_list.append(inputs[var_name])
|
264 |
+
inputs_list.extend([hidden_selected_characters, random_characters, num_characters, num_people])
|
265 |
|
266 |
generate_button.click(generate_prompt, inputs=inputs_list, outputs=prompt_output)
|
267 |
|