Skip to content
Open

Dev #3498

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

981 changes: 981 additions & 0 deletions .idea/caches/deviceStreaming.xml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/whisper.cpp-master.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added BUILD_STATUS.md
Empty file.
96 changes: 96 additions & 0 deletions IMPLEMENTATION_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Intent Classifier Tokenizer Upgrade - Implementation Summary

## Problem Identified
Your Android intent classifier was using a basic word-level tokenizer that was **reducing model accuracy** because:
- It performed simple word splitting instead of BERT's WordPiece tokenization
- The tokenization didn't match how your all-MiniLM-L6-v2 model was trained
- Out-of-vocabulary words were poorly handled

## Solution Implemented

### 1. Rust-based HuggingFace Tokenizer
- **Location**: `rs-hf-tokenizer/`
- **Technology**: Rust + HuggingFace tokenizers library + JNI
- **Purpose**: Provides exact BERT WordPiece tokenization identical to model training

### 2. Updated Android Integration
- **Modified**: `IntentClassifier.kt` - Replaced basic tokenization with HF tokenizer
- **Added**: `HFTokenizer.kt` - Kotlin wrapper for Rust tokenizer
- **Approach**: Uses existing `tokenizer.json` file from your assets

### 3. TensorFlow Lite Compatibility
- Adapted the ONNX-based solution to work with your TensorFlow Lite model
- Maintains the same input format (input_ids + attention_mask)
- No changes needed to your TFLite model or inference code

## Files Created/Modified

### New Files:
```
rs-hf-tokenizer/
├── Cargo.toml # Rust project configuration
├── src/lib.rs # Rust tokenizer implementation
├── .cargo/config.toml # Android NDK build configuration
├── build_android.bat # Build script for Windows
└── README.md # Detailed setup instructions

whisper.cpp-master/examples/whisper.android/app/src/main/java/com/whispercppdemo/intent/
└── HFTokenizer.kt # Kotlin wrapper for Rust tokenizer
```

### Modified Files:
```
whisper.cpp-master/examples/whisper.android/app/src/main/java/com/whispercppdemo/intent/
└── IntentClassifier.kt # Updated to use HF tokenizer
```

## Key Changes Made

### IntentClassifier.kt Changes:
1. **Removed**: Basic vocabulary loading and special token management
2. **Added**: HuggingFace tokenizer initialization from `tokenizer.json`
3. **Replaced**: `tokenizeText()` method with proper BERT tokenization
4. **Updated**: Resource cleanup to include tokenizer disposal

### Architecture Comparison:

**Before:**
```
Text → Basic Word Split → Vocab Lookup → TFLite Model → Intent
```

**After:**
```
Text → HF BERT Tokenizer (Rust) → TFLite Model → Intent
```

## Expected Improvements

1. **Higher Accuracy**: Proper subword tokenization matches model training
2. **Better OOV Handling**: WordPiece handles unknown words gracefully
3. **Consistent Results**: Identical tokenization to model training pipeline
4. **Professional Quality**: Industry-standard tokenization approach

## Next Steps

1. **Build the Rust Library**:
```powershell
cd rs-hf-tokenizer
.\build_android.bat
```

2. **Copy Native Libraries**: Move `.so` files to Android jniLibs directories

3. **Test**: Compare classification accuracy on previously problematic texts

4. **Deploy**: The improved tokenizer should significantly boost your model's performance

## Technical Notes

- The solution uses the exact same `tokenizer.json` file you already have
- No changes needed to your TensorFlow Lite model
- The Rust implementation is memory-efficient and fast
- JNI bridge provides seamless Kotlin integration
- Builds for all Android architectures (arm64, armv7, x86, x86_64)

This implementation follows the proven approach from the Sentence-Embeddings-Android repository but adapts it specifically for your TensorFlow Lite intent classification use case.
133 changes: 133 additions & 0 deletions README_INTENT_CLASSIFIER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Whisper.cpp with Fine-tuned BERT Intent Classifier

