Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import os
|
|
2 |
import requests
|
3 |
import shutil
|
4 |
import gradio as gr
|
5 |
-
from concurrent.futures import ThreadPoolExecutor
|
6 |
from zipfile import ZipFile
|
7 |
import logging
|
8 |
|
@@ -24,6 +23,8 @@ def get_image_ids(batch_id: str) -> list[str]:
|
|
24 |
image_ids.append(image_id)
|
25 |
else:
|
26 |
logging.warning(f"Unexpected id format: {item['id']}")
|
|
|
|
|
27 |
logging.info(f"Found {len(image_ids)} images in batch {batch_id}")
|
28 |
return image_ids
|
29 |
|
@@ -42,6 +43,7 @@ def download_image(url: str, dest: str) -> None:
|
|
42 |
logging.info(f"Successfully downloaded image: {dest}")
|
43 |
else:
|
44 |
logging.error(f"Failed to download image from {url}. Status code: {response.status_code}")
|
|
|
45 |
del response
|
46 |
|
47 |
def download_image_by_image_id(image_id: str):
|
@@ -56,28 +58,22 @@ def download_image_by_image_id(image_id: str):
|
|
56 |
dest = os.path.join(batch_id, image_id + ".jpg")
|
57 |
download_image(url, dest)
|
58 |
|
59 |
-
def download_batch_images(batch_id: str,
|
60 |
logging.info(f"Starting download for batch {batch_id}")
|
61 |
-
image_ids = get_image_ids(batch_id)
|
62 |
-
if not image_ids:
|
63 |
-
raise ValueError("No images found to download.")
|
64 |
-
total_images = len(image_ids)
|
65 |
|
66 |
-
if progress:
|
67 |
progress(0, desc=f"Starting download for {batch_id}...")
|
68 |
|
69 |
-
|
|
|
|
|
|
|
70 |
download_image_by_image_id(image_id)
|
71 |
logging.info(f"Downloaded image {image_id}")
|
72 |
-
if progress:
|
73 |
-
# Update progress after each image
|
74 |
current_progress = (idx + 1) / total_images
|
75 |
progress(current_progress, desc=f"Downloading {image_id}...")
|
76 |
|
77 |
-
with ThreadPoolExecutor(max_workers=workers) as executor:
|
78 |
-
for idx, image_id in enumerate(image_ids):
|
79 |
-
executor.submit(track_download, image_id, idx)
|
80 |
-
|
81 |
logging.info(f"Zipping downloaded images for batch {batch_id}")
|
82 |
zip_filename = f"{batch_id}.zip"
|
83 |
with ZipFile(zip_filename, 'w') as zipf:
|
@@ -88,20 +84,19 @@ def download_batch_images(batch_id: str, workers: int = 2, progress=None):
|
|
88 |
else:
|
89 |
logging.warning(f"Image {img_path} does not exist and will not be zipped.")
|
90 |
|
91 |
-
if progress:
|
92 |
progress(1, desc=f"Completed {batch_id}")
|
93 |
|
94 |
logging.info(f"Completed download and zip for batch {batch_id}")
|
95 |
return zip_filename
|
96 |
|
97 |
def gradio_interface(batch_id_input, progress=gr.Progress()):
|
98 |
-
batch_id = batch_id_input.strip()
|
99 |
try:
|
100 |
-
zip_file = download_batch_images(
|
101 |
-
return zip_file
|
102 |
except Exception as e:
|
103 |
logging.error(f"Error processing batch: {e}")
|
104 |
-
|
105 |
|
106 |
with gr.Blocks() as app:
|
107 |
gr.Markdown("# Batch Image Downloader")
|
@@ -112,12 +107,11 @@ with gr.Blocks() as app:
|
|
112 |
download_button = gr.Button("Download Images")
|
113 |
with gr.Column():
|
114 |
output_file = gr.File(label="Download Zip File")
|
115 |
-
error_message = gr.Textbox(label="Error Message", interactive=False)
|
116 |
|
117 |
download_button.click(
|
118 |
gradio_interface,
|
119 |
inputs=[batch_id_input],
|
120 |
-
outputs=[output_file
|
121 |
)
|
122 |
|
123 |
app.queue()
|
|
|
2 |
import requests
|
3 |
import shutil
|
4 |
import gradio as gr
|
|
|
5 |
from zipfile import ZipFile
|
6 |
import logging
|
7 |
|
|
|
23 |
image_ids.append(image_id)
|
24 |
else:
|
25 |
logging.warning(f"Unexpected id format: {item['id']}")
|
26 |
+
if not image_ids:
|
27 |
+
raise ValueError("No images found in the manifest.")
|
28 |
logging.info(f"Found {len(image_ids)} images in batch {batch_id}")
|
29 |
return image_ids
|
30 |
|
|
|
43 |
logging.info(f"Successfully downloaded image: {dest}")
|
44 |
else:
|
45 |
logging.error(f"Failed to download image from {url}. Status code: {response.status_code}")
|
46 |
+
raise Exception(f"Failed to download image from {url}. Status code: {response.status_code}")
|
47 |
del response
|
48 |
|
49 |
def download_image_by_image_id(image_id: str):
|
|
|
58 |
dest = os.path.join(batch_id, image_id + ".jpg")
|
59 |
download_image(url, dest)
|
60 |
|
61 |
+
def download_batch_images(batch_id: str, progress=None):
|
62 |
logging.info(f"Starting download for batch {batch_id}")
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
if progress is not None:
|
65 |
progress(0, desc=f"Starting download for {batch_id}...")
|
66 |
|
67 |
+
image_ids = get_image_ids(batch_id)
|
68 |
+
total_images = len(image_ids)
|
69 |
+
|
70 |
+
for idx, image_id in enumerate(image_ids):
|
71 |
download_image_by_image_id(image_id)
|
72 |
logging.info(f"Downloaded image {image_id}")
|
73 |
+
if progress is not None:
|
|
|
74 |
current_progress = (idx + 1) / total_images
|
75 |
progress(current_progress, desc=f"Downloading {image_id}...")
|
76 |
|
|
|
|
|
|
|
|
|
77 |
logging.info(f"Zipping downloaded images for batch {batch_id}")
|
78 |
zip_filename = f"{batch_id}.zip"
|
79 |
with ZipFile(zip_filename, 'w') as zipf:
|
|
|
84 |
else:
|
85 |
logging.warning(f"Image {img_path} does not exist and will not be zipped.")
|
86 |
|
87 |
+
if progress is not None:
|
88 |
progress(1, desc=f"Completed {batch_id}")
|
89 |
|
90 |
logging.info(f"Completed download and zip for batch {batch_id}")
|
91 |
return zip_filename
|
92 |
|
93 |
def gradio_interface(batch_id_input, progress=gr.Progress()):
|
|
|
94 |
try:
|
95 |
+
zip_file = download_batch_images(batch_id_input, progress=progress)
|
96 |
+
return zip_file
|
97 |
except Exception as e:
|
98 |
logging.error(f"Error processing batch: {e}")
|
99 |
+
raise gr.Error(f"Error: {str(e)}")
|
100 |
|
101 |
with gr.Blocks() as app:
|
102 |
gr.Markdown("# Batch Image Downloader")
|
|
|
107 |
download_button = gr.Button("Download Images")
|
108 |
with gr.Column():
|
109 |
output_file = gr.File(label="Download Zip File")
|
|
|
110 |
|
111 |
download_button.click(
|
112 |
gradio_interface,
|
113 |
inputs=[batch_id_input],
|
114 |
+
outputs=[output_file]
|
115 |
)
|
116 |
|
117 |
app.queue()
|