AI in Healthcare
Clinical NLP
import openai
from typing import List, Dict
class ClinicalNLP:
def __init__(self, api_key: str):
self.client = openai.OpenAI(api_key=api_key)
def extract_medical_entities(self, clinical_note: str) -> Dict:
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """Extract medical entities from clinical notes.
Return JSON with: diagnoses, medications, procedures, vitals, lab_results."""},
{"role": "user", "content": clinical_note}
],
temperature=0,
response_format={"type": "json_object"}
)
import json
return json.loads(response.choices[0].message.content)
def summarize_clinical_note(self, note: str) -> str:
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """Summarize clinical notes in structured format:
- Chief Complaint
- History of Present Illness
- Assessment
- Plan"""},
{"role": "user", "content": note}
],
temperature=0.1
)
return response.choices[0].message.content
def suggest_diagnoses(self, symptoms: List[str]) -> List[Dict]:
symptoms_text = ", ".join(symptoms)
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """Suggest possible diagnoses based on symptoms.
Return JSON array with diagnosis, probability, and reasoning."""},
{"role": "user", "content": f"Symptoms: {symptoms_text}"}
],
temperature=0.3,
response_format={"type": "json_object"}
)
import json
return json.loads(response.choices[0].message.content)
nlp = ClinicalNLP(api_key="your-api-key")
entities = nlp.extract_medical_entities(clinical_note)
summary = nlp.summarize_clinical_note(clinical_note)
Medical Image Analysis
import torch
import torch.nn as nn
from torchvision import models
class MedicalImageClassifier:
def __init__(self, n_classes=5):
self.model = models.resnet50(pretrained=True)
self.model.fc = nn.Linear(self.model.fc.in_features, n_classes)
def preprocess(self, image):
from torchvision import transforms
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
return transform(image).unsqueeze(0)
def predict(self, image_tensor):
self.model.eval()
with torch.no_grad():
outputs = self.model(image_tensor)
probabilities = torch.softmax(outputs, dim=1)
prediction = torch.argmax(probabilities, dim=1)
return {
"class": prediction.item(),
"confidence": probabilities.max().item(),
"probabilities": probabilities[0].tolist()
}
classifier = MedicalImageClassifier(n_classes=5)
result = classifier.predict(preprocessed_image)
Drug Discovery
class MoleculeGenerator:
def __init__(self, api_key: str):
self.client = openai.OpenAI(api_key=api_key)
def generate_molecules(self, target: str, properties: Dict) -> List[str]:
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """Generate novel molecule SMILES strings
with specified properties for drug discovery."""},
{"role": "user", "content": f"Target: {target}\nProperties: {properties}"}
],
temperature=0.8
)
return response.choices[0].message.content.split("\n")
def predict_properties(self, smiles: str) -> Dict:
response = self.client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """Predict molecular properties from SMILES.
Return JSON with solubility, toxicity, binding_affinity."""},
{"role": "user", "content": f"SMILES: {smiles}"}
],
temperature=0,
response_format={"type": "json_object"}
)
import json
return json.loads(response.choices[0].message.content)
generator = MoleculeGenerator(api_key="your-api-key")
molecules = generator.generate_molecules(
target="kinase inhibitor",
properties={"logp": 2.5, "mw": 450}
)
Best Practices
- Ensure HIPAA compliance for all patient data
- Validate AI outputs with clinical experts
- Implement audit trails for medical decisions
- Use federated learning for privacy preservation
- Maintain transparency in AI recommendations
- Follow FDA guidelines for medical AI