An open API service indexing awesome lists of open source software.

https://github.com/boltuix/tflite-text-classifier-android-app

This Android app leverages a TensorFlow Lite model for on-device classification of social media posts into 11 categories, including technology, sports, finance, and more. Built with Kotlin and Jetpack Compose, it ensures efficient, privacy-focused inference without server dependencies.
https://github.com/boltuix/tflite-text-classifier-android-app

ai android appdev colab-notebook jetpack-compose jetpackcompose kotlin machine-learning mobile-app nlp on-device tensorflow tensorflow-lite text-classification tflite tflite-models tips-and-tricks

Last synced: 5 months ago
JSON representation

This Android app leverages a TensorFlow Lite model for on-device classification of social media posts into 11 categories, including technology, sports, finance, and more. Built with Kotlin and Jetpack Compose, it ensures efficient, privacy-focused inference without server dependencies.

Awesome Lists containing this project

README

          

# TFLite Text Classifier Android App
![How to Use TensorFlow Lite for Text Classification in Jetpack Compose](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxW1gOAZgRe8G3nD_Pw4quU5b4AK3ensLRIEWL04WwJHst17xzAKKNZBDCiMFpU3MG5imsmGQ8CnOJJzMWbQwvwWXopMjW-F13HjwsciXKsP-5XQe3S-iQW3LzqQLzpPrGg04J46rAgqChnsJeC0LF0fAvLpOGgv4IftvutkYbbfMGqNU7HYyFbPbMSyI/s16000/How%20to%20Use%20TensorFlow%20Lite%20for%20Text%20Classification%20in%20Jetpack%20Compose.png)

------------------------------------------------------------------------

This Android app leverages a TensorFlow Lite model for on-device
classification of social media posts into 10+ categories, including
technology, sports, finance, and more. Built with Kotlin and Jetpack
Compose, it ensures efficient, privacy-focused inference without server
dependencies.

[![License:
MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

[![TensorFlow
Lite](https://img.shields.io/badge/TensorFlow-Lite-blue.svg)](https://www.tensorflow.org/lite)

[![Kotlin](https://img.shields.io/badge/Kotlin-1.9-blueviolet.svg)](https://kotlinlang.org/)

[![Jetpack
Compose](https://img.shields.io/badge/Jetpack-Compose-green.svg)](https://developer.android.com/jetpack/compose)

[![Android](https://img.shields.io/badge/Android-14+-brightgreen.svg)](https://developer.android.com/)

------------------------------------------------------------------------

## Features

- **On-device classification** using TensorFlow Lite
- **Jetpack Compose UI** for modern Android apps
- **Multi-line input** for flexible text entry
- **Probability distribution** for each category
- **Fast & lightweight inference** with no server calls
- **Manual tokenizer** with `vocab.txt` for reproducibility

------------------------------------------------------------------------

## Training Notebook -- `model.ipynb`

The repository includes a Jupyter notebook **`Android.ipynb`**,
containing the full **training pipeline** for the classifier:

- Load & preprocess training data
- Tokenization with `TextVectorization`
- Build & train a Keras model
- Convert the model to `.tflite` format
- Export `vocab.txt` and `labels.txt` for Android usage

Use this notebook to retrain or fine-tune your model while ensuring
**consistency** between training and deployment.

------------------------------------------------------------------------

## Model Overview

- **Model file**: `model_with_softmax.tflite`
- **Input**: `int32[1, 50]` → tokenized & padded sequence
- **Output**: `float32[1, 10]` → probability vector (softmax)
- **Tokenizer**: `vocab.txt` from training (`TextVectorization`)
- **Labels**: `labels.txt` with 11 categories

------------------------------------------------------------------------

## Folder Structure

app/
├── assets/
│ ├── model_with_softmax.tflite
│ ├── vocab.txt
│ └── labels.txt
└── res/
└── layout/ (if legacy XML UI is present)

------------------------------------------------------------------------

## Setup Instructions

1. Add dependencies in `build.gradle`:

``` gradle
implementation 'org.tensorflow:tensorflow-lite:2.12.0'
implementation 'org.tensorflow:tensorflow-lite-support:0.3.1'
```

2. Copy required files to `app/src/main/assets/`:
- `model_with_softmax.tflite`
- `vocab.txt`
- `labels.txt`
3. Initialize the classifier in Kotlin:

``` kotlin
TextClassifier.initialize(context)
```

------------------------------------------------------------------------

## Example Usage (Jetpack Compose)

``` kotlin
val text by remember { mutableStateOf("") }

Column {
TextField(
value = text,
onValueChange = { text = it },
placeholder = { Text("Enter your post...") },
modifier = Modifier
.fillMaxWidth()
.height(150.dp),
maxLines = 5
)

Button(onClick = {
val result = TextClassifier.predictAll(text)
val formatted = result.joinToString("\n") { (label, prob) ->
"$label: ${"%.2f".format(prob * 100)}%"
}
Log.d("Prediction", formatted)
}) {
Text("Classify")
}
}
```

------------------------------------------------------------------------

## Notes

- The `.tflite` model **must include softmax**
- `vocab.txt` and `labels.txt` must match training data
- Assets must be placed in `app/src/main/assets/` (not `res/`)
- Use `coerceIn()` to ensure predictions remain between `0–1`

------------------------------------------------------------------------

## License

MIT --- Free to use, modify, and distribute.