An advanced Augmentative and Alternative Communication (AAC) app for iPad that uses BERT (Bidirectional Encoder Representations from Transformers) to provide intelligent, context-aware word predictions for building sentences.
- BERT-base model (110M parameters) for contextual understanding
- 15 word suggestions ranked by probability
- Bidirectional attention - sees full sentence context
- On-device inference - no internet required, privacy-first
- Large, easy-to-tap buttons for accessibility
- Probability display (testing mode) shows model confidence
- Letter filtering to narrow down word choices
- Sentence display with delete and clear functions
- Responsive grid layout optimized for iPad
- Core ML integration for fast on-device inference (~50-100ms)
- WordPiece tokenization compatible with BERT
- Masked Language Modeling (MLM) approach
- Softmax probability calculation with numerical stability
- Smart word filtering removes punctuation and invalid tokens
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ SwiftUI Interface โ
โ (ContentView.swift) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Word Prediction Service โ
โ (WordPredictionService.swift) โ
โ โข Tokenization โ
โ โข Model inference โ
โ โข Probability calculation โ
โ โข Word filtering โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ BERT Tokenizer โ
โ (GPT2Tokenizer.swift โ BERTTokenizer) โ
โ โข WordPiece tokenization โ
โ โข Special token handling ([CLS], [MASK], [SEP]) โ
โ โข Attention mask generation โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Core ML Model โ
โ (WordPredictor.mlpackage) โ
โ โข BERT-base-uncased โ
โ โข Input: [batch_size=1, seq_len=128] โ
โ โข Output: [batch_size=1, seq_len=128, vocab_size=30522] โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- macOS with Xcode 15.0+
- iPad running iOS 15.0 or later
- Apple ID (free account works)
- Python 3.9+ (for model conversion only)
git clone https://github.com/stevendisano/bert-word-prediction.git
cd bert-word-predictionpip install transformers coremltools torchThe model weights are not included in the repo due to GitHub's 100MB file size limit.
You must generate the Core ML model before running the app:
cd Predict
python3 convert_model.pyThis will:
- Download
bert-base-uncasedfrom Hugging Face (~440MB) - Convert it to Core ML format (~420MB)
- Save to
Predict/WordPredictor.mlpackage/
First time setup takes ~2-5 minutes depending on your internet speed.
Follow the detailed instructions in DEPLOYMENT.md
Quick steps:
- Open
Predict/Predict.xcodeprojin Xcode - Connect your iPad via USB
- Select your iPad as the target device
- Configure code signing with your Apple ID
- Click Run (
โถ๏ธ )
Ipad/
โโโ Predict/
โ โโโ Predict.xcodeproj/ # Xcode project file
โ โโโ Predict/
โ โ โโโ PredictApp.swift # App entry point
โ โ โโโ ContentView.swift # Main UI (SwiftUI)
โ โ โโโ WordPredictionService.swift # Prediction logic
โ โ โโโ GPT2Tokenizer.swift # BERT tokenizer (renamed)
โ โ โโโ WordPredictor.mlpackage/ # Core ML BERT model
โ โ โโโ vocab.json # BERT vocabulary (30,522 tokens)
โ โ โโโ merges.txt # Placeholder for BERT
โ โโโ convert_model.py # Python script to convert BERT
โ โโโ MODEL_SETUP.md # Model conversion guide
โโโ DEPLOYMENT.md # iPad deployment instructions
โโโ README.md # This file
โโโ .gitignore # Git ignore rules
User input "I asked" is converted to token IDs:
[CLS] "i" "asked" [MASK] "and" [SEP] [PAD] [PAD] ...
[101] [1045] [2356] [103] [1998] [102] [0] [0] ...
- Model processes the entire sequence with bidirectional attention
- Generates logits for each position:
[1, 128, 30522] - Extracts logits at the
[MASK]position (position 3)
- Apply softmax to convert logits โ probabilities
- Sort by probability (highest first)
- Filter out punctuation and invalid tokens
- Top 15 words shown with probabilities
- Example for "I asked":
- Him (5.6%)
- Her (2.6%)
- Quietly (0.9%)
Adding "and" after [MASK] prevents punctuation predictions:
- Without:
[CLS] "i" "asked" [MASK] [SEP]โ predicts.(81%) - With:
[CLS] "i" "asked" [MASK] "and" [SEP]โ predictshim(5.6%)
| Property | Value |
|---|---|
| Model | BERT-base-uncased |
| Parameters | 110 million |
| Vocabulary Size | 30,522 tokens |
| Max Sequence Length | 128 tokens |
| Tokenization | WordPiece |
| Framework | Core ML (converted from PyTorch) |
| Inference Time | ~50-100ms on iPad |
| Model Size | ~420MB |
- Sentence display area at top
- 15 word prediction buttons (5x3 grid)
- Letter filter buttons at bottom
- Delete and Clear All controls
- Probability display: Shows model confidence (testing mode)
- Letter filtering: Tap a letter to filter words
- Contextual predictions: Suggestions change based on sentence
python3 test_bert_prediction.pyThis shows raw BERT output with probabilities for test sentences.
xcrun simctl spawn booted log stream --predicate 'processImagePath contains "Predict"' --level debugEdit WordPredictionService.swift (lines 87-89):
let words = [
"I", "The", "It", "You", "We", "What", "This", "Can",
"How", "There", "When", "My", "If", "He", "She"
]Comment out in ContentView.swift (lines 86-90):
// if prediction.probability > 0 {
// Text(String(format: "%.1f%%", prediction.probability * 100))
// .font(.system(size: 12, weight: .medium))
// .foregroundColor(.white.opacity(0.8))
// }Change in WordPredictionService.swift (line 177):
let result = Array(wordPredictions.prefix(15)) // Change 15 to desired number- Ensure
WordPredictor.mlpackageexists inPredict/Predict/ - Run
python3 convert_model.pyto regenerate the model
- Add your Apple ID in Xcode โ Settings โ Accounts
- Enable "Automatically manage signing" in project settings
- Connect iPad via USB
- Unlock iPad and trust the computer
- Check: Window โ Devices and Simulators in Xcode
- First prediction is slower (~200ms) due to model loading
- Subsequent predictions are fast (~50-100ms)
- Performance depends on iPad model (newer = faster)
- Vocabulary Coverage: BERT-base has 30,522 tokens, may not include rare/slang words
- Sentence Length: Limited to 128 tokens (truncated if longer)
- Single Word Prediction: Currently predicts one word at a time
- Model Size: 420MB - requires significant storage
- First Launch: Takes a few seconds to load model
- Multi-word phrase predictions
- User vocabulary customization
- Prediction history and learning
- Voice output (text-to-speech)
- Smaller model option (DistilBERT)
- Fine-tuning on AAC-specific corpus
- Dark mode support
- Accessibility improvements (VoiceOver)
BERT (Bidirectional):
- โ Sees context before AND after the mask
- โ Better for filling in blanks (MLM task)
- โ More natural for single-word prediction
GPT-2 (Causal/Left-to-right):
- โ Only sees context before the current position
- โ Designed for continuous text generation
- โ Tends to predict low-probability tokens
let maxLogit = logits.max() ?? 0.0
let expValues = logits.map { exp($0 - maxLogit) }
let sumExp = expValues.reduce(0, +)
let probability = expValue / sumExpSubtracting maxLogit prevents overflow in exp() calculation.
- BERT Paper - Original BERT research
- Hugging Face BERT - Pre-trained model
- Core ML Documentation - Apple's ML framework
- AAC Research - Communication aids
Contributions are welcome! Areas of interest:
- Model optimization (quantization, pruning)
- UI/UX improvements for accessibility
- Additional language support
- Performance benchmarking
- Bug fixes and testing
This project is licensed under the Apache License 2.0.
The BERT model (bert-base-uncased) is licensed under Apache 2.0 by Google and Hugging Face.
Steven DiSano
- GitHub: @stevendisano
- Google Research - BERT model
- Hugging Face - Pre-trained models and transformers library
- Apple - Core ML framework
- AAC Community - Inspiration and feedback
For questions or issues:
- Check DEPLOYMENT.md for setup help
- Review Troubleshooting section
- Open an issue on GitHub
Built with โค๏ธ for augmentative and alternative communication