Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,12 @@
|
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
import shutil
|
4 |
import gradio as gr
|
5 |
from zipfile import ZipFile
|
6 |
import logging
|
|
|
|
|
7 |
|
8 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
9 |
|
@@ -58,6 +61,28 @@ 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 |
|
@@ -97,22 +122,54 @@ def gradio_interface(batch_id_input, progress=gr.Progress()):
|
|
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("#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
-
with gr.
|
105 |
-
|
106 |
-
|
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()
|
118 |
-
app.launch()
|
|
|
1 |
+
from concurrent.futures import ThreadPoolExecutor
|
2 |
import os
|
3 |
import requests
|
4 |
import shutil
|
5 |
import gradio as gr
|
6 |
from zipfile import ZipFile
|
7 |
import logging
|
8 |
+
from typing import IO
|
9 |
+
|
10 |
|
11 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
12 |
|
|
|
61 |
dest = os.path.join(batch_id, image_id + ".jpg")
|
62 |
download_image(url, dest)
|
63 |
|
64 |
+
def rest_download_batch_images(batch_id: str) -> str:
|
65 |
+
|
66 |
+
image_ids = get_image_ids(batch_id)
|
67 |
+
|
68 |
+
|
69 |
+
def track_download(image_id):
|
70 |
+
download_image_by_image_id(image_id)
|
71 |
+
|
72 |
+
with ThreadPoolExecutor() as executor:
|
73 |
+
for _, image_id in enumerate(image_ids):
|
74 |
+
executor.submit(track_download, image_id)
|
75 |
+
|
76 |
+
zip_filename = f"{batch_id}.zip"
|
77 |
+
with ZipFile(zip_filename, 'w') as zipf:
|
78 |
+
for image_id in image_ids:
|
79 |
+
img_path = os.path.join(batch_id, f"{image_id}.jpg")
|
80 |
+
if os.path.exists(img_path):
|
81 |
+
zipf.write(img_path, arcname=os.path.basename(img_path))
|
82 |
+
|
83 |
+
return zip_filename
|
84 |
+
|
85 |
+
|
86 |
def download_batch_images(batch_id: str, progress=None):
|
87 |
logging.info(f"Starting download for batch {batch_id}")
|
88 |
|
|
|
122 |
except Exception as e:
|
123 |
logging.error(f"Error processing batch: {e}")
|
124 |
raise gr.Error(f"Error: {str(e)}")
|
125 |
+
|
126 |
+
def rest_gradio_interface(batch_id_input :str ) -> IO[bytes]:
|
127 |
+
try:
|
128 |
+
zip_file = rest_download_batch_images(batch_id_input)
|
129 |
+
return zip_file
|
130 |
+
except Exception as e:
|
131 |
+
logging.error(f"Error processing batch: {e}")
|
132 |
+
raise gr.Error(f"Error: {str(e)}")
|
133 |
+
|
134 |
|
135 |
with gr.Blocks() as app:
|
136 |
+
gr.Markdown("# IIIF Downloader")
|
137 |
+
|
138 |
+
with gr.Tab("Download Batch"):
|
139 |
+
|
140 |
+
with gr.Row():
|
141 |
+
with gr.Column():
|
142 |
+
batch_id_input = gr.Textbox(label="Batch ID", placeholder="Enter batch ID.")
|
143 |
+
download_button = gr.Button("Download Images")
|
144 |
+
|
145 |
+
with gr.Column():
|
146 |
+
output_file = gr.File(label="Download Zip File")
|
147 |
+
|
148 |
+
download_button.click(
|
149 |
+
gradio_interface,
|
150 |
+
inputs=[batch_id_input],
|
151 |
+
outputs=[output_file]
|
152 |
+
)
|
153 |
+
|
154 |
+
download_button.click(
|
155 |
+
rest_gradio_interface,
|
156 |
+
api_name="iiif_rest_download" ,
|
157 |
+
inputs=[batch_id_input],
|
158 |
+
outputs=[output_file]
|
159 |
+
)
|
160 |
+
|
161 |
+
with gr.Tab("Multiple Batches"):
|
162 |
+
gr.Markdown("WIP")
|
163 |
+
gr.Markdown("Make it possible to download batches to a huggingface account so it can be used through fastapi")
|
164 |
+
gr.Markdown("Will uses threading")
|
165 |
+
|
166 |
+
pass
|
167 |
+
|
168 |
|
169 |
+
with gr.Tab("How to use"):
|
170 |
+
gr.Markdown("WIP, instructional video")
|
171 |
+
pass
|
|
|
|
|
|
|
172 |
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
app.queue()
|
175 |
+
app.launch()
|