This repository contains a modified version of [whisper.cpp](https://github.com/ggerganov/whisper.cpp) with an integrated fine-tuned BERT intent classifier for Android applications.

## 🚀 Features

- **Complete End-to-End Intent Classification**: Single TensorFlow Lite model (no separate encoder needed)
- **Fine-tuned BERT Model**: Custom trained on TensorFlow 2.19.0 with dynamic int8 quantization
- **Android Ready**: Optimized for mobile deployment with TFLite
- **13 Intent Classes**: Supports LogEvent, MediaAction, OpenApp, PhoneAction, QueryPoint, QueryTrend, SetGoal, SetThreshold, StartActivity, StopActivity, TimerStopwatch, ToggleFeature, WeatherQuery

## 📱 Android Integration

### Model Files
- `intent_classifier.tflite` - Complete end-to-end intent classifier (int8 quantized)
- `model_metadata.json` - Intent mappings and model configuration
- `tokenizer/` - BERT tokenizer files (vocab.txt, tokenizer.json, etc.)

### Architecture
```
Text Input → BERT Tokenizer → Complete TFLite Model → Intent Classification
```

### Key Features
- **Input**: Raw text strings
- **Tokenization**: BERT-style with [CLS], [SEP], [PAD], [UNK] tokens
- **Max Sequence Length**: 256 tokens
- **Model Type**: Complete end-to-end (includes both encoder and classification head)
- **Quantization**: Dynamic int8 for optimal mobile performance

## 🔧 Implementation Details

### Model Inputs
- **Input 0**: `input_ids` - Tokenized text as int32 array [1, 256]
- **Input 1**: `attention_mask` - Attention mask as int32 array [1, 256]

### Model Output
- **Output**: Intent logits as float32 array [1, 13]

### IntentClassifier.kt
The Android implementation handles:
- BERT tokenization with proper special tokens
- TensorFlow Lite inference with multiple inputs
- Softmax application for probability scores
- Intent mapping to human-readable labels

## 📊 Performance

- **Model Size**: Optimized with int8 quantization
- **Inference Speed**: Single model call (faster than two-stage approaches)
- **Memory Footprint**: Reduced compared to separate encoder + classifier
- **Accuracy**: Maintains high accuracy with quantization

## 🛠️ Usage

### Android Integration
1. Copy model files to `/assets/` directory
2. Use `IntentClassifier.kt` for inference
3. Initialize with `initialize()` method
4. Classify with `classifyIntent(text: String)`

### Example
```kotlin
val classifier = IntentClassifier(context)
classifier.initialize()

val result = classifier.classifyIntent("Set my daily step goal to 10000")
// result.intent = "SetGoal"
// result.confidence = 0.95
```

## 📝 Supported Intent Classes

1. **LogEvent** - Logging activities/events
2. **MediaAction** - Media control (volume, play, pause)
3. **OpenApp** - Opening applications
4. **PhoneAction** - Phone-related actions (calls, etc.)
5. **QueryPoint** - Point-in-time queries (current status)
6. **QueryTrend** - Trend/historical queries
7. **SetGoal** - Setting goals/targets
8. **SetThreshold** - Setting thresholds/limits
9. **StartActivity** - Starting activities/workouts
10. **StopActivity** - Stopping activities
11. **TimerStopwatch** - Timer/stopwatch operations
12. **ToggleFeature** - Enabling/disabling features
13. **WeatherQuery** - Weather-related queries

## 🎯 Model Training

- **Base Model**: BERT (fine-tuned)
- **Framework**: TensorFlow 2.19.0
- **Quantization**: Dynamic int8 for mobile optimization
- **Training**: Custom dataset with 13 intent classes
- **Tokenizer**: BERT tokenizer with 30,522 vocabulary size

## 📄 Files Changed

### Key Android Files
- `examples/whisper.android/app/src/main/java/com/whispercppdemo/intent/IntentClassifier.kt`
- `examples/whisper.android/app/src/main/assets/intent_classifier.tflite`
- `examples/whisper.android/app/src/main/assets/model_metadata.json`
- `examples/whisper.android/app/src/main/assets/tokenizer/`

### Backup Files
Original files are backed up in `/assets/backup/` for reference.

## 🔍 Testing

Test examples:
```
"decrease volume" → MediaAction
"what's my heart rate" → QueryPoint
"enable DND" → ToggleFeature
"set daily step goal to 10000" → SetGoal
```

## 🚀 Deployment

Ready for production Android deployment with:
- Optimized model size
- Fast inference
- Low memory usage
- High accuracy intent classification

## 📋 Requirements

- Android API Level 21+
- TensorFlow Lite for Android
- ~30MB additional storage for model files

---

*Based on whisper.cpp by Georgi Gerganov with custom intent classification integration.*
1 change: 1 addition & 0 deletions Sentence-Embeddings-Android
Submodule Sentence-Embeddings-Android added at 394517
2 changes: 1 addition & 1 deletion examples/whisper.android/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions examples/whisper.android/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions examples/whisper.android/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/whisper.android/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Loading