Step 2: Download the onnx model: model_q4f16.onnx from here: https://huggingface.co/HuggingFaceTB/SmolLM2-135M-Instruct/tree/main/onnx
This guide explains the steps to set up a project with a basic views activity and integrate ONNX for machine learning inference.
- Task: Initialize a new project.
- Action: Create a basic activity to serve as the main UI.
- Task: Add a new library module.
- Action: Name the module
mylibrary
to allow for modularization and reusability.
-
File:
AndroidManifest.xml
(in the app module). -
Action: Add the following permission for audio recording:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
-
File:
build.gradle
(app module). -
Task: Integrate the ONNX Runtime library to enable inference using ONNX models.
-
Action: Add the following dependency to the
dependencies
block:implementation("com.microsoft.onnxruntime:onnxruntime-android:latest.release")
- Source: Download the
model.onnx
file from HuggingFace. - Task: Place the model file in the
assets
folder of the app module. - Action:
- Locate or create the
assets
directory undersrc/main
. - Copy the
model.onnx
file into this directory.
- Locate or create the
- Location:
mylibrary
module. - Task: Implement a utility class for handling ONNX model operations.
- Action:
- Create a new Java/Kotlin class named
OnnxReader
. - Write methods to:
- Load the
model.onnx
file. - Execute inference using the ONNX Runtime library.
- Load the
- Create a new Java/Kotlin class named
- Location:
MyActivity
in the app module. - Task: Add logic for performing inference using the ONNX model.
- Action:
- Use the
OnnxReader
class to load the model. - Process input data through the model.
- Display or utilize the output as needed.
- Use the
- Location:
mylibrary
module. - Task: Create a tokenizer class that processes input text into
input_ids
andattention_mask
.