Spaces:
Paused
Paused
throaway2854
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ import io
|
|
10 |
import uuid
|
11 |
import time
|
12 |
import random
|
|
|
13 |
|
14 |
DATA_DIR = "/data"
|
15 |
IMAGES_DIR = os.path.join(DATA_DIR, "images")
|
@@ -92,6 +93,29 @@ class DatasetBuilder:
|
|
92 |
with open(dataset_file, 'w') as f:
|
93 |
json.dump(self.dataset, f)
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
def add_image(self, url, cookies=None):
|
96 |
try:
|
97 |
response = make_request(url, cookies)
|
@@ -201,7 +225,12 @@ def view_dataset(dataset_name):
|
|
201 |
|
202 |
def upload_huggingface_dataset(dataset_name, privacy):
|
203 |
builder = DatasetBuilder(dataset_name)
|
204 |
-
return builder.upload_to_huggingface(private=privacy)
|
|
|
|
|
|
|
|
|
|
|
205 |
|
206 |
# Create Gradio interface
|
207 |
with gr.Blocks(theme="huggingface") as iface:
|
@@ -236,5 +265,16 @@ with gr.Blocks(theme="huggingface") as iface:
|
|
236 |
hf_upload_result = gr.Textbox(label="Upload Result")
|
237 |
upload_hf_button.click(upload_huggingface_dataset, inputs=[dataset_name_input, privacy_radio], outputs=hf_upload_result)
|
238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
# Launch the interface
|
240 |
iface.launch()
|
|
|
10 |
import uuid
|
11 |
import time
|
12 |
import random
|
13 |
+
import zipfile
|
14 |
|
15 |
DATA_DIR = "/data"
|
16 |
IMAGES_DIR = os.path.join(DATA_DIR, "images")
|
|
|
93 |
with open(dataset_file, 'w') as f:
|
94 |
json.dump(self.dataset, f)
|
95 |
|
96 |
+
def create_downloadable_dataset(self):
|
97 |
+
if not self.dataset:
|
98 |
+
return None, "Dataset is empty. Add some images first."
|
99 |
+
|
100 |
+
try:
|
101 |
+
# Create a temporary ZIP file
|
102 |
+
zip_filename = f"{self.dataset_name}.zip"
|
103 |
+
zip_path = os.path.join(DATA_DIR, zip_filename)
|
104 |
+
|
105 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
106 |
+
# Add the dataset JSON file
|
107 |
+
dataset_file = self.get_dataset_file()
|
108 |
+
zipf.write(dataset_file, os.path.basename(dataset_file))
|
109 |
+
|
110 |
+
# Add all images
|
111 |
+
for item in self.dataset:
|
112 |
+
image_path = os.path.join(IMAGES_DIR, item['image'])
|
113 |
+
zipf.write(image_path, os.path.join("images", item['image']))
|
114 |
+
|
115 |
+
return zip_path, f"Dataset '{self.dataset_name}' ready for download."
|
116 |
+
except Exception as e:
|
117 |
+
return None, f"Error creating downloadable dataset: {str(e)}"
|
118 |
+
|
119 |
def add_image(self, url, cookies=None):
|
120 |
try:
|
121 |
response = make_request(url, cookies)
|
|
|
225 |
|
226 |
def upload_huggingface_dataset(dataset_name, privacy):
|
227 |
builder = DatasetBuilder(dataset_name)
|
228 |
+
return builder.upload_to_huggingface(private=privacy)
|
229 |
+
|
230 |
+
def download_dataset(dataset_name):
|
231 |
+
builder = DatasetBuilder(dataset_name)
|
232 |
+
zip_path, message = builder.create_downloadable_dataset()
|
233 |
+
return zip_path, message
|
234 |
|
235 |
# Create Gradio interface
|
236 |
with gr.Blocks(theme="huggingface") as iface:
|
|
|
265 |
hf_upload_result = gr.Textbox(label="Upload Result")
|
266 |
upload_hf_button.click(upload_huggingface_dataset, inputs=[dataset_name_input, privacy_radio], outputs=hf_upload_result)
|
267 |
|
268 |
+
gr.Markdown("## Download Dataset")
|
269 |
+
download_button = gr.Button("Download Dataset")
|
270 |
+
download_output = gr.File(label="Download")
|
271 |
+
download_message = gr.Textbox(label="Download Status")
|
272 |
+
|
273 |
+
download_button.click(
|
274 |
+
download_dataset,
|
275 |
+
inputs=[dataset_name_input],
|
276 |
+
outputs=[download_output, download_message]
|
277 |
+
)
|
278 |
+
|
279 |
# Launch the interface
|
280 |
iface.launch()
|