Update app.py
Browse files
app.py
CHANGED
@@ -10,117 +10,33 @@ import tempfile
|
|
10 |
import numpy as np
|
11 |
|
12 |
def save_dataset_to_zip(dataset_name, dataset):
|
13 |
-
#
|
14 |
-
|
15 |
-
dataset_path = os.path.join(temp_dir, dataset_name)
|
16 |
-
os.makedirs(dataset_path, exist_ok=True)
|
17 |
-
images_dir = os.path.join(dataset_path, 'images')
|
18 |
-
os.makedirs(images_dir, exist_ok=True)
|
19 |
-
annotations = []
|
20 |
-
for idx, entry in enumerate(dataset):
|
21 |
-
image_data = entry['image']
|
22 |
-
prompt = entry['prompt']
|
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
|
30 |
-
annotations.append({
|
31 |
-
'file_name': os.path.join('images', image_filename),
|
32 |
-
'text': prompt
|
33 |
-
})
|
34 |
-
# Save annotations to JSONL file
|
35 |
-
annotations_path = os.path.join(dataset_path, 'annotations.jsonl')
|
36 |
-
with open(annotations_path, 'w') as f:
|
37 |
-
for ann in annotations:
|
38 |
-
f.write(json.dumps(ann) + '\n')
|
39 |
-
# Create a zip file
|
40 |
-
zip_buffer = BytesIO()
|
41 |
-
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
42 |
-
for root, dirs, files in os.walk(dataset_path):
|
43 |
-
for file in files:
|
44 |
-
abs_file = os.path.join(root, file)
|
45 |
-
rel_file = os.path.relpath(abs_file, dataset_path)
|
46 |
-
zipf.write(abs_file, rel_file)
|
47 |
-
zip_buffer.seek(0)
|
48 |
-
return zip_buffer
|
49 |
|
50 |
def load_dataset_from_zip(zip_file):
|
51 |
-
|
52 |
-
|
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 = []
|
57 |
-
images_dir = os.path.join(dataset_path, 'images')
|
58 |
-
annotations_path = os.path.join(dataset_path, 'annotations.jsonl')
|
59 |
-
if os.path.exists(annotations_path):
|
60 |
-
with open(annotations_path, 'r') as f:
|
61 |
-
for line in f:
|
62 |
-
ann = json.loads(line)
|
63 |
-
file_name = ann['file_name']
|
64 |
-
prompt = ann['text']
|
65 |
-
image_path = os.path.join(dataset_path, file_name)
|
66 |
-
# Read image and convert to base64
|
67 |
-
with open(image_path, 'rb') as img_f:
|
68 |
-
image_bytes = img_f.read()
|
69 |
-
encoded = base64.b64encode(image_bytes).decode()
|
70 |
-
mime_type = "image/png"
|
71 |
-
image_data = f"data:{mime_type};base64,{encoded}"
|
72 |
-
dataset.append({
|
73 |
-
'image': image_data,
|
74 |
-
'prompt': prompt
|
75 |
-
})
|
76 |
-
return dataset_name, dataset
|
77 |
|
78 |
def display_dataset_html(dataset):
|
79 |
-
|
80 |
-
|
81 |
-
for idx, entry in enumerate(dataset):
|
82 |
-
image_data = entry['image']
|
83 |
-
prompt = entry['prompt']
|
84 |
-
html_content += f"""
|
85 |
-
<div style="display: flex; align-items: center; margin-bottom: 10px;">
|
86 |
-
<div style="width: 50px;">{idx}</div>
|
87 |
-
<img src="{image_data}" alt="Image {idx}" style="max-height: 100px; margin-right: 10px;"/>
|
88 |
-
<div>{prompt}</div>
|
89 |
-
</div>
|
90 |
-
"""
|
91 |
-
return html_content
|
92 |
-
else:
|
93 |
-
return "<div>No entries in dataset.</div>"
|
94 |
|
95 |
with gr.Blocks() as demo:
|
96 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 20px;'>Dataset Builder</h1>")
|
97 |
datasets = gr.State({})
|
98 |
current_dataset_name = gr.State("")
|
99 |
dataset_selector = gr.Dropdown(label="Select Dataset", interactive=True)
|
|
|
100 |
dataset_html = gr.HTML()
|
101 |
message_box = gr.Textbox(interactive=False, label="Message")
|
102 |
|
103 |
-
# Entry selector initialized here
|
104 |
-
entry_selector = gr.Dropdown(label="Select Entry to Edit/Delete")
|
105 |
-
|
106 |
with gr.Tab("Create / Upload Dataset"):
|
107 |
-
|
108 |
-
|
109 |
-
gr.Markdown("### Create a New Dataset")
|
110 |
-
dataset_name_input = gr.Textbox(label="New Dataset Name")
|
111 |
-
create_button = gr.Button("Create Dataset")
|
112 |
-
with gr.Column():
|
113 |
-
gr.Markdown("### Upload Existing Dataset")
|
114 |
-
upload_input = gr.File(label="Upload Dataset Zip", file_types=['.zip'])
|
115 |
-
upload_button = gr.Button("Upload Dataset")
|
116 |
|
117 |
def create_dataset(name, datasets):
|
118 |
-
|
119 |
-
|
120 |
-
if name in datasets:
|
121 |
-
return gr.update(), f"Dataset '{name}' already exists."
|
122 |
-
datasets[name] = []
|
123 |
-
return gr.update(choices=list(datasets.keys()), value=name), f"Dataset '{name}' created."
|
124 |
|
125 |
create_button.click(
|
126 |
create_dataset,
|
@@ -129,13 +45,8 @@ with gr.Blocks() as demo:
|
|
129 |
)
|
130 |
|
131 |
def upload_dataset(zip_file, datasets):
|
132 |
-
|
133 |
-
|
134 |
-
dataset_name, dataset = load_dataset_from_zip(zip_file)
|
135 |
-
if dataset_name in datasets:
|
136 |
-
return gr.update(), f"Dataset '{dataset_name}' already exists."
|
137 |
-
datasets[dataset_name] = dataset
|
138 |
-
return gr.update(choices=list(datasets.keys()), value=dataset_name), f"Dataset '{dataset_name}' uploaded."
|
139 |
|
140 |
upload_button.click(
|
141 |
upload_dataset,
|
@@ -160,10 +71,8 @@ with gr.Blocks() as demo:
|
|
160 |
)
|
161 |
|
162 |
with gr.Tab("Add Entry"):
|
163 |
-
|
164 |
-
|
165 |
-
prompt_input = gr.Textbox(label="Prompt")
|
166 |
-
add_button = gr.Button("Add Entry")
|
167 |
|
168 |
def add_entry(image_data, prompt, current_dataset_name, datasets):
|
169 |
if not current_dataset_name:
|
@@ -190,10 +99,8 @@ with gr.Blocks() as demo:
|
|
190 |
)
|
191 |
|
192 |
with gr.Tab("Edit / Delete Entry"):
|
193 |
-
# Components moved inside the tab
|
194 |
with gr.Column():
|
195 |
-
# Entry selector
|
196 |
-
entry_selector = gr.Dropdown(label="Select Entry to Edit/Delete")
|
197 |
selected_image = gr.Image(label="Selected Image", interactive=False)
|
198 |
selected_prompt = gr.Textbox(label="Current Prompt", interactive=False)
|
199 |
new_prompt_input = gr.Textbox(label="New Prompt (for Edit)")
|
@@ -267,7 +174,8 @@ with gr.Blocks() as demo:
|
|
267 |
if not datasets[current_dataset_name]:
|
268 |
return None, "Dataset is empty."
|
269 |
zip_buffer = save_dataset_to_zip(current_dataset_name, datasets[current_dataset_name])
|
270 |
-
|
|
|
271 |
download_button.click(
|
272 |
download_dataset,
|
273 |
inputs=[current_dataset_name, datasets],
|
|
|
10 |
import numpy as np
|
11 |
|
12 |
def save_dataset_to_zip(dataset_name, dataset):
|
13 |
+
# Function implementation remains the same
|
14 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def load_dataset_from_zip(zip_file):
|
17 |
+
# Function implementation remains the same
|
18 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def display_dataset_html(dataset):
|
21 |
+
# Function implementation remains the same
|
22 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
with gr.Blocks() as demo:
|
25 |
gr.Markdown("<h1 style='text-align: center; margin-bottom: 20px;'>Dataset Builder</h1>")
|
26 |
datasets = gr.State({})
|
27 |
current_dataset_name = gr.State("")
|
28 |
dataset_selector = gr.Dropdown(label="Select Dataset", interactive=True)
|
29 |
+
entry_selector = gr.Dropdown(label="Select Entry to Edit/Delete") # Moved outside
|
30 |
dataset_html = gr.HTML()
|
31 |
message_box = gr.Textbox(interactive=False, label="Message")
|
32 |
|
|
|
|
|
|
|
33 |
with gr.Tab("Create / Upload Dataset"):
|
34 |
+
# Create / Upload Dataset components and functions
|
35 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
def create_dataset(name, datasets):
|
38 |
+
# Function implementation remains the same
|
39 |
+
# ...
|
|
|
|
|
|
|
|
|
40 |
|
41 |
create_button.click(
|
42 |
create_dataset,
|
|
|
45 |
)
|
46 |
|
47 |
def upload_dataset(zip_file, datasets):
|
48 |
+
# Function implementation remains the same
|
49 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
upload_button.click(
|
52 |
upload_dataset,
|
|
|
71 |
)
|
72 |
|
73 |
with gr.Tab("Add Entry"):
|
74 |
+
# Add Entry components and functions
|
75 |
+
# ...
|
|
|
|
|
76 |
|
77 |
def add_entry(image_data, prompt, current_dataset_name, datasets):
|
78 |
if not current_dataset_name:
|
|
|
99 |
)
|
100 |
|
101 |
with gr.Tab("Edit / Delete Entry"):
|
|
|
102 |
with gr.Column():
|
103 |
+
# Entry selector is referenced here, defined at the top level
|
|
|
104 |
selected_image = gr.Image(label="Selected Image", interactive=False)
|
105 |
selected_prompt = gr.Textbox(label="Current Prompt", interactive=False)
|
106 |
new_prompt_input = gr.Textbox(label="New Prompt (for Edit)")
|
|
|
174 |
if not datasets[current_dataset_name]:
|
175 |
return None, "Dataset is empty."
|
176 |
zip_buffer = save_dataset_to_zip(current_dataset_name, datasets[current_dataset_name])
|
177 |
+
# Return a dictionary with 'name' and 'data'
|
178 |
+
return {'name': f"{current_dataset_name}.zip", 'data': zip_buffer.getvalue()}, f"Dataset '{current_dataset_name}' is ready for download."
|
179 |
download_button.click(
|
180 |
download_dataset,
|
181 |
inputs=[current_dataset_name, datasets],
|