Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Policy Detection Android App

An Android application for policy management and violation detection using on-device machine learning models.

Features

Policy Screen

  • Display current policy text in a scrollable text view
  • Upload new policy files (.txt format only)
  • Save policy changes to device storage
  • File size validation (max 50KB)

Detection Screen

  • 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

Technical Implementation

Architecture

  • 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

SmolLM-135M Integration

The app is specifically designed to work with SmolLM-135M-Instruct ONNX model:

  1. SmolLM ONNX Runtime: Primary detection using text generation model
  2. Fallback Detection: Rule-based analysis when model unavailable

Setting up SmolLM Model:

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

File Validation

  • Policy files: Must be .txt format, max 50KB
  • Detection files: Supports .txt, .img, .jpg, .jpeg, .png
  • Security: File type validation prevents malicious uploads

Setup Instructions

Prerequisites

  • Android Studio Arctic Fox or later
  • Android SDK API 24+ (Android 7.0)
  • Kotlin 1.9.10+

Building the App

  1. Clone this repository
  2. Open in Android Studio
  3. Sync Gradle dependencies
  4. Build and run on device or emulator

Dependencies

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'

Usage

Setting Up a Policy

  1. Navigate to the "Policy" tab
  2. Tap "Upload New Policy" and select a .txt file
  3. Review the policy content in the text view
  4. Tap "Save" to store the policy locally

Running Detection

  1. Navigate to the "Detection" tab
  2. Tap "Upload File" and select a file to analyze
  3. Tap "Run Policy Detection" to analyze the file
  4. Review results in the output text area

Detection Results

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

File Structure

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

Security Considerations

  • 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

SmolLM Technical Implementation

How Policy Detection Works:

  1. Prompt Construction: Creates ChatML-formatted prompt with policy and content
  2. Tokenization: Converts text to token IDs using simplified vocabulary
  3. Model Inference: Runs SmolLM-135M to generate analysis response
  4. Response Parsing: Extracts YES/NO decision and confidence from generated text
  5. Fallback Logic: Uses keyword matching if model unavailable

Prompt Template:

<|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

Performance Optimizations:

  • Limited to 256 input tokens for mobile efficiency
  • 2-thread inference for balanced performance
  • Simplified vocabulary for faster tokenization
  • Greedy decoding for deterministic results

Future Enhancements

  • Proper tokenizer integration (GPT2TokenizerFast)
  • Support for additional SmolLM variants
  • Dynamic context length adjustment
  • Detection history and reporting
  • Policy version management
  • Export detection results

License

This project is for educational and defensive security purposes only.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages