This project is a reference implementation and experimental prototype. It is provided "as-is" for educational and research purposes only. Please note:
- 🔬 Not Production Ready: This code may contain bugs, incomplete features, and potential security issues
- 📚 Reference Only: Use this project as a learning resource and starting point for your own implementation
- 🛠️ Development Required: Significant testing, optimization, and customization will be needed for production use
- 🔍 No Warranties: No guarantees are provided regarding functionality, performance, or reliability
- 🚀 Build Your Own: This is intended to guide you in building your own robust AI integration
We strongly recommend thoroughly testing and adapting this code before using it in any production environment.
PurrPal AI is a comprehensive React Native Expo application that demonstrates how to integrate Google's Gemma 3na language models (2B/4B) for on-device AI inference. This project serves as both a functional AI chat application and a complete reference implementation for developers who want to add Gemma 3na AI capabilities to their own React Native projects.
- Native Bridge: Full iOS (Swift) and Android (Kotlin) bridge implementation for Gemma 3na model integration
- React Native Integration: TypeScript hooks and managers for seamless AI functionality
- Performance Monitoring: Real-time metrics and performance tracking
- Chat Interface: Modern, responsive UI with conversation management
- Bridge Architecture: Modular design with separate managers for models, metrics, files, and permissions
- Error Handling: Comprehensive error management and user feedback
- Configuration: Flexible model configuration and device capability detection
- Cross-Platform: Unified API that works consistently across iOS and Android
- React Native Developers looking to integrate on-device AI into their applications
- Mobile AI Enthusiasts interested in running language models locally on devices
- Educators and Students learning about mobile AI implementation and React Native development
- Researchers exploring on-device AI performance and capabilities
- Product Managers evaluating feasibility of local AI features
- Open Source Contributors interested in improving mobile AI tooling
- Ready-to-Use Code: Complete implementation that can be adapted for various use cases
- Best Practices: Demonstrates proper architecture for AI-enabled mobile apps
- Performance Insights: Built-in metrics help optimize AI integration
- Documentation: Clear code structure and inline documentation
- Privacy-First: All AI processing happens on-device, no data sent to servers
- Offline Capability: Works without internet connection once model is loaded
- Fast Response: Optimized for mobile performance with real-time metrics
- Native Experience: Smooth, responsive interface that feels like a native app
- Modular Design: Separate managers for different concerns (model, metrics, files)
- TypeScript: Full type safety across the entire application
- Expo Integration: Leverages Expo's development tools while maintaining native capability
- Performance Optimization: Efficient model loading and inference management
- Real-time Metrics: Token/second rates, inference timing, memory usage
- Model Management: Loading, unloading, and configuration of Gemma 3na models
- Device Detection: Automatic capability detection (GPU support, memory, etc.)
- Error Recovery: Robust error handling with user-friendly messages
- Learning Resource: Study how to implement AI in mobile apps
- Research Tool: Experiment with on-device AI performance
- Teaching Aid: Demonstrate AI concepts in mobile development courses
- Starter Template: Base for building AI-powered mobile applications
- Feature Integration: Add AI chat capabilities to existing apps
- Proof of Concept: Validate AI integration before full development
- Performance Benchmarking: Test Gemma 3na model performance across devices
- User Experience Studies: Evaluate on-device AI interaction patterns
- Technical Evaluation: Assess feasibility of mobile AI implementations
-
Get the Model from Kaggle
- Visit [Kaggle Gemma 3na Models or Hugging Face ]
- Download the
gemma3na 4B or 2Bmodel (recommended for mobile) - Extract the
.taskfile from the download
-
Model Placement
- iOS: Add the
gemma.taskfile to your Xcode project bundle - Android: Place the file in the device's
Documents/gemma/folder
- iOS: Add the
Performance varies based on model size, prompt complexity, and device thermal state.
import SimplifiedGemmaBridge from "./lib/GemmaBridge";
import { useGemmaModel } from "./lib/hooks";
// Using the hook (recommended)
const { loadModel, isLoaded, status } = useGemmaModel();
// Direct bridge usage
const response = await SimplifiedGemmaBridge.generateResponse("Hello, AI!");// Configure the bridge
await SimplifiedGemmaBridge.configure({
useGPU: true, // Enable GPU acceleration (auto-detected)
autoLoad: false, // Automatically load model on app start
retryAttempts: 3, // Number of retry attempts for model loading
retryDelay: 2000, // Delay between retries (ms)
});// Load model with custom configuration
const success = await SimplifiedGemmaBridge.loadModel("gemma.task");
// Check model status
const status = await SimplifiedGemmaBridge.getModelStatus();
console.log(status.isLoaded, status.backend, status.loadTime);
// Get device capabilities
const capabilities = await SimplifiedGemmaBridge.getDeviceCapabilities();
console.log(capabilities.supportsGPU, capabilities.totalMemoryMB);
// Unload model to free memory
await SimplifiedGemmaBridge.unloadModel();// Generate response with metrics
const result = await SimplifiedGemmaBridge.generateResponseWithMetrics(prompt);
console.log(result.metrics.tokensPerSecond);
console.log(result.metrics.inferenceTimeMs);
// Get real-time performance data
const metrics = await SimplifiedGemmaBridge.getRealtimeMetrics();
console.log(metrics.currentTokensPerSecond);
console.log(metrics.averageTokensPerSecond);
// Check for performance degradation
const isDegrading = SimplifiedGemmaBridge.isPerformanceDegrading();
if (isDegrading) {
console.log("Consider unloading/reloading model");
}// Model management hook
const { isLoaded, isLoading, loadModel, unloadModel, deviceCapabilities } =
useGemmaModel();
// Metrics monitoring hook
const {
realtimeMetrics,
performanceStats,
isPerformanceDegrading,
clearMetrics,
} = useGemmaMetrics();try {
const success = await SimplifiedGemmaBridge.loadModel();
if (!success) {
throw new Error("Model loading failed");
}
} catch (error) {
console.error("Bridge error:", error.message);
// Handle specific error cases:
// - "Storage permission required" (Android)
// - "Model file not found"
// - "Insufficient memory"
// - "GPU not supported"
}// List available models
const models = await SimplifiedGemmaBridge.listAvailableModels();
// Download model from URL
const modelPath = await SimplifiedGemmaBridge.downloadModel(
"https://example.com/gemma.task",
"gemma.task"
);
// Delete model
await SimplifiedGemmaBridge.deleteModel("old-model.task");
// Get storage information
const storage = await SimplifiedGemmaBridge.getStorageInfo();
console.log(storage.availableSpaceBytes, storage.modelCount);-
ModelManager (
lib/managers/ModelManager.ts)- Handles model loading/unloading
- Platform-specific optimizations
- Retry logic and error recovery
-
MetricsManager (
lib/managers/MetricsManager.ts)- Real-time performance tracking
- Performance degradation detection
- Metrics export functionality
-
FileManager (
lib/managers/FileManager.ts)- Model file management
- Download/upload operations
- Storage permission handling
-
GemmaBridge (
lib/GemmaBridge.ts)- Main interface for AI operations
- Unified API across platforms
- Enhanced error handling
- iOS: Swift implementation with MediaPipe integration
- Android: Kotlin implementation with TensorFlow Lite
- GPU Support: Automatic detection and fallback to CPU
This project is designed to be immediately usable while also serving as a learning resource. Whether you're building your first AI-enabled mobile app or looking to understand the technical details of on-device inference, PurrPal AI provides a solid foundation for your journey.
Check out the main README for setup instructions and start exploring the power of on-device AI in React Native applications.
Remember: This is an experimental reference implementation. Use it to learn, experiment, and build your own production-ready AI integration. Always thoroughly test any code before deploying to production environments.
Happy coding! 🚀