throaway2854
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import base64
|
|
7 |
from PIL import Image
|
8 |
import uuid
|
9 |
import tempfile
|
|
|
10 |
|
11 |
def save_dataset_to_zip(dataset_name, dataset):
|
12 |
# Create a temporary directory
|
@@ -22,6 +23,7 @@ def save_dataset_to_zip(dataset_name, dataset):
|
|
22 |
# Save image to images directory
|
23 |
image_filename = f"{uuid.uuid4().hex}.png"
|
24 |
image_path = os.path.join(images_dir, image_filename)
|
|
|
25 |
image = Image.open(BytesIO(base64.b64decode(image_data.split(",")[1])))
|
26 |
image.save(image_path)
|
27 |
# Add annotation
|
@@ -49,7 +51,6 @@ def load_dataset_from_zip(zip_file):
|
|
49 |
temp_dir = tempfile.mkdtemp()
|
50 |
with zipfile.ZipFile(zip_file.name, 'r') as zip_ref:
|
51 |
zip_ref.extractall(temp_dir)
|
52 |
-
# Assuming the dataset folder is the first folder in the zip
|
53 |
dataset_name = os.listdir(temp_dir)[0]
|
54 |
dataset_path = os.path.join(temp_dir, dataset_name)
|
55 |
dataset = []
|
@@ -143,14 +144,16 @@ with gr.Blocks() as demo:
|
|
143 |
if dataset_name in datasets:
|
144 |
dataset = datasets[dataset_name]
|
145 |
html_content = display_dataset_html(dataset)
|
146 |
-
|
|
|
|
|
147 |
else:
|
148 |
-
return "", gr.update(value="<div>Select a dataset.</div>"), ""
|
149 |
|
150 |
dataset_selector.change(
|
151 |
select_dataset,
|
152 |
inputs=[dataset_selector, datasets],
|
153 |
-
outputs=[current_dataset_name, dataset_html, message_box]
|
154 |
)
|
155 |
|
156 |
with gr.Tab("Add Entry"):
|
@@ -161,62 +164,89 @@ with gr.Blocks() as demo:
|
|
161 |
|
162 |
def add_entry(image_data, prompt, current_dataset_name, datasets):
|
163 |
if not current_dataset_name:
|
164 |
-
return datasets, gr.update(), "No dataset selected."
|
165 |
if image_data is None or not prompt:
|
166 |
-
return datasets, gr.update(), "Please provide both an image and a prompt."
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
add_button.click(
|
172 |
add_entry,
|
173 |
inputs=[image_input, prompt_input, current_dataset_name, datasets],
|
174 |
-
outputs=[datasets, dataset_html, message_box]
|
175 |
)
|
176 |
|
177 |
with gr.Tab("Edit / Delete Entry"):
|
178 |
-
|
|
|
|
|
|
|
179 |
new_prompt_input = gr.Textbox(label="New Prompt (for Edit)")
|
180 |
with gr.Row():
|
181 |
edit_button = gr.Button("Edit Entry")
|
182 |
delete_button = gr.Button("Delete Entry")
|
183 |
|
184 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
if not current_dataset_name:
|
186 |
-
return datasets, gr.update(), "No dataset selected."
|
187 |
-
if
|
188 |
-
return datasets, gr.update(), "Please
|
189 |
-
index = int(
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
|
197 |
edit_button.click(
|
198 |
edit_entry,
|
199 |
-
inputs=[
|
200 |
-
outputs=[datasets, dataset_html, message_box]
|
201 |
)
|
202 |
|
203 |
-
def delete_entry(
|
204 |
if not current_dataset_name:
|
205 |
-
return datasets, gr.update(), "No dataset selected."
|
206 |
-
if
|
207 |
-
return datasets, gr.update(), "Please
|
208 |
-
index = int(
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
|
216 |
delete_button.click(
|
217 |
delete_entry,
|
218 |
-
inputs=[
|
219 |
-
outputs=[datasets, dataset_html, message_box]
|
220 |
)
|
221 |
|
222 |
with gr.Tab("Download Dataset"):
|
@@ -225,6 +255,8 @@ with gr.Blocks() as demo:
|
|
225 |
def download_dataset(current_dataset_name, datasets):
|
226 |
if not current_dataset_name:
|
227 |
return None, "No dataset selected."
|
|
|
|
|
228 |
zip_buffer = save_dataset_to_zip(current_dataset_name, datasets[current_dataset_name])
|
229 |
return zip_buffer.getvalue(), f"Dataset '{current_dataset_name}' is ready for download."
|
230 |
download_button.click(
|
@@ -233,14 +265,14 @@ with gr.Blocks() as demo:
|
|
233 |
outputs=[download_output, message_box]
|
234 |
)
|
235 |
|
236 |
-
#
|
237 |
-
def
|
238 |
-
return gr.update(choices=list(datasets.keys()))
|
239 |
-
|
240 |
demo.load(
|
241 |
-
|
242 |
inputs=[datasets],
|
243 |
-
outputs=[dataset_selector]
|
244 |
)
|
245 |
|
246 |
demo.launch()
|
|
|
7 |
from PIL import Image
|
8 |
import uuid
|
9 |
import tempfile
|
10 |
+
import numpy as np
|
11 |
|
12 |
def save_dataset_to_zip(dataset_name, dataset):
|
13 |
# Create a temporary directory
|
|
|
23 |
# Save image to images directory
|
24 |
image_filename = f"{uuid.uuid4().hex}.png"
|
25 |
image_path = os.path.join(images_dir, image_filename)
|
26 |
+
# Decode the base64 image data
|
27 |
image = Image.open(BytesIO(base64.b64decode(image_data.split(",")[1])))
|
28 |
image.save(image_path)
|
29 |
# Add annotation
|
|
|
51 |
temp_dir = tempfile.mkdtemp()
|
52 |
with zipfile.ZipFile(zip_file.name, 'r') as zip_ref:
|
53 |
zip_ref.extractall(temp_dir)
|
|
|
54 |
dataset_name = os.listdir(temp_dir)[0]
|
55 |
dataset_path = os.path.join(temp_dir, dataset_name)
|
56 |
dataset = []
|
|
|
144 |
if dataset_name in datasets:
|
145 |
dataset = datasets[dataset_name]
|
146 |
html_content = display_dataset_html(dataset)
|
147 |
+
# Update entry_selector options
|
148 |
+
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
149 |
+
return dataset_name, gr.update(value=html_content), gr.update(choices=entry_options), ""
|
150 |
else:
|
151 |
+
return "", gr.update(value="<div>Select a dataset.</div>"), gr.update(choices=[]), ""
|
152 |
|
153 |
dataset_selector.change(
|
154 |
select_dataset,
|
155 |
inputs=[dataset_selector, datasets],
|
156 |
+
outputs=[current_dataset_name, dataset_html, gr.Variable(), message_box]
|
157 |
)
|
158 |
|
159 |
with gr.Tab("Add Entry"):
|
|
|
164 |
|
165 |
def add_entry(image_data, prompt, current_dataset_name, datasets):
|
166 |
if not current_dataset_name:
|
167 |
+
return datasets, gr.update(), gr.update(), "No dataset selected."
|
168 |
if image_data is None or not prompt:
|
169 |
+
return datasets, gr.update(), gr.update(), "Please provide both an image and a prompt."
|
170 |
+
# Convert image_data to base64
|
171 |
+
image = Image.fromarray(image_data.astype('uint8'))
|
172 |
+
buffered = BytesIO()
|
173 |
+
image.save(buffered, format="PNG")
|
174 |
+
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
175 |
+
img_data = f"data:image/png;base64,{img_str}"
|
176 |
+
datasets[current_dataset_name].append({'image': img_data, 'prompt': prompt})
|
177 |
+
dataset = datasets[current_dataset_name]
|
178 |
+
html_content = display_dataset_html(dataset)
|
179 |
+
# Update entry_selector options
|
180 |
+
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
181 |
+
return datasets, gr.update(value=html_content), gr.update(choices=entry_options), f"Entry added to dataset '{current_dataset_name}'."
|
182 |
|
183 |
add_button.click(
|
184 |
add_entry,
|
185 |
inputs=[image_input, prompt_input, current_dataset_name, datasets],
|
186 |
+
outputs=[datasets, dataset_html, gr.Variable(), message_box]
|
187 |
)
|
188 |
|
189 |
with gr.Tab("Edit / Delete Entry"):
|
190 |
+
# Entry selector
|
191 |
+
entry_selector = gr.Dropdown(label="Select Entry to Edit/Delete")
|
192 |
+
selected_image = gr.Image(label="Selected Image", interactive=False)
|
193 |
+
selected_prompt = gr.Textbox(label="Current Prompt", interactive=False)
|
194 |
new_prompt_input = gr.Textbox(label="New Prompt (for Edit)")
|
195 |
with gr.Row():
|
196 |
edit_button = gr.Button("Edit Entry")
|
197 |
delete_button = gr.Button("Delete Entry")
|
198 |
|
199 |
+
def update_selected_entry(entry_option, current_dataset_name, datasets):
|
200 |
+
if not current_dataset_name or not entry_option:
|
201 |
+
return gr.update(), gr.update()
|
202 |
+
index = int(entry_option.split(":")[0])
|
203 |
+
entry = datasets[current_dataset_name][index]
|
204 |
+
image_data = entry['image']
|
205 |
+
prompt = entry['prompt']
|
206 |
+
return gr.update(value=image_data), gr.update(value=prompt)
|
207 |
+
|
208 |
+
entry_selector.change(
|
209 |
+
update_selected_entry,
|
210 |
+
inputs=[entry_selector, current_dataset_name, datasets],
|
211 |
+
outputs=[selected_image, selected_prompt]
|
212 |
+
)
|
213 |
+
|
214 |
+
def edit_entry(entry_option, new_prompt, current_dataset_name, datasets):
|
215 |
if not current_dataset_name:
|
216 |
+
return datasets, gr.update(), gr.update(), "No dataset selected."
|
217 |
+
if not entry_option or not new_prompt.strip():
|
218 |
+
return datasets, gr.update(), gr.update(), "Please select an entry and provide a new prompt."
|
219 |
+
index = int(entry_option.split(":")[0])
|
220 |
+
datasets[current_dataset_name][index]['prompt'] = new_prompt
|
221 |
+
dataset = datasets[current_dataset_name]
|
222 |
+
html_content = display_dataset_html(dataset)
|
223 |
+
# Update entry_selector options
|
224 |
+
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
225 |
+
return datasets, gr.update(value=html_content), gr.update(choices=entry_options), f"Entry {index} updated."
|
226 |
|
227 |
edit_button.click(
|
228 |
edit_entry,
|
229 |
+
inputs=[entry_selector, new_prompt_input, current_dataset_name, datasets],
|
230 |
+
outputs=[datasets, dataset_html, entry_selector, message_box]
|
231 |
)
|
232 |
|
233 |
+
def delete_entry(entry_option, current_dataset_name, datasets):
|
234 |
if not current_dataset_name:
|
235 |
+
return datasets, gr.update(), gr.update(), "No dataset selected."
|
236 |
+
if not entry_option:
|
237 |
+
return datasets, gr.update(), gr.update(), "Please select an entry to delete."
|
238 |
+
index = int(entry_option.split(":")[0])
|
239 |
+
del datasets[current_dataset_name][index]
|
240 |
+
dataset = datasets[current_dataset_name]
|
241 |
+
html_content = display_dataset_html(dataset)
|
242 |
+
# Update entry_selector options
|
243 |
+
entry_options = [f"{idx}: {entry['prompt'][:30]}" for idx, entry in enumerate(dataset)]
|
244 |
+
return datasets, gr.update(value=html_content), gr.update(choices=entry_options), f"Entry {index} deleted."
|
245 |
|
246 |
delete_button.click(
|
247 |
delete_entry,
|
248 |
+
inputs=[entry_selector, current_dataset_name, datasets],
|
249 |
+
outputs=[datasets, dataset_html, entry_selector, message_box]
|
250 |
)
|
251 |
|
252 |
with gr.Tab("Download Dataset"):
|
|
|
255 |
def download_dataset(current_dataset_name, datasets):
|
256 |
if not current_dataset_name:
|
257 |
return None, "No dataset selected."
|
258 |
+
if not datasets[current_dataset_name]:
|
259 |
+
return None, "Dataset is empty."
|
260 |
zip_buffer = save_dataset_to_zip(current_dataset_name, datasets[current_dataset_name])
|
261 |
return zip_buffer.getvalue(), f"Dataset '{current_dataset_name}' is ready for download."
|
262 |
download_button.click(
|
|
|
265 |
outputs=[download_output, message_box]
|
266 |
)
|
267 |
|
268 |
+
# Initialize dataset_selector and entry_selector
|
269 |
+
def initialize_components(datasets):
|
270 |
+
return gr.update(choices=list(datasets.keys())), gr.update(choices=[])
|
271 |
+
|
272 |
demo.load(
|
273 |
+
initialize_components,
|
274 |
inputs=[datasets],
|
275 |
+
outputs=[dataset_selector, entry_selector]
|
276 |
)
|
277 |
|
278 |
demo.launch()
|