File size: 2,913 Bytes
c5e90ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import gradio as gr
import urllib.request
from bs4 import BeautifulSoup
import re

def get_citation(bibcode):
    """Fetch citation from ADS bibcode"""
    bibtex_url = f'https://ui.adsabs.harvard.edu/abs/{bibcode}/exportcitation'
    with urllib.request.urlopen(bibtex_url) as response:
        bibtex = response.read().decode('utf-8')
    soup = BeautifulSoup(bibtex, 'html.parser')
    citation_text = soup.textarea.text
    return citation_text

def process_bibcodes(bibcode_input):
    """Process single or multiple bibcodes and generate .bib file"""
    # Split input by newlines, commas, or spaces and clean up
    bibcodes = re.split(r'[,\n\s]+', bibcode_input.strip())
    bibcodes = [b.strip() for b in bibcodes if b.strip()]
    
    if not bibcodes:
        return "Error: No bibcodes provided", None
    
    all_citations = []
    errors = []
    
    for bibcode in bibcodes:
        try:
            citation = get_citation(bibcode)
            all_citations.append(citation)
        except Exception as e:
            errors.append(f"Error fetching {bibcode}: {str(e)}")
    
    if not all_citations:
        return "Error: Could not fetch any citations\n" + "\n".join(errors), None
    
    # Combine all citations
    combined_bib = "\n\n".join(all_citations)
    
    # Create status message
    status = f"Successfully fetched {len(all_citations)} citation(s)"
    if errors:
        status += "\n\nErrors:\n" + "\n".join(errors)
    
    # Save to file
    output_filename = "citations.bib"
    with open(output_filename, 'w', encoding='utf-8') as f:
        f.write(combined_bib)
    
    return status, output_filename

# Create Gradio interface
with gr.Blocks(title="ADS BibTeX Citation Generator") as demo:
    gr.Markdown("# ADS BibTeX Citation Generator")
    gr.Markdown("Enter one or multiple ADS bibcodes to generate a .bib file. Separate multiple bibcodes with commas, spaces, or newlines.")
    
    with gr.Row():
        with gr.Column():
            bibcode_input = gr.Textbox(
                label="Bibcode(s)",
                placeholder="e.g., 2021Natur.592..534C\nor multiple:\n2021Natur.592..534C, 1999AJ....118.2751M",
                lines=5
            )
            submit_btn = gr.Button("Generate .bib File", variant="primary")
        
        with gr.Column():
            status_output = gr.Textbox(label="Status", lines=5)
            file_output = gr.File(label="Download .bib File")
    
    # Examples
    gr.Examples(
        examples=[
            ["2021Natur.592..534C"],
            ["2021Natur.592..534C\n1999AJ....118.2751M"],
            ["2021Natur.592..534C, 1999AJ....118.2751M, 2019ApJ...887..235P"]
        ],
        inputs=bibcode_input
    )
    
    submit_btn.click(
        fn=process_bibcodes,
        inputs=bibcode_input,
        outputs=[status_output, file_output]
    )

# Launch the app
if __name__ == "__main__":
    demo.launch()