File size: 4,264 Bytes
b6d30d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48092d5
 
 
b6d30d7
724953a
 
 
 
 
b6d30d7
 
724953a
b6d30d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48092d5
 
 
b6d30d7
 
 
 
 
 
 
 
 
 
 
724953a
 
 
b6d30d7
 
 
 
724953a
 
 
 
 
b6d30d7
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import gradio as gr
from Bio import Entrez
from datetime import datetime
import os

# Function to fetch PubMed articles
def fetch_pubmed(query, email, start_date, end_date, max_results=100):
    Entrez.email = email
    handle = Entrez.esearch(
        db="pubmed",
        term=query,
        retmax=max_results,
        sort="pub_date",
        mindate=start_date,
        maxdate=end_date,
        datetype="pdat"
    )
    results = Entrez.read(handle)
    handle.close()
    ids = results["IdList"]
    return ids

# Function to fetch article details
def fetch_details(pubmed_ids):
    handle = Entrez.efetch(db="pubmed", id=",".join(pubmed_ids), retmode="xml")
    records = Entrez.read(handle)
    handle.close()
    return records

# Function to compile Markdown content
def compile_summaries(records):
    content = "# PubMed Search Results\n\n"
    for record in records["PubmedArticle"]:
        title = record["MedlineCitation"]["Article"]["ArticleTitle"]
        abstract = record["MedlineCitation"]["Article"].get("Abstract", {}).get("AbstractText", ["No abstract available."])[0]
        url = f'https://pubmed.ncbi.nlm.nih.gov/{record["MedlineCitation"]["PMID"]}/'
        content += f"## [{title}]({url})\n\n{abstract}\n\n"
    return content

# Main function for the Gradio interface
def pubmed_search(mesh_terms, email, start_date, end_date):
    if not mesh_terms or not email:
        return "Please provide MeSH terms and email.", None, None
    
    if start_date is None or end_date is None:
        return "Please select both start and end dates.", None, None

    # Join MeSH terms for query
    query = " AND ".join([f'"{term}"[MeSH Terms]' for term in mesh_terms.split(", ")])
    
    # Format the start and end dates
    start_date_str = start_date.strftime("%Y/%m/%d %H:%M:%S")
    end_date_str = end_date.strftime("%Y/%m/%d %H:%M:%S")
    
    # Fetch PubMed IDs
    pubmed_ids = fetch_pubmed(query, email, start_date_str, end_date_str)
    if not pubmed_ids:
        return "No articles found for the given search terms and date range.", None, None
    
    # Fetch article details
    records = fetch_details(pubmed_ids)
    if not records["PubmedArticle"]:
        return "No detailed articles found.", None, None
    
    # Compile summaries
    markdown_content = compile_summaries(records)
    text_content = markdown_content.replace("\n", "\r\n")
    
    # Save files temporarily for download
    with open("results.md", "w") as md_file:
        md_file.write(markdown_content)
    with open("results.txt", "w") as txt_file:
        txt_file.write(text_content)
    
    return "Search completed successfully. Download your files below.", "results.md", "results.txt"

# Gradio interface components
with gr.Blocks() as app:
    gr.Markdown("# PubMed Search Tool for Thyroid AI Research")
    
    with gr.Row():
        mesh_term_input = gr.Textbox(label="Enter a MeSH Term", placeholder="e.g., thyroid", interactive=True)
        add_button = gr.Button("Add MeSH Term")
        mesh_terms_box = gr.Textbox(label="Added MeSH Terms", interactive=False, lines=2)
    
    # Default values for date-time fields
    start_date = gr.DateTime(label="Start Date and Time", value=datetime.now())
    end_date = gr.DateTime(label="End Date and Time", value=datetime.now())
    email_input = gr.Textbox(label="Email", placeholder="Your email (required by PubMed API)", interactive=True)
    
    search_button = gr.Button("Search PubMed")
    status_output = gr.Textbox(label="Status")
    
    markdown_file = gr.File(label="Markdown File", interactive=False)
    text_file = gr.File(label="Text File", interactive=False)

    # Logic for adding MeSH terms
    def add_mesh_term(term, terms):
        if term.strip():
            terms_list = terms.split(", ") if terms else []
            terms_list.append(term.strip())
            return ", ".join(terms_list)
        return terms

    # Bind functions to interface
    add_button.click(fn=add_mesh_term, inputs=[mesh_term_input, mesh_terms_box], outputs=mesh_terms_box)
    search_button.click(
        fn=pubmed_search,
        inputs=[mesh_terms_box, email_input, start_date, end_date],
        outputs=[status_output, markdown_file, text_file]
    )

# Launch app
app.launch()