Please correct and enhance the following: # Install necessary libraries !pip install…
- User
Please correct and enhance the following: # Install necessary libraries !pip install transformers datasets pandas numpy nltk torch scikit-learn ipywidgets evaluate --quiet !pip install sacremoses sentencepiece --quiet # Required for some tokenizers
Import libraries
import pandas as pd import numpy as np import os import torch from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, TrainingArguments, Trainer from datasets import Dataset, DatasetDict, concatenate_datasets, load_dataset from sklearn.model_selection import train_test_split import nltk nltk.download('punkt') from nltk.tokenize import sent_tokenize from tqdm.notebook import tqdm import warnings warnings.filterwarnings('ignore') import ipywidgets as widgets from IPython.display import display, clear_output import evaluate
Check if GPU is available
device = 'cuda' if torch.cuda.is_available() else 'cpu' print(f'Using device: {device}')
Function to load datasets
def load_datasets(): datasets = {}
# MedNLI Dataset mednli = load_dataset('mednli') datasets['mednli'] = mednli['train'] # Disease Symptoms Dataset symptoms_url = 'https://raw.githubusercontent.com/iamAnki/Medicine-Chatbot/master/dataset.csv' symptoms_data = pd.read_csv(symptoms_url) datasets['symptoms'] = Dataset.from_pandas(symptoms_data) # PubMedQA Dataset pubmed_qa = load_dataset('pubmed_qa', 'pqa_labeled') datasets['pubmed_qa'] = pubmed_qa['train'] # MedQA Dataset (USMLE) medqa_us = load_dataset('medqa_usmle') datasets['medqa_usmle'] = medqa_us['train'] # MIMIC-CXR Dataset (Chest X-ray Reports) mimic_cxr = load_dataset('mimic_cxr', split='train') datasets['mimic_cxr'] = mimic_cxr # BioASQ Dataset (Question Answering) bioasq = load_dataset('bioasq', 'bioasq_factoid') datasets['bioasq'] = bioasq['train'] # CheXpert Dataset (Chest X-ray Classification) chexpert = load_dataset('chexpert', split='train') datasets['chexpert'] = chexpert # ClinicalBERT Dataset (Clinical Text Classification) clinicalbert = load_dataset('clinicalbert', split='train') datasets['clinicalbert'] = clinicalbert # MultiMedQA Dataset (Multimodal Question Answering) multimedqa = load_dataset('multimedqa', split='train') datasets['multimedqa'] = multimedqa # COVID-19 Open Research Dataset Challenge (CORD-19) cord19 = load_dataset('cord19', split='train') datasets['cord19'] = cord19 # Additional datasets can be added here # ... return datasetsLoad datasets
datasets = load_datasets()
Preprocess datasets for chain-of-thought reasoning
def preprocess_for_cot(datasets): cot_data = []
# Preprocess Disease Symptoms dataset for CoT if 'symptoms' in datasets: df = datasets['symptoms'].to_pandas().dropna() for _, row in df.iterrows(): symptoms = ', '.join([str(row[f'Symptom_{i}']) for i in range(1, 18) if pd.notnull(row.get(f'Symptom_{i}', None))]) diagnosis = row['Disease'] question = f"Patient presents with symptoms: {symptoms}. What is the most probable diagnosis?" reasoning = f"The symptoms presented are {symptoms}, which are commonly associated with {diagnosis}." answer = f"{reasoning} Therefore, the most probable diagnosis is {diagnosis}." cot_data.append({'question': question, 'answer': answer}) # Preprocess PubMedQA dataset for CoT if 'pubmed_qa' in datasets: df = datasets['pubmed_qa'].to_pandas() for _, row in df.iterrows(): question_text = row['question'] context = row['context'] long_answer = row['final_answer'] reasoning = f"From the context: {context}" answer = f"{reasoning} Therefore, the answer is: {long_answer}" cot_data.append({'question': question_text, 'answer': answer}) # Preprocess MedNLI dataset for CoT if 'mednli' in datasets: df = datasets['mednli'].to_pandas() for _, row in df.iterrows(): premise = row['premise'] hypothesis = row['hypothesis'] label = row['label'] question = f"Given the medical note: '{premise}' Can we infer that: '{hypothesis}'?" reasoning = f"Based on the note, '{premise}', it is {label} that '{hypothesis}'." answer = f"{reasoning} Therefore, the answer is {label}." cot_data.append({'question': question, 'answer': answer}) # Preprocess MedQA USMLE dataset for CoT if 'medqa_usmle' in datasets: df = datasets['medqa_usmle'].to_pandas() for _, row in df.iterrows(): question_text = row['question'] answer_text = row['answer'] options = row['options'] explanation = row.get('explanation', '') reasoning = f"Question: {question_text}\nOptions: {options}\nBased on medical knowledge, the correct answer is {answer_text}." if explanation: reasoning += f" Explanation: {explanation}" answer = f"{reasoning}" cot_data.append({'question': question, 'answer': answer}) # Preprocess MIMIC-CXR dataset for CoT if 'mimic_cxr' in datasets: df = datasets['mimic_cxr'].to_pandas() for _, row in df.iterrows(): report = row['report'] findings = row['findings'] impression = row['impression'] question = f"Based on this chest X-ray report: {report}, what are the findings and impression?" reasoning = f"The report states: {findings} and the impression is: {impression}" answer = f"{reasoning} Therefore, the findings are {findings} and the impression is {impression}." cot_data.append({'question': question, 'answer': answer}) # Preprocess BioASQ dataset for CoT if 'bioasq' in datasets: df = datasets['bioasq'].to_pandas() for _, row in df.iterrows(): question_text = row['question'] ideal_answer = row['ideal_answer'] question = f"Answer the following question about biomedical research: {question_text}" reasoning = f"Based on scientific literature, the answer to the question is: {ideal_answer}" answer = f"{reasoning} Therefore, the answer is {ideal_answer}." cot_data.append({'question': question, 'answer': answer}) # Preprocess CheXpert dataset for CoT if 'chexpert' in datasets: df = datasets['chexpert'].to_pandas() for _, row in df.iterrows(): labels = row['labels'] image_path = row['path'] question = f"What are the findings in this chest X-ray image: {image_path}?" reasoning = f"The image shows the following findings: {labels}" answer = f"{reasoning} Therefore, the findings are {labels}." cot_data.append({'question': question, 'answer': answer}) # Preprocess ClinicalBERT dataset for CoT if 'clinicalbert' in datasets: df = datasets['clinicalbert'].to_pandas() for _, row in df.iterrows(): text = row['text'] label = row['label'] question = f"What is the clinical classification of the following text: {text}?" reasoning = f"Based on the text, the clinical classification is {label}" answer = f"{reasoning} Therefore, the classification is {label}." cot_data.append({'question': question, 'answer': answer}) # Preprocess MultiMedQA dataset for CoT if 'multimedqa' in datasets: df = datasets['multimedqa'].to_pandas() for _, row in df.iterrows(): question_text = row['question'] answer_text = row['answer'] image_path = row['image_path'] question = f"Answer this question based on the provided image: {image_path} - {question_text}" reasoning = f"From the image and question, the answer is: {answer_text}" answer = f"{reasoning} Therefore, the answer is {answer_text}." cot_data.append({'question': question, 'answer': answer}) # Preprocess CORD-19 dataset for CoT if 'cord19' in datasets: df = datasets['cord19'].to_pandas() for _, row in df.iterrows(): text = row['text'] abstract = row['abstract'] question = f"Based on this scientific article: {abstract}, {text}, what are the key findings?" reasoning = f"The article discusses the following key findings: {abstract}, {text}" answer = f"{reasoning} Therefore, the key findings are {abstract}, {text}." cot_data.append({'question': question, 'answer': answer}) # Additional datasets can be processed similarly # ... return pd.DataFrame(cot_data) - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.