Gabriel commited on
Commit
d543428
1 Parent(s): c6194f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -21
app.py CHANGED
@@ -8,7 +8,7 @@ import logging
8
 
9
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
10
 
11
- IIIF_URL = "https://lbiiif.riksarkivet.se" #"https://iiifintern.ra.se"
12
 
13
  def get_image_ids(batch_id: str) -> list[str]:
14
  """A list of image IDs in the given batch"""
@@ -57,17 +57,17 @@ def download_batch_images(batch_id: str, workers: int = 2, progress=None):
57
  if progress:
58
  progress(0, desc=f"Starting download for {batch_id}...")
59
 
60
- def track_download(image_id):
61
  download_image_by_image_id(image_id)
62
  logging.info(f"Downloaded image {image_id}")
63
  if progress:
64
  # Update progress after each image
65
- current_progress = (image_ids.index(image_id) + 1) / total_images
66
  progress(current_progress, desc=f"Downloading {image_id}...")
67
 
68
  with ThreadPoolExecutor(max_workers=workers) as executor:
69
- for image_id in image_ids:
70
- executor.submit(track_download, image_id)
71
 
72
  logging.info(f"Zipping downloaded images for batch {batch_id}")
73
  zip_filename = f"{batch_id}.zip"
@@ -85,19 +85,13 @@ def download_batch_images(batch_id: str, workers: int = 2, progress=None):
85
  logging.info(f"Completed download and zip for batch {batch_id}")
86
  return zip_filename
87
 
88
-
89
- def gradio_interface(batch_ids_input, progress=gr.Progress()):
90
- batch_ids = [batch_id.strip() for batch_id in batch_ids_input.split("\n") if batch_id.strip()]
91
-
92
- zip_files = []
93
  try:
94
- for batch_id in progress.tqdm(batch_ids, desc="Processing batches"):
95
- logging.info(f"Processing batch {batch_id}")
96
- zip_file = download_batch_images(batch_id, progress=progress)
97
- zip_files.append(zip_file)
98
- return zip_files # Return the list of zip files for download
99
  except Exception as e:
100
- logging.error(f"Error processing batches: {e}")
101
  return str(e)
102
 
103
  with gr.Blocks() as app:
@@ -105,16 +99,15 @@ with gr.Blocks() as app:
105
 
106
  with gr.Row():
107
  with gr.Column():
108
- batch_ids_input = gr.Textbox(label="Batch IDs (one per line)", placeholder="Enter batch IDs, one per line.")
109
  download_button = gr.Button("Download Images")
110
  with gr.Column():
111
- output_files = gr.File(label="Download Zip Files", file_count="multiple")
112
-
113
 
114
  download_button.click(
115
  gradio_interface,
116
- inputs=[batch_ids_input],
117
- outputs=[output_files]
118
  )
119
 
120
  app.queue()
 
8
 
9
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
10
 
11
+ IIIF_URL = "https://lbiiif.riksarkivet.se" # "https://iiifintern.ra.se"
12
 
13
  def get_image_ids(batch_id: str) -> list[str]:
14
  """A list of image IDs in the given batch"""
 
57
  if progress:
58
  progress(0, desc=f"Starting download for {batch_id}...")
59
 
60
+ def track_download(image_id, idx):
61
  download_image_by_image_id(image_id)
62
  logging.info(f"Downloaded image {image_id}")
63
  if progress:
64
  # Update progress after each image
65
+ current_progress = (idx + 1) / total_images
66
  progress(current_progress, desc=f"Downloading {image_id}...")
67
 
68
  with ThreadPoolExecutor(max_workers=workers) as executor:
69
+ for idx, image_id in enumerate(image_ids):
70
+ executor.submit(track_download, image_id, idx)
71
 
72
  logging.info(f"Zipping downloaded images for batch {batch_id}")
73
  zip_filename = f"{batch_id}.zip"
 
85
  logging.info(f"Completed download and zip for batch {batch_id}")
86
  return zip_filename
87
 
88
+ def gradio_interface(batch_id_input, progress=gr.Progress()):
89
+ batch_id = batch_id_input.strip()
 
 
 
90
  try:
91
+ zip_file = download_batch_images(batch_id, progress=progress)
92
+ return zip_file # Return the zip file for download
 
 
 
93
  except Exception as e:
94
+ logging.error(f"Error processing batch: {e}")
95
  return str(e)
96
 
97
  with gr.Blocks() as app:
 
99
 
100
  with gr.Row():
101
  with gr.Column():
102
+ batch_id_input = gr.Textbox(label="Batch ID", placeholder="Enter batch ID.")
103
  download_button = gr.Button("Download Images")
104
  with gr.Column():
105
+ output_file = gr.File(label="Download Zip File")
 
106
 
107
  download_button.click(
108
  gradio_interface,
109
+ inputs=[batch_id_input],
110
+ outputs=[output_file]
111
  )
112
 
113
  app.queue()