Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
import zipfile
|
@@ -10,6 +9,8 @@ import uuid
|
|
10 |
import tempfile
|
11 |
import numpy as np
|
12 |
|
|
|
|
|
13 |
def save_dataset_to_zip(dataset_name, dataset):
|
14 |
temp_dir = tempfile.mkdtemp()
|
15 |
dataset_path = os.path.join(temp_dir, dataset_name)
|
@@ -53,6 +54,8 @@ def save_dataset_to_zip(dataset_name, dataset):
|
|
53 |
zip_buffer.seek(0)
|
54 |
return zip_buffer
|
55 |
|
|
|
|
|
56 |
def load_dataset_from_zip(zip_file_path):
|
57 |
temp_dir = tempfile.mkdtemp()
|
58 |
try:
|
@@ -107,23 +110,24 @@ def load_dataset_from_zip(zip_file_path):
|
|
107 |
print(f"Error loading dataset: {e}")
|
108 |
return None, []
|
109 |
|
|
|
|
|
110 |
def display_dataset_html(dataset, page_number=0, items_per_page=2):
|
111 |
if dataset:
|
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 |
-
'''
|
118 |
for idx_offset, entry in enumerate(dataset_slice):
|
119 |
idx = start_idx + idx_offset
|
120 |
image_data = entry['image']
|
121 |
prompt = entry['prompt']
|
122 |
html_content += f"""
|
123 |
-
<div style="display: flex; flex-direction: column; align-items: center; margin-right: 20px;">
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
</div>
|
128 |
"""
|
129 |
html_content += '</div>'
|
@@ -131,14 +135,14 @@ def display_dataset_html(dataset, page_number=0, items_per_page=2):
|
|
131 |
else:
|
132 |
return "<div>No entries in dataset.</div>"
|
133 |
|
134 |
-
#Interface
|
135 |
with gr.Blocks() as demo:
|
136 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1px;'>Dataset Creator</h1>")
|
137 |
gr.Markdown("You must create/upload a dataset before selecting one")
|
138 |
datasets = gr.State({})
|
139 |
current_dataset_name = gr.State("")
|
140 |
current_page_number = gr.State(0)
|
141 |
-
dataset_html = gr.HTML()
|
142 |
|
143 |
# Top-level components
|
144 |
with gr.Column():
|
@@ -351,10 +355,11 @@ with gr.Blocks() as demo:
|
|
351 |
# Dataset Viewer and Pagination Controls at the Bottom
|
352 |
with gr.Column():
|
353 |
gr.Markdown("### Dataset Viewer")
|
|
|
354 |
with gr.Row():
|
355 |
prev_button = gr.Button("Previous Page")
|
356 |
next_button = gr.Button("Next Page")
|
357 |
-
|
358 |
def change_page(action, current_page_number, datasets, current_dataset_name):
|
359 |
if not current_dataset_name:
|
360 |
return current_page_number, gr.update(), "No dataset selected."
|
@@ -368,17 +373,17 @@ with gr.Blocks() as demo:
|
|
368 |
current_page_number -= 1
|
369 |
html_content = display_dataset_html(dataset, page_number=current_page_number)
|
370 |
return current_page_number, gr.update(value=html_content), ""
|
371 |
-
|
372 |
prev_button.click(
|
373 |
fn=lambda current_page_number, datasets, current_dataset_name: change_page("prev", current_page_number, datasets, current_dataset_name),
|
374 |
inputs=[current_page_number, datasets, current_dataset_name],
|
375 |
-
outputs=[current_page_number,
|
376 |
)
|
377 |
|
378 |
next_button.click(
|
379 |
fn=lambda current_page_number, datasets, current_dataset_name: change_page("next", current_page_number, datasets, current_dataset_name),
|
380 |
inputs=[current_page_number, datasets, current_dataset_name],
|
381 |
-
outputs=[current_page_number,
|
382 |
)
|
383 |
|
384 |
# Initialize dataset_selector
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import zipfile
|
|
|
9 |
import tempfile
|
10 |
import numpy as np
|
11 |
|
12 |
+
# Function to save dataset to zip
|
13 |
+
|
14 |
def save_dataset_to_zip(dataset_name, dataset):
|
15 |
temp_dir = tempfile.mkdtemp()
|
16 |
dataset_path = os.path.join(temp_dir, dataset_name)
|
|
|
54 |
zip_buffer.seek(0)
|
55 |
return zip_buffer
|
56 |
|
57 |
+
# Function to load dataset from zip
|
58 |
+
|
59 |
def load_dataset_from_zip(zip_file_path):
|
60 |
temp_dir = tempfile.mkdtemp()
|
61 |
try:
|
|
|
110 |
print(f"Error loading dataset: {e}")
|
111 |
return None, []
|
112 |
|
113 |
+
# Function to display dataset as HTML
|
114 |
+
|
115 |
def display_dataset_html(dataset, page_number=0, items_per_page=2):
|
116 |
if dataset:
|
117 |
start_idx = page_number * items_per_page
|
118 |
end_idx = start_idx + items_per_page
|
119 |
dataset_slice = dataset[start_idx:end_idx]
|
120 |
html_content = '''
|
121 |
+
<div style="display: flex; overflow-x: auto; padding: 10px; border: 1px solid #ccc;">'''
|
|
|
122 |
for idx_offset, entry in enumerate(dataset_slice):
|
123 |
idx = start_idx + idx_offset
|
124 |
image_data = entry['image']
|
125 |
prompt = entry['prompt']
|
126 |
html_content += f"""
|
127 |
+
<div style="display: flex; flex-direction: column; align-items: center; margin-right: 20px;">'''
|
128 |
+
<div style="margin-bottom: 5px;">{idx}</div>
|
129 |
+
<img src="{image_data}" alt="Image {idx}" style="max-height: 150px;"/>
|
130 |
+
<div style="max-width: 150px; word-wrap: break-word; text-align: center;">{prompt}</div>
|
131 |
</div>
|
132 |
"""
|
133 |
html_content += '</div>'
|
|
|
135 |
else:
|
136 |
return "<div>No entries in dataset.</div>"
|
137 |
|
138 |
+
# Interface
|
139 |
with gr.Blocks() as demo:
|
140 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 1px;'>Dataset Creator</h1>")
|
141 |
gr.Markdown("You must create/upload a dataset before selecting one")
|
142 |
datasets = gr.State({})
|
143 |
current_dataset_name = gr.State("")
|
144 |
current_page_number = gr.State(0)
|
145 |
+
dataset_html = gr.HTML() # Define dataset_html here
|
146 |
|
147 |
# Top-level components
|
148 |
with gr.Column():
|
|
|
355 |
# Dataset Viewer and Pagination Controls at the Bottom
|
356 |
with gr.Column():
|
357 |
gr.Markdown("### Dataset Viewer")
|
358 |
+
dataset_viewer = gr.HTML() # Use dataset_viewer instead of dataset_html
|
359 |
with gr.Row():
|
360 |
prev_button = gr.Button("Previous Page")
|
361 |
next_button = gr.Button("Next Page")
|
362 |
+
|
363 |
def change_page(action, current_page_number, datasets, current_dataset_name):
|
364 |
if not current_dataset_name:
|
365 |
return current_page_number, gr.update(), "No dataset selected."
|
|
|
373 |
current_page_number -= 1
|
374 |
html_content = display_dataset_html(dataset, page_number=current_page_number)
|
375 |
return current_page_number, gr.update(value=html_content), ""
|
376 |
+
|
377 |
prev_button.click(
|
378 |
fn=lambda current_page_number, datasets, current_dataset_name: change_page("prev", current_page_number, datasets, current_dataset_name),
|
379 |
inputs=[current_page_number, datasets, current_dataset_name],
|
380 |
+
outputs=[current_page_number, dataset_viewer, message_box]
|
381 |
)
|
382 |
|
383 |
next_button.click(
|
384 |
fn=lambda current_page_number, datasets, current_dataset_name: change_page("next", current_page_number, datasets, current_dataset_name),
|
385 |
inputs=[current_page_number, datasets, current_dataset_name],
|
386 |
+
outputs=[current_page_number, dataset_viewer, message_box]
|
387 |
)
|
388 |
|
389 |
# Initialize dataset_selector
|