-
-
Save joowkim/0c382cec70f9d4fe698965437f9fba73 to your computer and use it in GitHub Desktop.
Pubmed ChatGPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import openai | |
import json | |
import pandas as pd | |
from Bio import Entrez | |
from datetime import datetime | |
from PyQt6 import QtWidgets, QtGui | |
from PyQt6.QtCore import Qt | |
# Replace with your own ChatGPT API key | |
openai.api_key = "API Key" | |
Entrez.email = "Email" # Replace with your email | |
def generate_overall_introduction(article_infos): | |
prompt = f"Write an overall introduction for a research paper based on the following articles:\n\n{article_infos}\n\nCite each article in the introduction and provide a reference list at the end." | |
with open("prompt.txt", "w") as f: | |
f.write(prompt) | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[{"role": "assistant", "content": prompt}] | |
) | |
return response["choices"][0]["message"]["content"] | |
def search_pubmed_articles(keywords, max_results=5): | |
search_term = " AND ".join(keywords) | |
handle = Entrez.esearch(db="pubmed", term=search_term, sort="relevance", retmax=max_results) | |
record = Entrez.read(handle) | |
handle.close() | |
return record["IdList"] | |
def fetch_article_details(article_id): | |
handle = Entrez.efetch(db="pubmed", id=article_id, rettype="xml", retmode="text") | |
article = Entrez.read(handle)["PubmedArticle"][0] | |
handle.close() | |
return article | |
def format_authors(authors): | |
formatted_authors = [] | |
for author in authors: | |
last_name = author.get("LastName", "") | |
initials = author.get("Initials", "") | |
formatted_authors.append(f"{last_name}, {initials}.") | |
return ", ".join(formatted_authors) | |
def summarize_abstract(abstract): | |
prompt = "Summarize the following abstract in three bullet points:"+abstract | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[{"role": "assistant", "content":prompt}] | |
) | |
return response["choices"][0]["message"]["content"] | |
def format_apa_citation(article): | |
authors = format_authors(article["MedlineCitation"]["Article"]["AuthorList"]) | |
title = article["MedlineCitation"]["Article"]["ArticleTitle"] | |
journal = article["MedlineCitation"]["Article"]["Journal"]["Title"] | |
pub_year = datetime.strptime(article["MedlineCitation"]["Article"]["Journal"]["JournalIssue"]["PubDate"]["Year"], "%Y").year | |
volume = article["MedlineCitation"]["Article"]["Journal"]["JournalIssue"]["Volume"] | |
#issue = article["MedlineCitation"]["Article"]["Journal"]["JournalIssue"]["Issue"] | |
pages = article["MedlineCitation"]["Article"]["Pagination"]["MedlinePgn"] | |
return f"{authors} ({pub_year}). {title}. {journal}, {volume}, {pages}." | |
class MainWindow(QtWidgets.QWidget): | |
def __init__(self): | |
super().__init__() | |
self.init_ui() | |
def init_ui(self): | |
self.keywords_label = QtWidgets.QLabel('Keywords:') | |
self.keywords_input = QtWidgets.QLineEdit() | |
self.search_button = QtWidgets.QPushButton('Search') | |
self.search_button.clicked.connect(self.search_and_export) | |
self.status_label = QtWidgets.QLabel('') | |
self.layout = QtWidgets.QVBoxLayout() | |
self.layout.addWidget(self.keywords_label) | |
self.layout.addWidget(self.keywords_input) | |
self.layout.addWidget(self.search_button) | |
self.layout.addWidget(self.status_label) | |
self.setLayout(self.layout) | |
self.setWindowTitle('PubMed Article Search') | |
def generate_overall_introduction(article_infos): | |
prompt = f"Write an overall introduction for a research paper based on the following articles:\n\n{article_infos}\n\nCite each article in the introduction and provide a reference list at the end." | |
with open("prompt.txt", "w") as f: | |
f.write(prompt) | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[{"role": "assistant", "content": prompt}] | |
) | |
return response["choices"][0]["message"]["content"] | |
# ... (Keep the MainWindow class and init_ui function) | |
def search_and_export(self): | |
self.status_label.setText('Searching...') | |
QtWidgets.QApplication.processEvents() | |
keywords = self.keywords_input.text().split(', ') | |
article_ids = search_pubmed_articles(keywords) | |
results = [] | |
article_infos = "" | |
for i, article_id in enumerate(article_ids): | |
article = fetch_article_details(article_id) | |
authors = format_authors(article["MedlineCitation"]["Article"]["AuthorList"]) | |
title = article["MedlineCitation"]["Article"]["ArticleTitle"] | |
journal = article["MedlineCitation"]["Article"]["Journal"]["Title"] | |
abstract = article["MedlineCitation"]["Article"]["Abstract"]["AbstractText"][0] | |
pub_year = datetime.strptime(article["MedlineCitation"]["Article"]["Journal"]["JournalIssue"]["PubDate"]["Year"], "%Y").year | |
apa_citation = format_apa_citation(article) | |
results.append({ | |
"Authors": authors, | |
"Title": title, | |
"Journal": journal, | |
"Publication Year": pub_year, | |
"Abstract": abstract, | |
"APA Citation": apa_citation | |
}) | |
article_info = f"Article {i+1}:\nAuthors: {authors}\nTitle: {title}\nJournal: {journal}\nPublication Year: {pub_year}\nAbstract: {abstract}\nAPA Citation: {apa_citation}\n\n" | |
article_infos += article_info | |
# Save results to Excel file | |
df = pd.DataFrame(results) | |
df.to_excel("pubmed_results.xlsx", index=False) | |
overall_introduction = generate_overall_introduction(article_infos) | |
with open("overall_introduction.txt", "w") as f: | |
f.write(overall_introduction) | |
self.status_label.setText('Done!') | |
if __name__ == '__main__': | |
app = QtWidgets.QApplication(sys.argv) | |
main_window = MainWindow() | |
main_window.show() | |
sys.exit(app.exec()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment