Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,6 @@ import tempfile
|
|
10 |
import numpy as np
|
11 |
|
12 |
def save_dataset_to_zip(dataset_name, dataset):
|
13 |
-
# Create a temporary directory
|
14 |
temp_dir = tempfile.mkdtemp()
|
15 |
dataset_path = os.path.join(temp_dir, dataset_name)
|
16 |
os.makedirs(dataset_path, exist_ok=True)
|
@@ -136,9 +135,14 @@ with gr.Blocks() as demo:
|
|
136 |
datasets = gr.State({})
|
137 |
current_dataset_name = gr.State("")
|
138 |
current_page_number = gr.State(0)
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
# Tabs as before
|
144 |
with gr.Tabs():
|
@@ -208,20 +212,20 @@ with gr.Blocks() as demo:
|
|
208 |
html_content = display_dataset_html(dataset, page_number=page_number)
|
209 |
# Update entry_selector options
|
210 |
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
211 |
-
return datasets, page_number, gr.update(value=html_content), entry_options, f"Entry added to dataset '{current_dataset_name}'."
|
212 |
|
213 |
add_button.click(
|
214 |
add_entry,
|
215 |
inputs=[image_input, prompt_input, current_dataset_name, datasets],
|
216 |
-
outputs=[datasets, current_page_number, gr.HTML(),
|
217 |
)
|
218 |
|
219 |
with gr.TabItem("Edit / Delete Entry"):
|
220 |
with gr.Column():
|
221 |
selected_image = gr.Image(label="Selected Image", interactive=False, type="numpy")
|
222 |
selected_prompt = gr.Textbox(label="Current Prompt", interactive=False)
|
223 |
-
#
|
224 |
-
entry_selector
|
225 |
new_prompt_input = gr.Textbox(label="New Prompt (for Edit)")
|
226 |
with gr.Row():
|
227 |
edit_button = gr.Button("Edit Entry")
|
@@ -262,7 +266,7 @@ with gr.Blocks() as demo:
|
|
262 |
edit_button.click(
|
263 |
edit_entry,
|
264 |
inputs=[entry_selector, new_prompt_input, current_dataset_name, datasets, current_page_number],
|
265 |
-
outputs=[datasets, gr.HTML(), entry_selector, new_prompt_input, message_box]
|
266 |
)
|
267 |
|
268 |
def delete_entry(entry_option, current_dataset_name, datasets, current_page_number):
|
@@ -276,12 +280,12 @@ with gr.Blocks() as demo:
|
|
276 |
html_content = display_dataset_html(dataset, page_number=current_page_number)
|
277 |
# Update entry_selector options
|
278 |
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
279 |
-
return datasets, gr.update(value=html_content), gr.update(choices=entry_options), gr.update(value=None), gr.update(value=""),
|
280 |
|
281 |
delete_button.click(
|
282 |
delete_entry,
|
283 |
inputs=[entry_selector, current_dataset_name, datasets, current_page_number],
|
284 |
-
outputs=[datasets, gr.HTML(), entry_selector, selected_image, selected_prompt, message_box]
|
285 |
)
|
286 |
|
287 |
with gr.TabItem("Download Dataset"):
|
@@ -321,14 +325,14 @@ with gr.Blocks() as demo:
|
|
321 |
html_content = display_dataset_html(dataset, page_number=0)
|
322 |
# Update entry_selector options
|
323 |
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
324 |
-
return dataset_name, 0, gr.update(value=html_content), entry_options, ""
|
325 |
else:
|
326 |
-
return "", 0, gr.update(value="<div>Select a dataset.</div>"), [], ""
|
327 |
|
328 |
dataset_selector.change(
|
329 |
select_dataset,
|
330 |
inputs=[dataset_selector, datasets],
|
331 |
-
outputs=[current_dataset_name, current_page_number, dataset_html,
|
332 |
)
|
333 |
|
334 |
def change_page(action, current_page_number, datasets, current_dataset_name):
|
@@ -357,35 +361,14 @@ with gr.Blocks() as demo:
|
|
357 |
outputs=[current_page_number, dataset_html, message_box]
|
358 |
)
|
359 |
|
360 |
-
# Initialize dataset_selector
|
361 |
def initialize_components(datasets):
|
362 |
-
return gr.update(choices=list(datasets.keys()))
|
363 |
|
364 |
demo.load(
|
365 |
initialize_components,
|
366 |
inputs=[datasets],
|
367 |
-
outputs=[dataset_selector]
|
368 |
-
)
|
369 |
-
|
370 |
-
# Function to update entry_selector options
|
371 |
-
def update_entry_selector(entry_options):
|
372 |
-
entry_selector.update(choices=entry_options)
|
373 |
-
|
374 |
-
# Use gr.Variable to pass entry_options between functions
|
375 |
-
gr.Variable(name="entry_options_var")
|
376 |
-
|
377 |
-
# Update entry_selector options when dataset is selected
|
378 |
-
dataset_selector.change(
|
379 |
-
lambda entry_options: update_entry_selector(entry_options),
|
380 |
-
inputs=[gr.Variable("entry_options_var")],
|
381 |
-
outputs=[]
|
382 |
-
)
|
383 |
-
|
384 |
-
# Update entry_selector options when an entry is added
|
385 |
-
add_button.click(
|
386 |
-
lambda entry_options: update_entry_selector(entry_options),
|
387 |
-
inputs=[gr.Variable("entry_options_var")],
|
388 |
-
outputs=[]
|
389 |
)
|
390 |
|
391 |
demo.launch()
|
|
|
10 |
import numpy as np
|
11 |
|
12 |
def save_dataset_to_zip(dataset_name, dataset):
|
|
|
13 |
temp_dir = tempfile.mkdtemp()
|
14 |
dataset_path = os.path.join(temp_dir, dataset_name)
|
15 |
os.makedirs(dataset_path, exist_ok=True)
|
|
|
135 |
datasets = gr.State({})
|
136 |
current_dataset_name = gr.State("")
|
137 |
current_page_number = gr.State(0)
|
138 |
+
|
139 |
+
# Define entry_selector here but do not place it in the interface yet
|
140 |
+
entry_selector = gr.Dropdown(label="Select Entry to Edit/Delete")
|
141 |
+
|
142 |
+
# Top-level components
|
143 |
+
with gr.Column():
|
144 |
+
dataset_selector = gr.Dropdown(label="Select Dataset", interactive=True)
|
145 |
+
message_box = gr.Textbox(interactive=False, label="Message")
|
146 |
|
147 |
# Tabs as before
|
148 |
with gr.Tabs():
|
|
|
212 |
html_content = display_dataset_html(dataset, page_number=page_number)
|
213 |
# Update entry_selector options
|
214 |
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
215 |
+
return datasets, page_number, gr.update(value=html_content), gr.update(choices=entry_options), f"Entry added to dataset '{current_dataset_name}'."
|
216 |
|
217 |
add_button.click(
|
218 |
add_entry,
|
219 |
inputs=[image_input, prompt_input, current_dataset_name, datasets],
|
220 |
+
outputs=[datasets, current_page_number, gr.HTML.update(), entry_selector, message_box]
|
221 |
)
|
222 |
|
223 |
with gr.TabItem("Edit / Delete Entry"):
|
224 |
with gr.Column():
|
225 |
selected_image = gr.Image(label="Selected Image", interactive=False, type="numpy")
|
226 |
selected_prompt = gr.Textbox(label="Current Prompt", interactive=False)
|
227 |
+
# Include entry_selector in the layout
|
228 |
+
entry_selector
|
229 |
new_prompt_input = gr.Textbox(label="New Prompt (for Edit)")
|
230 |
with gr.Row():
|
231 |
edit_button = gr.Button("Edit Entry")
|
|
|
266 |
edit_button.click(
|
267 |
edit_entry,
|
268 |
inputs=[entry_selector, new_prompt_input, current_dataset_name, datasets, current_page_number],
|
269 |
+
outputs=[datasets, gr.HTML.update(), entry_selector, new_prompt_input, message_box]
|
270 |
)
|
271 |
|
272 |
def delete_entry(entry_option, current_dataset_name, datasets, current_page_number):
|
|
|
280 |
html_content = display_dataset_html(dataset, page_number=current_page_number)
|
281 |
# Update entry_selector options
|
282 |
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
283 |
+
return datasets, gr.update(value=html_content), gr.update(choices=entry_options), gr.update(value=None), gr.update(value=""), f"Entry {index} deleted."
|
284 |
|
285 |
delete_button.click(
|
286 |
delete_entry,
|
287 |
inputs=[entry_selector, current_dataset_name, datasets, current_page_number],
|
288 |
+
outputs=[datasets, gr.HTML.update(), entry_selector, selected_image, selected_prompt, message_box]
|
289 |
)
|
290 |
|
291 |
with gr.TabItem("Download Dataset"):
|
|
|
325 |
html_content = display_dataset_html(dataset, page_number=0)
|
326 |
# Update entry_selector options
|
327 |
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
328 |
+
return dataset_name, 0, gr.update(value=html_content), gr.update(choices=entry_options), ""
|
329 |
else:
|
330 |
+
return "", 0, gr.update(value="<div>Select a dataset.</div>"), gr.update(choices=[]), ""
|
331 |
|
332 |
dataset_selector.change(
|
333 |
select_dataset,
|
334 |
inputs=[dataset_selector, datasets],
|
335 |
+
outputs=[current_dataset_name, current_page_number, dataset_html, entry_selector, message_box]
|
336 |
)
|
337 |
|
338 |
def change_page(action, current_page_number, datasets, current_dataset_name):
|
|
|
361 |
outputs=[current_page_number, dataset_html, message_box]
|
362 |
)
|
363 |
|
364 |
+
# Initialize dataset_selector and entry_selector
|
365 |
def initialize_components(datasets):
|
366 |
+
return gr.update(choices=list(datasets.keys())), gr.update(choices=[])
|
367 |
|
368 |
demo.load(
|
369 |
initialize_components,
|
370 |
inputs=[datasets],
|
371 |
+
outputs=[dataset_selector, entry_selector]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
372 |
)
|
373 |
|
374 |
demo.launch()
|