An Android application for policy management and violation detection using on-device machine learning models.
- Display current policy text in a scrollable text view
- Upload new policy files (
.txtformat only) - Save policy changes to device storage
- File size validation (max 50KB)
- Upload files for analysis (
.txt,.img,.jpg,.jpeg,.png) - Run policy detection using ONNX Runtime or fallback rule-based detection
- Display input file information and detection results
- Real-time analysis feedback
- Navigation: Tab-based navigation using ViewPager2 and TabLayout
- File Handling: Android Storage Access Framework for file selection
- ML Integration: ONNX Runtime for on-device inference
- Fallback Detection: Rule-based keyword matching when ML model unavailable
The app is specifically designed to work with SmolLM-135M-Instruct ONNX model:
- SmolLM ONNX Runtime: Primary detection using text generation model
- Fallback Detection: Rule-based analysis when model unavailable
Step 1: Export the model using optimum-cli
pip install optimum[onnxruntime]
optimum-cli export onnx --model HuggingFaceTB/SmolLM-135M-Instruct --task text-generation onnx_smollm_135m/Step 2: Copy model to device
Place the exported model.onnx file in one of these locations on your Android device:
/data/data/com.example.policydetection/files/onnx_smollm_135m/model.onnx/storage/emulated/0/Android/data/com.example.policydetection/files/onnx_smollm_135m/model.onnx
Step 3: Model features
- Text generation-based policy analysis
- ChatML prompt format support
- Mobile-optimized inference (256 token limit)
- Intelligent response parsing for violation detection
- Policy files: Must be
.txtformat, max 50KB - Detection files: Supports
.txt,.img,.jpg,.jpeg,.png - Security: File type validation prevents malicious uploads
- Android Studio Arctic Fox or later
- Android SDK API 24+ (Android 7.0)
- Kotlin 1.9.10+
- Clone this repository
- Open in Android Studio
- Sync Gradle dependencies
- Build and run on device or emulator
implementation 'androidx.navigation:navigation-fragment-ktx:2.7.6'
implementation 'androidx.navigation:navigation-ui-ktx:2.7.6'
implementation 'com.microsoft.onnxruntime:onnxruntime-android:1.16.3'
implementation 'androidx.activity:activity-ktx:1.8.2'- Navigate to the "Policy" tab
- Tap "Upload New Policy" and select a
.txtfile - Review the policy content in the text view
- Tap "Save" to store the policy locally
- Navigate to the "Detection" tab
- Tap "Upload File" and select a file to analyze
- Tap "Run Policy Detection" to analyze the file
- Review results in the output text area
The app provides detailed analysis including:
- Violation detection (YES/NO)
- Confidence score (0.0 - 1.0)
- Detection method used (ONNX/Rule-based)
- Flagged keywords or content
- Analysis summary and recommendations
app/src/main/java/com/example/policydetection/
├── MainActivity.kt # Main activity with tab navigation
├── PolicyFragment.kt # Policy management screen
├── DetectionFragment.kt # File upload and detection screen
└── PolicyDetector.kt # ML model integration and detection logic
- File type validation prevents execution of malicious files
- Local storage ensures policy data remains on device
- ONNX Runtime provides secure on-device inference
- No network connections required for operation
- Prompt Construction: Creates ChatML-formatted prompt with policy and content
- Tokenization: Converts text to token IDs using simplified vocabulary
- Model Inference: Runs SmolLM-135M to generate analysis response
- Response Parsing: Extracts YES/NO decision and confidence from generated text
- Fallback Logic: Uses keyword matching if model unavailable
<|im_start|>system
You are a policy compliance analyzer...
<|im_end|>
<|im_start|>user
Analyze this content for policy violations: [CONTENT]
<|im_end|>
<|im_start|>assistant
- Limited to 256 input tokens for mobile efficiency
- 2-thread inference for balanced performance
- Simplified vocabulary for faster tokenization
- Greedy decoding for deterministic results
- Proper tokenizer integration (GPT2TokenizerFast)
- Support for additional SmolLM variants
- Dynamic context length adjustment
- Detection history and reporting
- Policy version management
- Export detection results
This project is for educational and defensive security purposes only.