Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,102 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import gradio as gr
         | 
| 2 | 
            +
            from Bio import Entrez
         | 
| 3 | 
            +
            from datetime import datetime
         | 
| 4 | 
            +
            import os
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            # Function to fetch PubMed articles
         | 
| 7 | 
            +
            def fetch_pubmed(query, email, start_date, end_date, max_results=100):
         | 
| 8 | 
            +
                Entrez.email = email
         | 
| 9 | 
            +
                handle = Entrez.esearch(
         | 
| 10 | 
            +
                    db="pubmed",
         | 
| 11 | 
            +
                    term=query,
         | 
| 12 | 
            +
                    retmax=max_results,
         | 
| 13 | 
            +
                    sort="pub_date",
         | 
| 14 | 
            +
                    mindate=start_date,
         | 
| 15 | 
            +
                    maxdate=end_date,
         | 
| 16 | 
            +
                    datetype="pdat"
         | 
| 17 | 
            +
                )
         | 
| 18 | 
            +
                results = Entrez.read(handle)
         | 
| 19 | 
            +
                handle.close()
         | 
| 20 | 
            +
                ids = results["IdList"]
         | 
| 21 | 
            +
                return ids
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            # Function to fetch article details
         | 
| 24 | 
            +
            def fetch_details(pubmed_ids):
         | 
| 25 | 
            +
                handle = Entrez.efetch(db="pubmed", id=",".join(pubmed_ids), retmode="xml")
         | 
| 26 | 
            +
                records = Entrez.read(handle)
         | 
| 27 | 
            +
                handle.close()
         | 
| 28 | 
            +
                return records
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            # Function to compile Markdown content
         | 
| 31 | 
            +
            def compile_summaries(records):
         | 
| 32 | 
            +
                content = "# PubMed Search Results\n\n"
         | 
| 33 | 
            +
                for record in records["PubmedArticle"]:
         | 
| 34 | 
            +
                    title = record["MedlineCitation"]["Article"]["ArticleTitle"]
         | 
| 35 | 
            +
                    abstract = record["MedlineCitation"]["Article"].get("Abstract", {}).get("AbstractText", ["No abstract available."])[0]
         | 
| 36 | 
            +
                    url = f'https://pubmed.ncbi.nlm.nih.gov/{record["MedlineCitation"]["PMID"]}/'
         | 
| 37 | 
            +
                    content += f"## [{title}]({url})\n\n{abstract}\n\n"
         | 
| 38 | 
            +
                return content
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            # Main function for the Gradio interface
         | 
| 41 | 
            +
            def pubmed_search(mesh_terms, email, start_date, end_date):
         | 
| 42 | 
            +
                if not mesh_terms or not email:
         | 
| 43 | 
            +
                    return "Please provide MeSH terms and email.", None, None
         | 
| 44 | 
            +
                
         | 
| 45 | 
            +
                # Join MeSH terms for query
         | 
| 46 | 
            +
                query = " AND ".join([f'"{term}"[MeSH Terms]' for term in mesh_terms])
         | 
| 47 | 
            +
                
         | 
| 48 | 
            +
                # Fetch PubMed IDs
         | 
| 49 | 
            +
                pubmed_ids = fetch_pubmed(query, email, start_date, end_date)
         | 
| 50 | 
            +
                if not pubmed_ids:
         | 
| 51 | 
            +
                    return "No articles found for the given search terms and date range.", None, None
         | 
| 52 | 
            +
                
         | 
| 53 | 
            +
                # Fetch article details
         | 
| 54 | 
            +
                records = fetch_details(pubmed_ids)
         | 
| 55 | 
            +
                if not records["PubmedArticle"]:
         | 
| 56 | 
            +
                    return "No detailed articles found.", None, None
         | 
| 57 | 
            +
                
         | 
| 58 | 
            +
                # Compile summaries
         | 
| 59 | 
            +
                markdown_content = compile_summaries(records)
         | 
| 60 | 
            +
                text_content = markdown_content.replace("\n", "\r\n")
         | 
| 61 | 
            +
                
         | 
| 62 | 
            +
                # Save files temporarily for download
         | 
| 63 | 
            +
                with open("results.md", "w") as md_file:
         | 
| 64 | 
            +
                    md_file.write(markdown_content)
         | 
| 65 | 
            +
                with open("results.txt", "w") as txt_file:
         | 
| 66 | 
            +
                    txt_file.write(text_content)
         | 
| 67 | 
            +
                
         | 
| 68 | 
            +
                return "Search completed successfully. Download your files below.", "results.md", "results.txt"
         | 
| 69 | 
            +
             | 
| 70 | 
            +
            # Gradio interface components
         | 
| 71 | 
            +
            with gr.Blocks() as app:
         | 
| 72 | 
            +
                gr.Markdown("# PubMed Search Tool for Thyroid AI Research")
         | 
| 73 | 
            +
                
         | 
| 74 | 
            +
                with gr.Row():
         | 
| 75 | 
            +
                    mesh_term_input = gr.Textbox(label="Enter a MeSH Term", placeholder="e.g., thyroid", interactive=True)
         | 
| 76 | 
            +
                    add_button = gr.Button("Add MeSH Term")
         | 
| 77 | 
            +
                    mesh_terms_box = gr.Textbox(label="Added MeSH Terms", interactive=False, lines=2)
         | 
| 78 | 
            +
                
         | 
| 79 | 
            +
                start_date = gr.Date(label="Start Date")
         | 
| 80 | 
            +
                end_date = gr.Date(label="End Date")
         | 
| 81 | 
            +
                email_input = gr.Textbox(label="Email", placeholder="Your email (required by PubMed API)", interactive=True)
         | 
| 82 | 
            +
                
         | 
| 83 | 
            +
                search_button = gr.Button("Search PubMed")
         | 
| 84 | 
            +
                status_output = gr.Textbox(label="Status")
         | 
| 85 | 
            +
                
         | 
| 86 | 
            +
                markdown_file = gr.File(label="Markdown File", interactive=False)
         | 
| 87 | 
            +
                text_file = gr.File(label="Text File", interactive=False)
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                # Logic for adding MeSH terms
         | 
| 90 | 
            +
                def add_mesh_term(term, terms):
         | 
| 91 | 
            +
                    if term.strip():
         | 
| 92 | 
            +
                        terms = terms.split(",") if terms else []
         | 
| 93 | 
            +
                        terms.append(term.strip())
         | 
| 94 | 
            +
                        return ", ".join(terms)
         | 
| 95 | 
            +
                    return terms
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                # Bind functions to interface
         | 
| 98 | 
            +
                add_button.click(fn=add_mesh_term, inputs=[mesh_term_input, mesh_terms_box], outputs=mesh_terms_box)
         | 
| 99 | 
            +
                search_button.click(fn=pubmed_search, inputs=[mesh_terms_box, email_input, start_date, end_date], outputs=[status_output, markdown_file, text_file])
         | 
| 100 | 
            +
             | 
| 101 | 
            +
            # Launch app
         | 
| 102 | 
            +
            app.launch()
         | 
