Update app.py
Browse files
app.py
CHANGED
|
@@ -76,7 +76,6 @@ def load_dataset_from_zip(zip_file_path):
|
|
| 76 |
dataset_name = dataset_name_guess
|
| 77 |
dataset_path = temp_dir
|
| 78 |
|
| 79 |
-
images_dir = os.path.join(dataset_path, 'images')
|
| 80 |
annotations_path = os.path.join(dataset_path, 'annotations.jsonl')
|
| 81 |
dataset = []
|
| 82 |
|
|
@@ -113,7 +112,6 @@ def display_dataset_html(dataset, page_number=0, items_per_page=5):
|
|
| 113 |
start_idx = page_number * items_per_page
|
| 114 |
end_idx = start_idx + items_per_page
|
| 115 |
dataset_slice = dataset[start_idx:end_idx]
|
| 116 |
-
total_pages = (len(dataset) - 1) // items_per_page + 1
|
| 117 |
html_content = '''
|
| 118 |
<div style="display: flex; overflow-x: auto; padding: 10px; border: 1px solid #ccc;">
|
| 119 |
'''
|
|
@@ -142,47 +140,179 @@ with gr.Blocks() as demo:
|
|
| 142 |
entry_selector = gr.Dropdown(label="Select Entry to Edit/Delete")
|
| 143 |
message_box = gr.Textbox(interactive=False, label="Message")
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
with gr.Column():
|
| 152 |
-
gr.
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
return gr.update(), f"Dataset '{dataset_name}' already exists."
|
| 178 |
-
datasets[dataset_name] = dataset
|
| 179 |
-
return gr.update(choices=list(datasets.keys()), value=dataset_name), f"Dataset '{dataset_name}' uploaded."
|
| 180 |
-
|
| 181 |
-
upload_button.click(
|
| 182 |
-
upload_dataset,
|
| 183 |
-
inputs=[upload_input, datasets],
|
| 184 |
-
outputs=[dataset_selector, message_box]
|
| 185 |
-
)
|
| 186 |
|
| 187 |
def select_dataset(dataset_name, datasets):
|
| 188 |
if dataset_name in datasets:
|
|
@@ -197,137 +327,9 @@ with gr.Blocks() as demo:
|
|
| 197 |
dataset_selector.change(
|
| 198 |
select_dataset,
|
| 199 |
inputs=[dataset_selector, datasets],
|
| 200 |
-
outputs=[current_dataset_name, current_page_number,
|
| 201 |
)
|
| 202 |
|
| 203 |
-
with gr.Tab("Add Entry"):
|
| 204 |
-
with gr.Row():
|
| 205 |
-
image_input = gr.Image(label="Upload Image", type="numpy")
|
| 206 |
-
prompt_input = gr.Textbox(label="Prompt")
|
| 207 |
-
add_button = gr.Button("Add Entry")
|
| 208 |
-
|
| 209 |
-
def add_entry(image_data, prompt, current_dataset_name, datasets):
|
| 210 |
-
if not current_dataset_name:
|
| 211 |
-
return datasets, gr.update(), gr.update(), gr.update(), "No dataset selected."
|
| 212 |
-
if image_data is None or not prompt:
|
| 213 |
-
return datasets, gr.update(), gr.update(), gr.update(), "Please provide both an image and a prompt."
|
| 214 |
-
# Convert image_data to base64
|
| 215 |
-
image = Image.fromarray(image_data.astype('uint8'))
|
| 216 |
-
buffered = BytesIO()
|
| 217 |
-
image.save(buffered, format="PNG")
|
| 218 |
-
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 219 |
-
img_data = f"data:image/png;base64,{img_str}"
|
| 220 |
-
datasets[current_dataset_name].append({'image': img_data, 'prompt': prompt})
|
| 221 |
-
dataset = datasets[current_dataset_name]
|
| 222 |
-
# Reset page number to 0 when a new entry is added
|
| 223 |
-
page_number = 0
|
| 224 |
-
html_content = display_dataset_html(dataset, page_number=page_number)
|
| 225 |
-
# Update entry_selector options
|
| 226 |
-
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
| 227 |
-
return datasets, page_number, gr.update(value=html_content), gr.update(choices=entry_options), f"Entry added to dataset '{current_dataset_name}'."
|
| 228 |
-
|
| 229 |
-
add_button.click(
|
| 230 |
-
add_entry,
|
| 231 |
-
inputs=[image_input, prompt_input, current_dataset_name, datasets],
|
| 232 |
-
outputs=[datasets, current_page_number, gr.HTML(), entry_selector, message_box]
|
| 233 |
-
)
|
| 234 |
-
|
| 235 |
-
with gr.Tab("Edit / Delete Entry"):
|
| 236 |
-
with gr.Column():
|
| 237 |
-
selected_image = gr.Image(label="Selected Image", interactive=False, type="numpy")
|
| 238 |
-
selected_prompt = gr.Textbox(label="Current Prompt", interactive=False)
|
| 239 |
-
new_prompt_input = gr.Textbox(label="New Prompt (for Edit)")
|
| 240 |
-
with gr.Row():
|
| 241 |
-
edit_button = gr.Button("Edit Entry")
|
| 242 |
-
delete_button = gr.Button("Delete Entry")
|
| 243 |
-
|
| 244 |
-
def update_selected_entry(entry_option, current_dataset_name, datasets):
|
| 245 |
-
if not current_dataset_name or not entry_option:
|
| 246 |
-
return gr.update(), gr.update()
|
| 247 |
-
index = int(entry_option.split(":")[0])
|
| 248 |
-
entry = datasets[current_dataset_name][index]
|
| 249 |
-
image_data = entry['image']
|
| 250 |
-
prompt = entry['prompt']
|
| 251 |
-
# Decode base64 image data to numpy array
|
| 252 |
-
image_bytes = base64.b64decode(image_data.split(",")[1])
|
| 253 |
-
image = Image.open(BytesIO(image_bytes))
|
| 254 |
-
image_array = np.array(image)
|
| 255 |
-
return gr.update(value=image_array), gr.update(value=prompt)
|
| 256 |
-
|
| 257 |
-
entry_selector.change(
|
| 258 |
-
update_selected_entry,
|
| 259 |
-
inputs=[entry_selector, current_dataset_name, datasets],
|
| 260 |
-
outputs=[selected_image, selected_prompt]
|
| 261 |
-
)
|
| 262 |
-
|
| 263 |
-
def edit_entry(entry_option, new_prompt, current_dataset_name, datasets, current_page_number):
|
| 264 |
-
if not current_dataset_name:
|
| 265 |
-
return datasets, gr.update(), gr.update(), gr.update(), gr.update(), "No dataset selected."
|
| 266 |
-
if not entry_option or not new_prompt.strip():
|
| 267 |
-
return datasets, gr.update(), gr.update(), gr.update(), gr.update(), "Please select an entry and provide a new prompt."
|
| 268 |
-
index = int(entry_option.split(":")[0])
|
| 269 |
-
datasets[current_dataset_name][index]['prompt'] = new_prompt
|
| 270 |
-
dataset = datasets[current_dataset_name]
|
| 271 |
-
html_content = display_dataset_html(dataset, page_number=current_page_number)
|
| 272 |
-
# Update entry_selector options
|
| 273 |
-
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
| 274 |
-
return datasets, gr.update(value=html_content), gr.update(choices=entry_options), gr.update(value=""), gr.update(), f"Entry {index} updated."
|
| 275 |
-
|
| 276 |
-
edit_button.click(
|
| 277 |
-
edit_entry,
|
| 278 |
-
inputs=[entry_selector, new_prompt_input, current_dataset_name, datasets, current_page_number],
|
| 279 |
-
outputs=[datasets, gr.HTML(), entry_selector, new_prompt_input, message_box]
|
| 280 |
-
)
|
| 281 |
-
|
| 282 |
-
def delete_entry(entry_option, current_dataset_name, datasets, current_page_number):
|
| 283 |
-
if not current_dataset_name:
|
| 284 |
-
return datasets, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), "No dataset selected."
|
| 285 |
-
if not entry_option:
|
| 286 |
-
return datasets, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), "Please select an entry to delete."
|
| 287 |
-
index = int(entry_option.split(":")[0])
|
| 288 |
-
del datasets[current_dataset_name][index]
|
| 289 |
-
dataset = datasets[current_dataset_name]
|
| 290 |
-
html_content = display_dataset_html(dataset, page_number=current_page_number)
|
| 291 |
-
# Update entry_selector options
|
| 292 |
-
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
| 293 |
-
return datasets, gr.update(value=html_content), gr.update(choices=entry_options), gr.update(value=None), gr.update(value=""), message_box, f"Entry {index} deleted."
|
| 294 |
-
|
| 295 |
-
delete_button.click(
|
| 296 |
-
delete_entry,
|
| 297 |
-
inputs=[entry_selector, current_dataset_name, datasets, current_page_number],
|
| 298 |
-
outputs=[datasets, gr.HTML(), entry_selector, selected_image, selected_prompt, message_box]
|
| 299 |
-
)
|
| 300 |
-
|
| 301 |
-
with gr.Tab("Download Dataset"):
|
| 302 |
-
download_button = gr.Button("Download Dataset")
|
| 303 |
-
download_output = gr.File(label="Download Zip", interactive=False)
|
| 304 |
-
def download_dataset(current_dataset_name, datasets):
|
| 305 |
-
if not current_dataset_name:
|
| 306 |
-
return None, "No dataset selected."
|
| 307 |
-
if not datasets[current_dataset_name]:
|
| 308 |
-
return None, "Dataset is empty."
|
| 309 |
-
zip_buffer = save_dataset_to_zip(current_dataset_name, datasets[current_dataset_name])
|
| 310 |
-
# Write zip_buffer to a temporary file
|
| 311 |
-
temp_dir = tempfile.mkdtemp()
|
| 312 |
-
zip_path = os.path.join(temp_dir, f"{current_dataset_name}.zip")
|
| 313 |
-
with open(zip_path, 'wb') as f:
|
| 314 |
-
f.write(zip_buffer.getvalue())
|
| 315 |
-
return zip_path, f"Dataset '{current_dataset_name}' is ready for download."
|
| 316 |
-
|
| 317 |
-
download_button.click(
|
| 318 |
-
download_dataset,
|
| 319 |
-
inputs=[current_dataset_name, datasets],
|
| 320 |
-
outputs=[download_output, message_box]
|
| 321 |
-
)
|
| 322 |
-
|
| 323 |
-
# Dataset Viewer and Pagination Controls at the Bottom
|
| 324 |
-
with gr.Column():
|
| 325 |
-
gr.Markdown("### Dataset Viewer")
|
| 326 |
-
dataset_html = gr.HTML()
|
| 327 |
-
with gr.Row():
|
| 328 |
-
prev_button = gr.Button("Previous Page")
|
| 329 |
-
next_button = gr.Button("Next Page")
|
| 330 |
-
|
| 331 |
def change_page(action, current_page_number, datasets, current_dataset_name):
|
| 332 |
if not current_dataset_name:
|
| 333 |
return current_page_number, gr.update(), "No dataset selected."
|
|
|
|
| 76 |
dataset_name = dataset_name_guess
|
| 77 |
dataset_path = temp_dir
|
| 78 |
|
|
|
|
| 79 |
annotations_path = os.path.join(dataset_path, 'annotations.jsonl')
|
| 80 |
dataset = []
|
| 81 |
|
|
|
|
| 112 |
start_idx = page_number * items_per_page
|
| 113 |
end_idx = start_idx + items_per_page
|
| 114 |
dataset_slice = dataset[start_idx:end_idx]
|
|
|
|
| 115 |
html_content = '''
|
| 116 |
<div style="display: flex; overflow-x: auto; padding: 10px; border: 1px solid #ccc;">
|
| 117 |
'''
|
|
|
|
| 140 |
entry_selector = gr.Dropdown(label="Select Entry to Edit/Delete")
|
| 141 |
message_box = gr.Textbox(interactive=False, label="Message")
|
| 142 |
|
| 143 |
+
# Tabs as before
|
| 144 |
+
with gr.Tabs():
|
| 145 |
+
with gr.TabItem("Create / Upload Dataset"):
|
| 146 |
+
with gr.Row():
|
| 147 |
+
with gr.Column():
|
| 148 |
+
gr.Markdown("### Create a New Dataset")
|
| 149 |
+
dataset_name_input = gr.Textbox(label="New Dataset Name")
|
| 150 |
+
create_button = gr.Button("Create Dataset")
|
| 151 |
+
with gr.Column():
|
| 152 |
+
gr.Markdown("### Upload Existing Dataset")
|
| 153 |
+
upload_input = gr.File(label="Upload Dataset Zip", type="filepath", file_types=['.zip'])
|
| 154 |
+
upload_button = gr.Button("Upload Dataset")
|
| 155 |
+
|
| 156 |
+
def create_dataset(name, datasets):
|
| 157 |
+
if not name:
|
| 158 |
+
return gr.update(), "Please enter a dataset name."
|
| 159 |
+
if name in datasets:
|
| 160 |
+
return gr.update(), f"Dataset '{name}' already exists."
|
| 161 |
+
datasets[name] = []
|
| 162 |
+
return gr.update(choices=list(datasets.keys()), value=name), f"Dataset '{name}' created."
|
| 163 |
+
|
| 164 |
+
create_button.click(
|
| 165 |
+
create_dataset,
|
| 166 |
+
inputs=[dataset_name_input, datasets],
|
| 167 |
+
outputs=[dataset_selector, message_box]
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
def upload_dataset(zip_file_path, datasets):
|
| 171 |
+
if not zip_file_path:
|
| 172 |
+
return gr.update(), "Please upload a zip file."
|
| 173 |
+
dataset_name, dataset = load_dataset_from_zip(zip_file_path)
|
| 174 |
+
if dataset_name is None:
|
| 175 |
+
return gr.update(), "Failed to load dataset from zip file."
|
| 176 |
+
if dataset_name in datasets:
|
| 177 |
+
return gr.update(), f"Dataset '{dataset_name}' already exists."
|
| 178 |
+
datasets[dataset_name] = dataset
|
| 179 |
+
return gr.update(choices=list(datasets.keys()), value=dataset_name), f"Dataset '{dataset_name}' uploaded."
|
| 180 |
+
|
| 181 |
+
upload_button.click(
|
| 182 |
+
upload_dataset,
|
| 183 |
+
inputs=[upload_input, datasets],
|
| 184 |
+
outputs=[dataset_selector, message_box]
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
with gr.TabItem("Add Entry"):
|
| 188 |
+
with gr.Row():
|
| 189 |
+
image_input = gr.Image(label="Upload Image", type="numpy")
|
| 190 |
+
prompt_input = gr.Textbox(label="Prompt")
|
| 191 |
+
add_button = gr.Button("Add Entry")
|
| 192 |
+
|
| 193 |
+
def add_entry(image_data, prompt, current_dataset_name, datasets):
|
| 194 |
+
if not current_dataset_name:
|
| 195 |
+
return datasets, gr.update(), gr.update(), gr.update(), "No dataset selected."
|
| 196 |
+
if image_data is None or not prompt:
|
| 197 |
+
return datasets, gr.update(), gr.update(), gr.update(), "Please provide both an image and a prompt."
|
| 198 |
+
# Convert image_data to base64
|
| 199 |
+
image = Image.fromarray(image_data.astype('uint8'))
|
| 200 |
+
buffered = BytesIO()
|
| 201 |
+
image.save(buffered, format="PNG")
|
| 202 |
+
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 203 |
+
img_data = f"data:image/png;base64,{img_str}"
|
| 204 |
+
datasets[current_dataset_name].append({'image': img_data, 'prompt': prompt})
|
| 205 |
+
dataset = datasets[current_dataset_name]
|
| 206 |
+
# Reset page number to 0 when a new entry is added
|
| 207 |
+
page_number = 0
|
| 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), gr.update(choices=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(), entry_selector, message_box]
|
| 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 |
+
entry_selector = gr.Dropdown(label="Select Entry to Edit/Delete")
|
| 224 |
+
new_prompt_input = gr.Textbox(label="New Prompt (for Edit)")
|
| 225 |
+
with gr.Row():
|
| 226 |
+
edit_button = gr.Button("Edit Entry")
|
| 227 |
+
delete_button = gr.Button("Delete Entry")
|
| 228 |
+
|
| 229 |
+
def update_selected_entry(entry_option, current_dataset_name, datasets):
|
| 230 |
+
if not current_dataset_name or not entry_option:
|
| 231 |
+
return gr.update(), gr.update()
|
| 232 |
+
index = int(entry_option.split(":")[0])
|
| 233 |
+
entry = datasets[current_dataset_name][index]
|
| 234 |
+
image_data = entry['image']
|
| 235 |
+
prompt = entry['prompt']
|
| 236 |
+
# Decode base64 image data to numpy array
|
| 237 |
+
image_bytes = base64.b64decode(image_data.split(",")[1])
|
| 238 |
+
image = Image.open(BytesIO(image_bytes))
|
| 239 |
+
image_array = np.array(image)
|
| 240 |
+
return gr.update(value=image_array), gr.update(value=prompt)
|
| 241 |
+
|
| 242 |
+
entry_selector.change(
|
| 243 |
+
update_selected_entry,
|
| 244 |
+
inputs=[entry_selector, current_dataset_name, datasets],
|
| 245 |
+
outputs=[selected_image, selected_prompt]
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
def edit_entry(entry_option, new_prompt, current_dataset_name, datasets, current_page_number):
|
| 249 |
+
if not current_dataset_name:
|
| 250 |
+
return datasets, gr.update(), gr.update(), gr.update(), gr.update(), "No dataset selected."
|
| 251 |
+
if not entry_option or not new_prompt.strip():
|
| 252 |
+
return datasets, gr.update(), gr.update(), gr.update(), gr.update(), "Please select an entry and provide a new prompt."
|
| 253 |
+
index = int(entry_option.split(":")[0])
|
| 254 |
+
datasets[current_dataset_name][index]['prompt'] = new_prompt
|
| 255 |
+
dataset = datasets[current_dataset_name]
|
| 256 |
+
html_content = display_dataset_html(dataset, page_number=current_page_number)
|
| 257 |
+
# Update entry_selector options
|
| 258 |
+
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
| 259 |
+
return datasets, gr.update(value=html_content), gr.update(choices=entry_options), gr.update(value=""), gr.update(), f"Entry {index} updated."
|
| 260 |
+
|
| 261 |
+
edit_button.click(
|
| 262 |
+
edit_entry,
|
| 263 |
+
inputs=[entry_selector, new_prompt_input, current_dataset_name, datasets, current_page_number],
|
| 264 |
+
outputs=[datasets, gr.HTML(), entry_selector, new_prompt_input, message_box]
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
def delete_entry(entry_option, current_dataset_name, datasets, current_page_number):
|
| 268 |
+
if not current_dataset_name:
|
| 269 |
+
return datasets, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), "No dataset selected."
|
| 270 |
+
if not entry_option:
|
| 271 |
+
return datasets, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), "Please select an entry to delete."
|
| 272 |
+
index = int(entry_option.split(":")[0])
|
| 273 |
+
del datasets[current_dataset_name][index]
|
| 274 |
+
dataset = datasets[current_dataset_name]
|
| 275 |
+
html_content = display_dataset_html(dataset, page_number=current_page_number)
|
| 276 |
+
# Update entry_selector options
|
| 277 |
+
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
| 278 |
+
return datasets, gr.update(value=html_content), gr.update(choices=entry_options), gr.update(value=None), gr.update(value=""), message_box, f"Entry {index} deleted."
|
| 279 |
+
|
| 280 |
+
delete_button.click(
|
| 281 |
+
delete_entry,
|
| 282 |
+
inputs=[entry_selector, current_dataset_name, datasets, current_page_number],
|
| 283 |
+
outputs=[datasets, gr.HTML(), entry_selector, selected_image, selected_prompt, message_box]
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
with gr.TabItem("Download Dataset"):
|
| 287 |
+
download_button = gr.Button("Download Dataset")
|
| 288 |
+
download_output = gr.File(label="Download Zip", interactive=False)
|
| 289 |
+
|
| 290 |
+
def download_dataset(current_dataset_name, datasets):
|
| 291 |
+
if not current_dataset_name:
|
| 292 |
+
return None, "No dataset selected."
|
| 293 |
+
if not datasets[current_dataset_name]:
|
| 294 |
+
return None, "Dataset is empty."
|
| 295 |
+
zip_buffer = save_dataset_to_zip(current_dataset_name, datasets[current_dataset_name])
|
| 296 |
+
# Write zip_buffer to a temporary file
|
| 297 |
+
temp_dir = tempfile.mkdtemp()
|
| 298 |
+
zip_path = os.path.join(temp_dir, f"{current_dataset_name}.zip")
|
| 299 |
+
with open(zip_path, 'wb') as f:
|
| 300 |
+
f.write(zip_buffer.getvalue())
|
| 301 |
+
return zip_path, f"Dataset '{current_dataset_name}' is ready for download."
|
| 302 |
+
|
| 303 |
+
download_button.click(
|
| 304 |
+
download_dataset,
|
| 305 |
+
inputs=[current_dataset_name, datasets],
|
| 306 |
+
outputs=[download_output, message_box]
|
| 307 |
+
)
|
| 308 |
|
| 309 |
+
# Dataset Viewer and Pagination Controls at the Bottom
|
| 310 |
+
with gr.Column():
|
| 311 |
+
gr.Markdown("### Dataset Viewer")
|
| 312 |
+
dataset_html = gr.HTML()
|
| 313 |
+
with gr.Row():
|
| 314 |
+
prev_button = gr.Button("Previous Page")
|
| 315 |
+
next_button = gr.Button("Next Page")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
|
| 317 |
def select_dataset(dataset_name, datasets):
|
| 318 |
if dataset_name in datasets:
|
|
|
|
| 327 |
dataset_selector.change(
|
| 328 |
select_dataset,
|
| 329 |
inputs=[dataset_selector, datasets],
|
| 330 |
+
outputs=[current_dataset_name, current_page_number, dataset_html, entry_selector, message_box]
|
| 331 |
)
|
| 332 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
def change_page(action, current_page_number, datasets, current_dataset_name):
|
| 334 |
if not current_dataset_name:
|
| 335 |
return current_page_number, gr.update(), "No dataset selected."
|