Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -420,8 +420,8 @@ class PaperDownloader:
|
|
420 |
|
421 |
return None
|
422 |
|
423 |
-
def download_single_doi(self, doi):
|
424 |
-
"""Downloads a single paper using a DOI"""
|
425 |
if not doi:
|
426 |
return None, "Error: DOI not provided", "Error: DOI not provided"
|
427 |
|
@@ -436,17 +436,20 @@ class PaperDownloader:
|
|
436 |
with open(filepath, 'wb') as f:
|
437 |
f.write(pdf_content)
|
438 |
logger.info(f"Successfully downloaded: {filename}")
|
|
|
439 |
return filepath, f'<div style="display: flex; align-items: center;">✓ <a href="https://doi.org/{doi}">{doi}</a> <button onclick="copyLink(this)">Copy</button></div>', ""
|
440 |
else:
|
441 |
logger.warning(f"Could not download: {doi}")
|
|
|
442 |
return None, f"Could not download {doi}", f'<div style="display: flex; align-items: center;">❌ <a href="https://doi.org/{doi}">{doi}</a> <button onclick="copyLink(this)">Copy</button></div>'
|
443 |
|
444 |
except Exception as e:
|
445 |
logger.error(f"Error processing {doi}: {e}")
|
|
|
446 |
return None, f"Error processing {doi}: {e}", f"Error processing {doi}: {e}"
|
447 |
|
448 |
-
def download_multiple_dois(self, dois_text):
|
449 |
-
"""Downloads multiple papers from a list of DOIs"""
|
450 |
if not dois_text:
|
451 |
return None, "Error: No DOIs provided", "Error: No DOIs provided"
|
452 |
|
@@ -457,8 +460,8 @@ class PaperDownloader:
|
|
457 |
downloaded_files = []
|
458 |
failed_dois = []
|
459 |
downloaded_links = []
|
460 |
-
for i, doi in enumerate(
|
461 |
-
filepath, success_message, fail_message = self.download_single_doi(doi)
|
462 |
if filepath:
|
463 |
# Unique filename for zip
|
464 |
filename = f"{str(doi).replace('/', '_').replace('.', '_')}_{i}.pdf"
|
@@ -479,8 +482,8 @@ class PaperDownloader:
|
|
479 |
|
480 |
return zip_filename if downloaded_files else None, "\n".join(downloaded_links), "\n".join(failed_dois)
|
481 |
|
482 |
-
def process_bibtex(self, bib_file):
|
483 |
-
"""Process BibTeX file and download papers with multiple strategies"""
|
484 |
# Read BibTeX file content from the uploaded object
|
485 |
try:
|
486 |
with open(bib_file.name, 'r', encoding='utf-8') as f:
|
@@ -506,7 +509,7 @@ class PaperDownloader:
|
|
506 |
downloaded_links = []
|
507 |
|
508 |
# Download PDFs
|
509 |
-
for doi in
|
510 |
try:
|
511 |
# Try to download with multiple methods with retries
|
512 |
pdf_content = self.download_with_retry(doi)
|
@@ -541,7 +544,7 @@ class PaperDownloader:
|
|
541 |
|
542 |
return zip_filename, "\n".join(downloaded_links), "\n".join(failed_dois), None
|
543 |
|
544 |
-
async def process_bibtex_async(self, bib_file):
|
545 |
"""Process BibTeX file and download papers with multiple strategies"""
|
546 |
# Read BibTeX file content from the uploaded object
|
547 |
try:
|
@@ -568,7 +571,7 @@ class PaperDownloader:
|
|
568 |
downloaded_links = []
|
569 |
|
570 |
# Download PDFs
|
571 |
-
for doi in
|
572 |
try:
|
573 |
# Try to download with multiple methods with retries
|
574 |
pdf_content = await self.download_with_retry_async(doi)
|
@@ -607,19 +610,19 @@ def create_gradio_interface():
|
|
607 |
"""Create Gradio interface for Paper Downloader"""
|
608 |
downloader = PaperDownloader()
|
609 |
|
610 |
-
async def download_papers(bib_file, doi_input, dois_input):
|
611 |
if bib_file:
|
612 |
# Check file type
|
613 |
if not bib_file.name.lower().endswith('.bib'):
|
614 |
return None, "Error: Please upload a .bib file", "Error: Please upload a .bib file", None
|
615 |
|
616 |
-
zip_path, downloaded_dois, failed_dois, _ = await downloader.process_bibtex_async(bib_file)
|
617 |
return zip_path, downloaded_dois, failed_dois, None
|
618 |
elif doi_input:
|
619 |
-
filepath, message, failed_doi = downloader.download_single_doi(doi_input)
|
620 |
return None, message, failed_doi, filepath
|
621 |
elif dois_input:
|
622 |
-
zip_path, downloaded_dois, failed_dois = downloader.download_multiple_dois(dois_input)
|
623 |
return zip_path, downloaded_dois, failed_dois, None
|
624 |
else:
|
625 |
return None, "Please provide a .bib file, a single DOI, or a list of DOIs", "Please provide a .bib file, a single DOI, or a list of DOIs", None
|
|
|
420 |
|
421 |
return None
|
422 |
|
423 |
+
def download_single_doi(self, doi, progress=gr.Progress()):
|
424 |
+
"""Downloads a single paper using a DOI with progress bar"""
|
425 |
if not doi:
|
426 |
return None, "Error: DOI not provided", "Error: DOI not provided"
|
427 |
|
|
|
436 |
with open(filepath, 'wb') as f:
|
437 |
f.write(pdf_content)
|
438 |
logger.info(f"Successfully downloaded: {filename}")
|
439 |
+
progress(1, desc=f"Downloaded {doi}") #update progress
|
440 |
return filepath, f'<div style="display: flex; align-items: center;">✓ <a href="https://doi.org/{doi}">{doi}</a> <button onclick="copyLink(this)">Copy</button></div>', ""
|
441 |
else:
|
442 |
logger.warning(f"Could not download: {doi}")
|
443 |
+
progress(1, desc=f"Failed {doi}") #update progress
|
444 |
return None, f"Could not download {doi}", f'<div style="display: flex; align-items: center;">❌ <a href="https://doi.org/{doi}">{doi}</a> <button onclick="copyLink(this)">Copy</button></div>'
|
445 |
|
446 |
except Exception as e:
|
447 |
logger.error(f"Error processing {doi}: {e}")
|
448 |
+
progress(1, desc=f"Error {doi}") #update progress
|
449 |
return None, f"Error processing {doi}: {e}", f"Error processing {doi}: {e}"
|
450 |
|
451 |
+
def download_multiple_dois(self, dois_text, progress=gr.Progress()):
|
452 |
+
"""Downloads multiple papers from a list of DOIs with progress bar"""
|
453 |
if not dois_text:
|
454 |
return None, "Error: No DOIs provided", "Error: No DOIs provided"
|
455 |
|
|
|
460 |
downloaded_files = []
|
461 |
failed_dois = []
|
462 |
downloaded_links = []
|
463 |
+
for i, doi in enumerate(progress(dois, desc="Downloading papers")):
|
464 |
+
filepath, success_message, fail_message = self.download_single_doi(doi, progress=progress)
|
465 |
if filepath:
|
466 |
# Unique filename for zip
|
467 |
filename = f"{str(doi).replace('/', '_').replace('.', '_')}_{i}.pdf"
|
|
|
482 |
|
483 |
return zip_filename if downloaded_files else None, "\n".join(downloaded_links), "\n".join(failed_dois)
|
484 |
|
485 |
+
def process_bibtex(self, bib_file, progress=gr.Progress()):
|
486 |
+
"""Process BibTeX file and download papers with multiple strategies with progress bar"""
|
487 |
# Read BibTeX file content from the uploaded object
|
488 |
try:
|
489 |
with open(bib_file.name, 'r', encoding='utf-8') as f:
|
|
|
509 |
downloaded_links = []
|
510 |
|
511 |
# Download PDFs
|
512 |
+
for doi in progress(dois, desc="Downloading papers"):
|
513 |
try:
|
514 |
# Try to download with multiple methods with retries
|
515 |
pdf_content = self.download_with_retry(doi)
|
|
|
544 |
|
545 |
return zip_filename, "\n".join(downloaded_links), "\n".join(failed_dois), None
|
546 |
|
547 |
+
async def process_bibtex_async(self, bib_file, progress=gr.Progress()):
|
548 |
"""Process BibTeX file and download papers with multiple strategies"""
|
549 |
# Read BibTeX file content from the uploaded object
|
550 |
try:
|
|
|
571 |
downloaded_links = []
|
572 |
|
573 |
# Download PDFs
|
574 |
+
for doi in progress(dois, desc="Downloading papers"):
|
575 |
try:
|
576 |
# Try to download with multiple methods with retries
|
577 |
pdf_content = await self.download_with_retry_async(doi)
|
|
|
610 |
"""Create Gradio interface for Paper Downloader"""
|
611 |
downloader = PaperDownloader()
|
612 |
|
613 |
+
async def download_papers(bib_file, doi_input, dois_input, progress=gr.Progress()):
|
614 |
if bib_file:
|
615 |
# Check file type
|
616 |
if not bib_file.name.lower().endswith('.bib'):
|
617 |
return None, "Error: Please upload a .bib file", "Error: Please upload a .bib file", None
|
618 |
|
619 |
+
zip_path, downloaded_dois, failed_dois, _ = await downloader.process_bibtex_async(bib_file, progress)
|
620 |
return zip_path, downloaded_dois, failed_dois, None
|
621 |
elif doi_input:
|
622 |
+
filepath, message, failed_doi = downloader.download_single_doi(doi_input, progress)
|
623 |
return None, message, failed_doi, filepath
|
624 |
elif dois_input:
|
625 |
+
zip_path, downloaded_dois, failed_dois = downloader.download_multiple_dois(dois_input, progress)
|
626 |
return zip_path, downloaded_dois, failed_dois, None
|
627 |
else:
|
628 |
return None, "Please provide a .bib file, a single DOI, or a list of DOIs", "Please provide a .bib file, a single DOI, or a list of DOIs", None
|