Integrate random forest model from python pickle library into Android Studio

149 Views Asked by At

I have implemented Android wearable application and i want to import python ML function using chaquopy(Android) and random_forest_model(Python). I checked my code was getting variables and methods from python, when it comes to ML library like random_forest_model.pkl or knn_model.pkl i was getting error from chaquopy library like Caused by: com.chaquo.python.PyException: ModuleNotFoundError: No module named 'sklearn.metrics._dist_metrics'.

build.gradle

    plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-android'
    id 'com.google.gms.google-services'
    id 'dagger.hilt.android.plugin'
    id 'kotlin-kapt'
    id 'com.chaquo.python'
}
apply plugin: 'kotlin-android'

android {
    compileSdk 32

    dexOptions {
        javaMaxHeapSize "4g"
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    defaultConfig {
        configurations.all {
            resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' }
        }
        applicationId "xxx.xxxxx.xxxxxxxxx"
        minSdk 30
        targetSdk 32
        versionCode 13
        versionName "1.0.13"
        multiDexEnabled true
        sourceSets {
            main {
                python.srcDir "src/main/python"
                java.srcDirs = ['src/main/java', 'src/main/python']
                resources.srcDirs = ['src/main/resources']
            }
        }
        ndk {
            abiFilters "armeabi-v7a", "x86", "x86_64"
        }
        python {
            version "3.8"
        }
        python {
            buildPython "C:/Users/xxx/AppData/Local/Programs/Python/Python38/python.exe"
        }
        python {
            pip {
                install "Flask"
                install "pandas"
                install "sklearn_porter"
                install "pickle-mixin"
                install "scikit-learn"
                install "Keras==2.3.1"
                install "tensorflow"
                install "numpy"
            }
        }
        signingConfig signingConfigs.debug
    }

MainActivity.kt

if (! Python.isStarted()) {
        Python.start(AndroidPlatform(this));
    }

    val py = Python.getInstance()
    val module = py.getModule("api")

api.py

import pandas as pd
import os
from flask import Flask, request, jsonify
import pickle
app = Flask(__name__)

model_path = os.path.join(os.path.dirname(__file__), "knn_model.pkl")

with open(model_path, 'rb') as model_file:
    model = pickle.load(model_file)
0

There are 0 best solutions below