flutter_python_ml

pub package License: MIT Flutter Platforms

A high-performance, on-device Machine Learning & Direct Python Bridge for Flutter. Execute Python files (.py) and run ML inference locally on Android, iOS, Desktop, and Web without external web APIs, cloud servers, or network latency.


๐Ÿ“ฑ Supported Platforms

flutter_python_ml is engineered for cross-platform versatility, providing on-device execution across all major platforms:

Platform Support Runtime Driver Minimum Version
๐Ÿค– Android โœ… Supported Native Platform Channel & Embedded Dart Engine Android 5.0 (API 21+)
๐ŸŽ iOS โœ… Supported Native Platform Channel & Embedded Dart Engine iOS 12.0+
๐Ÿ’ป macOS โœ… Supported Local Process IPC (python3) & Embedded Dart Engine macOS 10.14+
๐ŸชŸ Windows โœ… Supported Local Process IPC (python) & Embedded Dart Engine Windows 10+
๐Ÿง Linux โœ… Supported Local Process IPC (python3) & Embedded Dart Engine Any modern distro
๐ŸŒ Web โœ… Supported Embedded Dart Python Interpreter All modern browsers

๐ŸŒŸ Key Features

  • ๐Ÿ Direct Python File Execution: Load and run Python scripts (.py) directly from Flutter assets or local app storage.
  • โšก Zero-Network On-Device Inference: 100% offline and local. Zero HTTP endpoints, zero cloud costs, and zero network data leaks.
  • ๐Ÿง  Dedicated ML Model Lifecycle (PythonMLModel):
    • High-level abstraction for Scikit-Learn, PyTorch, NumPy, XGBoost, and custom Python algorithms.
    • Lifecycle management: initialize(), predict(), predictBatch().
    • Built-in latency profiler (latencyMs, inferenceDuration).
  • ๐Ÿ“ Multi-Dimensional Tensor Container (TensorData):
    • Reshaping, flattening, softmax, argmax, normalization, and one-hot encoding.
    • Native conversions to and from Dart lists, Float32List, and 2D matrices.
  • ๐Ÿ“บ Interactive Developer UI Widgets:
    • PythonConsoleView: Real-time terminal/console widget displaying Python stdout, stderr, and tracebacks.
    • ModelInferenceCard: Ready-to-use visual card displaying model status, input controls, probability bars, and inference latency.
    • TensorVisualizer: Visualizes 1D/2D tensors and matrices as numeric grids.
  • ๐Ÿงช Zero-Setup Mock Engine (MockPythonEngine): Bundled test models (Iris classification, real estate regression, NLP sentiment) for instant prototyping and automated CI testing with zero dependencies.

๐Ÿš€ Getting Started

1. Add dependency

Add flutter_python_ml to your pubspec.yaml:

dependencies:
  flutter_python_ml: ^0.1.0

2. Write your Python Model (iris_classifier.py)

Create your Python script in your app assets or storage (e.g. assets/models/iris_classifier.py):

# assets/models/iris_classifier.py

def predict(features):
    # features: [sepal_length, sepal_width, petal_length, petal_width]
    petal_length = float(features[2])
    petal_width = float(features[3])

    if petal_length < 2.45:
        label = "setosa"
        probs = [0.985, 0.012, 0.003]
    elif petal_width < 1.75:
        label = "versicolor"
        probs = [0.025, 0.920, 0.055]
    else:
        label = "virginica"
        probs = [0.010, 0.075, 0.915]

    return {
        "label": label,
        "confidence": max(probs),
        "probabilities": probs,
        "classes": ["setosa", "versicolor", "virginica"],
    }

3. Load and Run in Flutter

import 'package:flutter/material.dart';
import 'package:flutter_python_ml/flutter_python_ml.dart';

void runInference() async {
  // 1. Initialize Model
  final model = PythonMLModel(
    scriptPath: 'assets/models/iris_classifier.py',
    predictFunction: 'predict',
    labels: const ['setosa', 'versicolor', 'virginica'],
  );

  await model.initialize();

  // 2. Run local prediction with TensorData
  final input = TensorData.fromVector([5.1, 3.5, 1.4, 0.2]);
  final MLPrediction prediction = await model.predict(input);

  print('Predicted Class: ${prediction.predictedLabel}'); // setosa
  print('Confidence: ${prediction.confidence}');          // 0.985
  print('Inference Latency: ${prediction.latencyMs} ms'); // e.g. 1.25 ms
}

๐Ÿ› ๏ธ Direct Python Script & Function Calling

You can also run arbitrary Python code or invoke functions without creating a model wrapper:

Run Raw Python Code

final result = await PythonML.run('''
import math
radius = 5.0
area = math.pi * (radius ** 2)
print(f"Computed circle area: {area}")
''');

print(result.stdout);
print('Execution time: ${result.duration.inMilliseconds}ms');

Call Specific Functions

final result = await PythonML.call(
  'assets/scripts/data_cleaner.py',
  'clean_and_scale',
  keywordArgs: {
    'records': [10.5, 20.0, 30.5],
    'scale_factor': 2.0,
  },
);

print(result.result);

๐Ÿ“ Tensor Operations with TensorData

// 1D Vector
final vector = TensorData.fromVector([1.0, 5.0, 3.0, 2.0]);
print(vector.argmax()); // Index of maximum value -> 1

// Softmax probabilities
final probs = vector.softmax();

// 2D Matrix
final matrix = TensorData.fromMatrix2D([
  [1.0, 2.0],
  [3.0, 4.0],
]);
final flattened = matrix.flatten();

// One-Hot Encoding
final oneHot = TensorData.oneHot(2, 4); // [0.0, 0.0, 1.0, 0.0]

๐Ÿ“บ Pre-Built UI Widgets

1. PythonConsoleView

Embed a live terminal showing standard output and errors in real-time:

const PythonConsoleView(
  title: 'Python ML Diagnostics Terminal',
  height: 220,
)

2. ModelInferenceCard

A complete interactive card widget with input controls, predict button, confidence indicators, and latency badges:

ModelInferenceCard(
  model: myModel,
  title: 'Iris Species Classifier',
  description: 'On-device Python classification',
  onPredictPressed: () => myModel.predict([5.1, 3.5, 1.4, 0.2]),
)

๐Ÿงช Testing

The library includes 100% test coverage using the bundled MockPythonEngine:

flutter test

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

Libraries

flutter_python_ml
Production-ready on-device Machine Learning and direct Python execution bridge for Flutter without external APIs or cloud network dependencies.
flutter_python_ml_method_channel
flutter_python_ml_platform_interface