Spaces:
Sleeping
π₯ AI SOAP Note Generator for Google Colab
Transform unstructured medical notes into professional SOAP documentation using Google's Gemma 3N model - Optimized for Google Colab
π Overview
The AI SOAP Note Generator is an intelligent medical documentation tool that converts informal doctor's notes, patient encounters, and clinical observations into structured SOAP (Subjective, Objective, Assessment, Plan) format. This tool leverages Google's Gemma 3N language model and runs seamlessly in Google Colab with GPU acceleration.
β¨ Features
- π Google Colab Ready: No local setup required - runs entirely in the cloud
- β‘ GPU Acceleration: Leverages Colab's free GPU/TPU for fast processing
- π§ Gemma 3N Integration: Uses Google's latest medical-aware language model
- π± Multiple Interfaces:
- Interactive Jupyter widgets
- Modern Gradio web interface
- Direct function calls
- π File Support: Upload .txt files directly in Colab
- π― Pre-loaded Examples: Built-in medical scenarios for immediate testing
- π Shareable Links: Generate public links to share your interface
π Quick Start in Google Colab
1. Open the Notebook
Click the "Open in Colab" badge above or create a new notebook in Google Colab
2. Set Runtime to GPU (Recommended)
Runtime β Change runtime type β Hardware accelerator β GPU
3. Install Dependencies
Run this cell first:
# Install required packages
!pip install -q gradio torch transformers accelerate bitsandbytes
!pip install -q ipywidgets
# Import libraries
import gradio as gr
import torch
from transformers import pipeline
import ipywidgets as widgets
from IPython.display import display, HTML
4. Run All Cells
Execute the notebook cells in order to:
- Load the Gemma 3N model
- Set up the interface
- Start generating SOAP notes
5. Use the Interface
- Gradio Interface: Click the public URL to access the web interface
- Colab Widgets: Use the interactive widgets directly in the notebook
π± Interface Options
Option 1: Gradio Web Interface (Recommended)
# Launches a web interface with public sharing
gradio_interface.launch(share=True)
Benefits:
- Modern, responsive design
- Public shareable links
- Mobile-friendly
- Copy-to-clipboard functionality
Option 2: Jupyter Widgets
# Interactive widgets within the notebook
display(main_interface)
Benefits:
- Runs directly in Colab
- No external links needed
- Integrated with notebook workflow
π§ Colab-Specific Setup
GPU Configuration
# Check GPU availability
import torch
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"GPU device: {torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'None'}")
# Configure for Colab GPU
device = "cuda" if torch.cuda.is_available() else "cpu"
model_config = {
"device_map": "auto",
"torch_dtype": torch.float16 if device == "cuda" else torch.float32
}
File Upload in Colab
# Method 1: Direct file upload
from google.colab import files
uploaded = files.upload()
# Method 2: Google Drive integration
from google.colab import drive
drive.mount('/content/drive')
Save Results to Drive
# Save generated SOAP notes to Google Drive
def save_to_drive(soap_note, filename):
with open(f'/content/drive/MyDrive/{filename}', 'w') as f:
f.write(soap_note)
print(f"β
Saved to Google Drive: {filename}")
π Usage Examples
Example 1: Quick Test
# Test with example data
test_note = """
Patient: 45yo male with chest pain x2 hours
Sharp substernal pain 7/10, radiates to left arm
SOB, diaphoresis, no nausea
Vitals: BP 150/90, HR 110, O2 96%
Anxious, diaphoretic appearance
"""
soap_result = generate_soap_note(test_note)
print(soap_result)
Example 2: File Processing
# Upload and process medical files
from google.colab import files
uploaded_files = files.upload()
for filename, content in uploaded_files.items():
medical_text = content.decode('utf-8')
soap_note = generate_soap_note(medical_text)
# Save result
output_filename = f"SOAP_{filename}"
with open(output_filename, 'w') as f:
f.write(soap_note)
# Download result
files.download(output_filename)
π― Pre-loaded Medical Examples
The notebook includes three clinical scenarios:
- Chest Pain Case: Acute coronary syndrome workup
- Diabetes Case: New onset diabetes mellitus
- Pediatric Case: Streptococcal pharyngitis
Click any example button to load and test immediately.
π Model Information
Gemma 3N Configuration
model_name = "google/gemma-3n-7b" # Adjust based on availability
tokenizer_config = {
"max_length": 2048,
"temperature": 0.7,
"do_sample": True
}
Memory Optimization for Colab
# For Colab's memory constraints
torch.cuda.empty_cache()
model = model.half() # Use 16-bit precision
β οΈ Colab-Specific Considerations
Runtime Limitations
- 12-hour session limit: Save work frequently
- GPU quota: Free tier has daily limits
- Memory constraints: ~12-15GB RAM available
Best Practices
- Save frequently: Download important results
- Use GPU wisely: Enable only when needed
- Monitor resources: Check RAM/GPU usage
- Backup notebooks: Save to Drive regularly
π οΈ Troubleshooting in Colab
Common Issues
"Runtime disconnected"
# Prevent disconnection
import time
while True:
time.sleep(60) # Keep session alive
"Out of GPU memory"
# Clear GPU memory
torch.cuda.empty_cache()
# Restart runtime if needed: Runtime β Restart runtime
"Package not found"
# Reinstall packages
!pip install --upgrade gradio transformers torch
Gradio interface not loading
# Try without share link
gradio_interface.launch(share=False, debug=True)
π Performance Tips
Optimize for Colab
# Batch processing for multiple notes
def batch_process_notes(note_list):
results = []
for i, note in enumerate(note_list):
print(f"Processing {i+1}/{len(note_list)}")
soap_note = generate_soap_note(note)
results.append(soap_note)
return results
Monitor Resources
# Check memory usage
!nvidia-smi
!cat /proc/meminfo | grep MemAvailable
π Sharing Your Work
Share Notebook
- File β Save a copy in Drive
- Share β Get shareable link
- Set permissions to "Anyone with the link"
Share Interface
# Gradio creates public URLs automatically
gradio_interface.launch(share=True)
# Copy the public URL to share with others
π Colab Notebook Structure
π SOAP_Note_Generator.ipynb
βββ π§ Setup & Installation
βββ π§ Model Loading
βββ π SOAP Generation Function
βββ π¨ Interface Creation
β βββ Gradio Web Interface
β βββ Jupyter Widgets
βββ π Example Cases
βββ π Launch Interface
βββ πΎ Save/Export Functions
π Medical Disclaimer
βοΈ IMPORTANT: This tool is for educational and research purposes only
- Not intended for actual clinical use
- Always consult qualified healthcare professionals
- Remove patient identifiers before processing
- Comply with HIPAA and privacy regulations
π Getting Help
In Colab:
- Use
!pip list
to check installed packages - Check GPU with
!nvidia-smi
- Restart runtime if needed:
Runtime β Restart runtime
Common Commands:
# Debug mode
import logging
logging.basicConfig(level=logging.DEBUG)
# Check versions
print(f"Torch: {torch.__version__}")
print(f"Transformers: {transformers.__version__}")
print(f"Gradio: {gr.__version__}")
π Advanced Features
Google Drive Integration
# Mount Google Drive
from google.colab import drive
drive.mount('/content/drive')
# Save notebooks and results automatically
import shutil
shutil.copy('generated_soap_notes.txt', '/content/drive/MyDrive/')
Scheduled Processing
# Process notes at scheduled intervals
import schedule
import time
def scheduled_processing():
# Your processing logic here
pass
schedule.every(30).minutes.do(scheduled_processing)
π¬ Ready to start generating professional SOAP notes in Google Colab!
Click "Open in Colab" above and run all cells to get started immediately.