Skip to content

juv85/Gemma3n-react-native

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About PurrPal AI - Gemma 3na Integration

⚠️ Important Disclaimer

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.

Project Overview

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.

What This Project Provides

🚀 Complete Implementation

  • 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

🛠️ Developer Resources

  • 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

Target Audience

Primary Audience

  • 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

Secondary Audience

  • 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

Key Benefits

For Developers

  • 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

For Users

  • 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

Technical Highlights

Architecture

  • 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

Features

  • 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

Use Cases

Educational

  • 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

Production

  • 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

Research

  • 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

Model Setup

Download Gemma 3na Model

  1. Get the Model from Kaggle

    • Visit [Kaggle Gemma 3na Models or Hugging Face ]
    • Download the gemma3na 4B or 2B model (recommended for mobile)
    • Extract the .task file from the download
  2. Model Placement

    • iOS: Add the gemma.task file to your Xcode project bundle
    • Android: Place the file in the device's Documents/gemma/ folder

Performance varies based on model size, prompt complexity, and device thermal state.

Bridge Configuration & Usage

Basic Integration

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!");

Configuration Options

// 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)
});

Model Management

// 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();

Performance Monitoring

// 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");
}

React Hooks Usage

// Model management hook
const { isLoaded, isLoading, loadModel, unloadModel, deviceCapabilities } =
  useGemmaModel();

// Metrics monitoring hook
const {
  realtimeMetrics,
  performanceStats,
  isPerformanceDegrading,
  clearMetrics,
} = useGemmaMetrics();

Error Handling

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

File Management

// 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);

Architecture Overview

Bridge Components

  1. ModelManager (lib/managers/ModelManager.ts)

    • Handles model loading/unloading
    • Platform-specific optimizations
    • Retry logic and error recovery
  2. MetricsManager (lib/managers/MetricsManager.ts)

    • Real-time performance tracking
    • Performance degradation detection
    • Metrics export functionality
  3. FileManager (lib/managers/FileManager.ts)

    • Model file management
    • Download/upload operations
    • Storage permission handling
  4. GemmaBridge (lib/GemmaBridge.ts)

    • Main interface for AI operations
    • Unified API across platforms
    • Enhanced error handling

Native Implementation

  • iOS: Swift implementation with MediaPipe integration
  • Android: Kotlin implementation with TensorFlow Lite
  • GPU Support: Automatic detection and fallback to CPU

Getting Started

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.


Final Notes

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

About

Native Gemma 3na model integration for React Native With Bridge using MediaPipe - iOS CPU optimized, Android CPU/GPU delegates, complete offline inference

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 72.3%
  • Kotlin 14.5%
  • Swift 8.8%
  • Ruby 2.9%
  • Objective-C 1.2%
  • JavaScript 0.3%