Bubbles30 commited on
Commit
b9d7d7c
Β·
verified Β·
1 Parent(s): 69b33eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -49,7 +49,7 @@ from urllib.parse import urlparse, quote
49
 
50
  def download_image_fast(url, out_path, fail_log, code, suf):
51
  def log(msg):
52
- with open(fail_log, "a") as f:
53
  f.write(f"{code}{suf}: {msg}\n")
54
 
55
  parts = urlparse(url)
@@ -289,6 +289,8 @@ def handle_batch(workbook_path, fmt, w, h):
289
  outdir = os.path.join(tmpdir, 'out')
290
  os.makedirs(outdir, exist_ok=True)
291
  fail_log = os.path.join(tmpdir, 'failed.txt')
 
 
292
 
293
  try:
294
  jobs = read_links_from_workbook(workbook_path.name)
@@ -310,16 +312,15 @@ def handle_batch(workbook_path, fmt, w, h):
310
 
311
  # Download all images
312
  future_to_task = {}
313
- with ThreadPoolExecutor(max_workers=min(32, CPU_CORES * 2)) as ex:
 
314
  for code, url, dl_path, suf in tasks:
315
- fut = ex.submit(download_image_fast, url, dl_path, fail_log, code, suf)
316
- future_to_task[fut] = (code, url, dl_path, suf)
317
-
318
- for fut in as_completed(future_to_task):
319
  success = fut.result()
320
- code, url, dl_path, suf = future_to_task[fut]
321
  if not success:
322
- log_failure(fail_log, f"{code}{suf}: download failed")
323
 
324
  processed = []
325
  for code, url, dl_path, suf in tasks:
@@ -379,11 +380,12 @@ def handle_single_upload(uploaded_file, fmt, w, h):
379
  outp=os.path.join(tmpdir,f"single.{fmt.lower()}")
380
  if fmt.upper()=="JPEG":
381
  final_jpg = convert_to_jpeg(img, target=w)
382
- final_jpg.save(out_p, "JPEG", quality=95, optimize=True, progressive=True)
383
  else:
384
  final.save(outp, fmt.upper())
385
  return final, outp, "βœ… Single processed."
386
 
 
387
  def handle_single_url(url, fmt, w, h):
388
  if not url.strip():
389
  return None, None, "❌ No URL provided."
@@ -424,13 +426,15 @@ def handle_single_url(url, fmt, w, h):
424
 
425
  # 5) Save final image
426
  outp = os.path.join(tmpdir, f"single.{fmt.lower()}")
 
427
  try:
428
  if fmt.upper() == "JPEG":
429
  final_jpg = convert_to_jpeg(img, target=w)
430
- final_jpg.save(out_p, "JPEG", quality=95, optimize=True, progressive=True)
431
  else:
432
  final.save(outp, fmt.upper())
433
  except Exception as e:
 
434
  reason = str(e)
435
  shutil.rmtree(tmpdir)
436
  return None, None, f"❌ Save failed: {reason}"
@@ -821,11 +825,12 @@ with gr.Blocks(css=css) as demo:
821
  return files, zip_path, msg
822
  else:
823
  if uploads:
824
- _, fpath, msg = handle_single_upload(uploads, fmt, w, h)
825
  else:
826
- _, fpath, msg = handle_single_url(url, fmt, w, h)
827
  return ([fpath] if fpath else []), None, msg
828
 
 
829
  # ─── Wire up the Process button here ───────────────────
830
  process_btn_url.click(
831
  fn=dispatch,
 
49
 
50
  def download_image_fast(url, out_path, fail_log, code, suf):
51
  def log(msg):
52
+ thread_log_failure(f"{code}{suf}: {msg}")
53
  f.write(f"{code}{suf}: {msg}\n")
54
 
55
  parts = urlparse(url)
 
289
  outdir = os.path.join(tmpdir, 'out')
290
  os.makedirs(outdir, exist_ok=True)
291
  fail_log = os.path.join(tmpdir, 'failed.txt')
292
+ fail_lock = threading.Lock()
293
+ failures = []
294
 
295
  try:
296
  jobs = read_links_from_workbook(workbook_path.name)
 
312
 
313
  # Download all images
314
  future_to_task = {}
315
+ with ThreadPoolExecutor(max_workers=min(32, CPU_CORES * 2)) as ex:
316
+ futures = []
317
  for code, url, dl_path, suf in tasks:
318
+ fut = ex.submit(download_image_fast_mod, url, dl_path, code, suf)
319
+ futures.append((fut, code, suf))
320
+ for fut, code, suf in futures:
 
321
  success = fut.result()
 
322
  if not success:
323
+ thread_log_failure(f"{code}{suf}: download failed")
324
 
325
  processed = []
326
  for code, url, dl_path, suf in tasks:
 
380
  outp=os.path.join(tmpdir,f"single.{fmt.lower()}")
381
  if fmt.upper()=="JPEG":
382
  final_jpg = convert_to_jpeg(img, target=w)
383
+ final_jpg.save(outp, "JPEG", quality=95, optimize=True, progressive=True) # fixed outp instead of out_p
384
  else:
385
  final.save(outp, fmt.upper())
386
  return final, outp, "βœ… Single processed."
387
 
388
+
389
  def handle_single_url(url, fmt, w, h):
390
  if not url.strip():
391
  return None, None, "❌ No URL provided."
 
426
 
427
  # 5) Save final image
428
  outp = os.path.join(tmpdir, f"single.{fmt.lower()}")
429
+ # ...
430
  try:
431
  if fmt.upper() == "JPEG":
432
  final_jpg = convert_to_jpeg(img, target=w)
433
+ final_jpg.save(outp, "JPEG", quality=95, optimize=True, progressive=True) # fixed outp instead of out_p
434
  else:
435
  final.save(outp, fmt.upper())
436
  except Exception as e:
437
+ # ...
438
  reason = str(e)
439
  shutil.rmtree(tmpdir)
440
  return None, None, f"❌ Save failed: {reason}"
 
825
  return files, zip_path, msg
826
  else:
827
  if uploads:
828
+ files, fpath, msg = handle_single_upload(uploads, fmt, w, h)
829
  else:
830
+ files, fpath, msg = handle_single_url(url, fmt, w, h)
831
  return ([fpath] if fpath else []), None, msg
832
 
833
+
834
  # ─── Wire up the Process button here ───────────────────
835
  process_btn_url.click(
836
  fn=dispatch,