Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
|
| 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 |
-
|
|
|
|
| 314 |
for code, url, dl_path, suf in tasks:
|
| 315 |
-
fut = ex.submit(
|
| 316 |
-
|
| 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 |
-
|
| 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(
|
| 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(
|
| 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 |
-
|
| 825 |
else:
|
| 826 |
-
|
| 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,
|