A production-ready language-detection REST API. scikit-learn Naive Bayes + FastAPI + Docker, deployed on Render and Azure.
🚀 Quick Start · 📡 API · 📊 Model Card · 🐳 Docker · ☁️ Deploy · 🛠 Troubleshooting
{ "text": "Ciao, come stai?" }{ "language": "Italian", "label": "Italian", "index": 8, "confidence": 1.0 }📑 Table of Contents
A complete, deployable language-detection service — not just a notebook.
- Model —
CountVectorizer(char_wb 2-4)→MultinomialNB, trained on 10,337 labelled samples across 17 languages, reaching 98.98% accuracy. - API — FastAPI with automatic OpenAPI docs, request validation and typed responses.
- Artifact — a single self-describing
.joblibbundle (pipeline + labels + metrics) — no hard-coded label lists to drift out of sync. - Container — a slim, non-root, health-checked Docker image built on
python:3.12-slim. - Deploy — one-click on Render (blueprint included) or Azure App Service (GitHub Actions workflow included).
- CI — lint, format check, unit tests and a live container smoke test on every push.
| Feature | Detail | |
|---|---|---|
| 🎯 | 98.98% accuracy | Character n-grams beat word unigrams — see Model Card |
| 🧠 | Self-describing artifact | Pipeline, class labels and training metadata bundled in one .joblib |
| 🌐 | FastAPI + OpenAPI | Interactive Swagger UI at /docs, ReDoc at /redoc |
| ⚡ | Batch endpoint | Classify up to 100 texts in a single round-trip |
| 📊 | Confidence scores | Every prediction returns the max class probability |
| 🐳 | Modern container | python:3.12-slim, non-root user, HEALTHCHECK, layered cache |
| 🔁 | Reproducible training | python model/prebuild/train.py rebuilds the artifact from the dataset |
| ✅ | 23 tests + CI | Pytest suite, Ruff lint, container smoke test on every push |
| 🔒 | Safe by default | .dockerignore keeps secrets and VCS data out of the image |
flowchart LR
C["📱 Client<br/>curl · Swagger UI"] --> A["🌐 FastAPI<br/>/predict · /predict/batch"]
A --> P["🧹 clean_text()<br/>strip punctuation · lowercase"]
P --> V["🔤 CountVectorizer<br/>char_wb 2-4 grams"]
V --> N["🧠 MultinomialNB<br/>17 classes"]
N --> O["✅ language · label · index · confidence"]
N -.-> F[("📦 language_detector-0.2.0.joblib")]
Request path
main.pyvalidates the body with Pydantic (text, 1–10,000 chars).model/preprocessing.pystrips punctuation/digits/brackets and lowercases — the exact same function used at training time.model/model.pyloads the artifact once at import, then runspipeline.predict().- The response carries the detected language, the raw dataset label, the class index and a confidence score.
| Component | Choice |
|---|---|
| Vectorizer | CountVectorizer(analyzer="char_wb", ngram_range=(2, 4)) |
| Classifier | MultinomialNB |
| Preprocessing | strip !@#$(),\n"%^*?:;~\0-9[]` → lowercase |
| Split | 80 / 20, stratified, random_state=42 |
| Artifact | model/language_detector-0.2.0.joblib (1.70 MB, compressed) |
| scikit-learn | 1.7.2 |
| Source | Kaggle — Language Detection |
| Rows | 10,337 |
| Classes | 17 |
| Split | 8,269 train / 2,068 test |
| Imbalance | English 1,385 → Hindi 63 samples per class |
| Metric | Score |
|---|---|
| Accuracy | 0.9898 |
| F1 (weighted) | 0.9898 |
| F1 (macro) | 0.9916 |
Per-class breakdown
| Language | Precision | Recall | F1 | Support |
|---|---|---|---|---|
| Arabic | 1.00 | 1.00 | 1.00 | 107 |
| Danish | 1.00 | 0.94 | 0.97 | 86 |
| Dutch | 0.98 | 0.96 | 0.97 | 109 |
| English | 0.96 | 1.00 | 0.98 | 277 |
| French | 0.99 | 0.99 | 0.99 | 203 |
| German | 1.00 | 0.98 | 0.99 | 94 |
| Greek | 1.00 | 1.00 | 1.00 | 73 |
| Hindi | 1.00 | 1.00 | 1.00 | 12 |
| Italian | 0.99 | 1.00 | 1.00 | 140 |
| Kannada | 1.00 | 1.00 | 1.00 | 74 |
| Malayalam | 1.00 | 1.00 | 1.00 | 119 |
| Portuguese | 0.99 | 0.99 | 0.99 | 148 |
| Russian | 1.00 | 0.99 | 0.99 | 138 |
| Spanish | 0.99 | 0.99 | 0.99 | 164 |
| Swedish | 0.99 | 0.99 | 0.99 | 135 |
| Tamil | 1.00 | 1.00 | 1.00 | 94 |
| Turkish | 1.00 | 0.99 | 0.99 | 95 |
The original model used word unigrams (CountVectorizer()). That works fine for
space-delimited languages but fails on scripts that do not separate words with
spaces — a Hindi sentence collapses into a single token and the model falls back
to guessing:
| Input | v0.1.0 (word BoW) | v0.2.0 (char n-grams) |
|---|---|---|
"नमस्ते दोस्तों" |
❌ English | ✅ Hindi |
"मैं ठीक हूँ धन्यवाद" |
✅ Hindi | ✅ Hindi |
| Accuracy on the test split | 97.63% | 98.98% |
Switching to char_wb 2-4 grams gained +1.35 accuracy points and fixed the Indic-script failures.
-
17 languages only. Chinese, Japanese, Korean and Thai are not in the training set and will be misclassified.
-
Short strings are unreliable — always check
confidence. A single Russian word is not enough signal:{ "text": "Привет" } → { "language": "English", "label": "English", "index": 3, "confidence": 0.134 }The same word inside a sentence is detected correctly with near-certainty. Treat low-confidence results as "unknown", not as a wrong answer.
-
Hindi is under-represented (63 training samples vs 1,385 English), so its test score is based on just 12 samples.
-
Dataset label typos. The source dataset spells two labels
PortugeeseandSweedish. The API returns the correctedPortuguese/Swedish; the raw label is still available in thelabelfield. -
Domain shift. The model is trained on Wikipedia-style sentences. Slang, code-switching and marketing copy will score lower.
git clone https://github.com/Manohargov/Language-Detection-Docker_FastAPI_Render.git
cd Language-Detection-Docker_FastAPI_Render
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements-dev.txt
requirements.txtholds the runtime deps;requirements-dev.txtadds pytest + Ruff.
uvicorn main:app --reload --port 8000Open http://127.0.0.1:8000/docs for the interactive Swagger UI.
curl -X POST http://127.0.0.1:8000/predict \
-H "Content-Type: application/json" \
-d '{"text": "Ciao, come stai?"}'{"language":"Italian","label":"Italian","index":8,"confidence":1.0}| Method | Path | Purpose |
|---|---|---|
GET |
/ |
Health check, model version, supported languages, metrics |
GET |
/health |
Same as / (alias — used by Docker HEALTHCHECK and Render) |
POST |
/predict |
Detect the language of one text |
POST |
/predict/batch |
Detect the language of up to 100 texts |
GET |
/openapi.json |
OpenAPI schema |
GET |
/docs |
Swagger UI |
GET |
/redoc |
ReDoc |
Request
{ "text": "Bonjour tout le monde" }Response 200
{
"language": "French",
"label": "French",
"index": 4,
"confidence": 1.0
}| Field | Type | Meaning |
|---|---|---|
language |
string | Detected language, correctly spelled |
label |
string | Raw dataset label (differs only for Portuguese / Swedish) |
index |
int | Class index into the model's 17 classes |
confidence |
float | Max class probability, 0.0–1.0 |
Errors — 422 when text is missing, empty, whitespace-only or longer than 10,000 characters.
curl -X POST http://127.0.0.1:8000/predict/batch \
-H "Content-Type: application/json" \
-d '{"texts": ["Hello there", "Ciao come stai", "Bonjour tout le monde"]}'{
"predictions": [
{"language": "English", "label": "English", "index": 3, "confidence": 1.0},
{"language": "Italian", "label": "Italian", "index": 8, "confidence": 1.0},
{"language": "French", "label": "French", "index": 4, "confidence": 1.0}
]
}{
"health_check": "OK",
"model_version": "0.2.0",
"status": "ok",
"languages": ["Arabic", "Danish", "Dutch", "English", "French", "German", "Greek", "Hindi", "Italian", "Kannada", "Malayalam", "Portuguese", "Russian", "Spanish", "Swedish", "Tamil", "Turkish"],
"metrics": {
"accuracy": 0.9898,
"f1_weighted": 0.9898,
"f1_macro": 0.9916,
"train_samples": 8269,
"test_samples": 2068
},
"sklearn_version": "1.7.2"
}from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
print(client.post("/predict", json={"text": "Ciao, come stai?"}).json())
# {'language': 'Italian', 'label': 'Italian', 'index': 8, 'confidence': 1.0}Language-Detection-Docker_FastAPI_Render/
├── 🐍 main.py # FastAPI app — /, /health, /predict, /predict/batch
├── 📦 requirements.txt # Runtime dependencies (pinned)
├── 🧪 requirements-dev.txt # + pytest, httpx, Ruff
├── 📦 pyproject.toml # Project metadata, pytest & Ruff config
├── 🐳 Dockerfile # python:3.12-slim, non-root, HEALTHCHECK
├── 🐳 .dockerignore # Keeps the build context lean
├── 🐳 docker/entrypoint.sh # exec uvicorn (PID 1, honours $PORT)
├── ☁️ render.yaml # Render blueprint — one-click deploy
├── 🧪 tests/test_api.py # 23 pytest tests
├── ⚙️ .github/workflows/ci.yml # Lint + test + container smoke test
├── ⚙️ .github/workflows/azure-container-webapp.yml
├── 📄 LICENSE # MIT
├── 📖 README.md
└── model/
├── 🐍 __init__.py
├── 🐍 model.py # Artifact loading + inference
├── 🐍 preprocessing.py # Shared, versioned text cleaning
├── 📦 language_detector-0.2.0.joblib # The deployable model (1.70 MB)
└── prebuild/
├── 📓 LanguageDetection.ipynb # Original exploratory notebook
├── 🐍 train.py # Reproducible training script
└── 📄 Language Detection.csv # Kaggle dataset
The artifact is fully reproducible — no notebook hand-editing required.
# 1. Get the dataset
# https://www.kaggle.com/datasets/basilb2s/language-detection
# → place "Language Detection.csv" in model/prebuild/
# 2. Train (writes model/language_detector-0.2.0.joblib)
python model/prebuild/train.py==================================================================
MODEL CARD — language-detector 0.2.0
==================================================================
Dataset : Language Detection.csv (10337 rows)
Classes : 17
Vectorizer : char_wb [2, 4]
Algorithm : MultinomialNB
scikit-learn : 1.7.2
------------------------------------------------------------------
Accuracy : 0.9898
F1 (weighted) : 0.9898
F1 (macro) : 0.9916
Train / test : 8269 / 2068
------------------------------------------------------------------
Artifact : model/language_detector-0.2.0.joblib (1.70 MB)
==================================================================
Customise the run:
python model/prebuild/train.py \
--dataset "path/to/Language Detection.csv" \
--output "model/language_detector-0.2.0.joblib"
⚠️ Keepscikit-learnin sync. The bundle records the version it was trained with (metadata.sklearn_version). Loading a pickle across sklearn versions raisesInconsistentVersionWarningand can silently change predictions. If you retrain with a different sklearn version, updaterequirements.txtto match.
docker build -t language-detection-api .
docker run --rm -p 8000:8000 language-detection-api
# → http://127.0.0.1:8000/docs| Before | Now | |
|---|---|---|
| Base image | tiangolo/uvicorn-gunicorn-fastapi (deprecated) |
python:3.12-slim |
| Process manager | Gunicorn + Uvicorn | Uvicorn --workers (Gunicorn no longer needed) |
| User | root |
non-root appuser (uid 1000) |
| Health check | none | HEALTHCHECK hitting /health |
| Signals | shell wrapper | exec → uvicorn is PID 1, gets SIGTERM cleanly |
| Port | hard-coded 80 |
$PORT (Render sets 10000) |
| Context | dockerignore (ignored by Docker) |
.dockerignore — actually applied |
# Override the port / worker count
docker run --rm -p 8080:8080 \
-e PORT=8080 -e WEB_CONCURRENCY=4 \
language-detection-apiThe repo ships a render.yaml blueprint:
- Push the repo to GitHub (done ✅).
- Open https://render.com/deploy and pick this repository — Render detects the blueprint.
- Deploy. The service comes up at
https://<your-service>.onrender.com.
Health checks hit /health; the container honours Render's PORT automatically.
Manual Render setup
| Setting | Value |
|---|---|
| Environment | Docker |
| Dockerfile path | ./Dockerfile |
| Context | . |
| Health check path | /health |
| Port | 10000 (set via PORT env var) |
A ready-to-use workflow lives at
.github/workflows/azure-container-webapp.yml.
It builds the image, pushes it to GHCR and deploys it to an Azure Web App.
Setup
- Create an Azure Web App for Linux (custom container).
- In GitHub → Settings → Secrets, add
AZURE_WEBAPP_PUBLISH_PROFILE. - Set three app settings on the Web App:
DOCKER_REGISTRY_SERVER_URL→https://ghcr.ioDOCKER_REGISTRY_SERVER_USERNAME→ your GitHub usernameDOCKER_REGISTRY_SERVER_PASSWORD→ a PAT withrepo+read:packages
- Update
AZURE_WEBAPP_NAMEin the workflow to your Web App's name. - Push to
main— the pipeline runs automatically.
| Variable | Default | Purpose |
|---|---|---|
PORT |
8000 |
Port the API listens on (Render injects 10000) |
WEB_CONCURRENCY |
2 |
Number of uvicorn worker processes |
MODEL_ARTIFACT |
(built in) | Override the path to the .joblib bundle |
pytest -q # 23 tests
ruff check . # lint
ruff format --check .The CI workflow (.github/workflows/ci.yml) runs on every push and pull request:
ruff check+ruff format --checkpytestwith a JUnit report uploaded as an artifact- A real Docker build followed by a live smoke test — the container is started,
/healthis polled until it responds, and a real/predictrequest is fired.
| Symptom | Cause | Fix |
|---|---|---|
ModuleNotFoundError: No module named 'model' |
Running from the wrong directory | Start from the repository root — uvicorn main:app |
InconsistentVersionWarning on startup |
scikit-learn version ≠ training version | Install the pinned version, or retrain with your version and update requirements.txt |
FileNotFoundError: language_detector-0.2.0.joblib |
Artifact missing (git-lfs, partial clone) | Run python model/prebuild/train.py to regenerate |
| Container exits immediately | Wrong working directory or missing artifact | docker logs <container> — check COPY model/ ./model/ in the Dockerfile |
422 Unprocessable Entity |
Empty or over-long text |
Send 1–10,000 characters |
| Confidence is very low | Text too short, or a language outside the 17 | Check confidence; try a longer sample |
docker build context is huge |
.dockerignore missing or renamed |
Ensure the file is named .dockerignore (with the leading dot) |
| Port already in use locally | 8000 is taken |
uvicorn main:app --port 8001 |
- Model — switched to
char_wb2-4 n-grams: 97.63% → 98.98% accuracy, and fixed Hindi/Indic-script detection. - Artifact — one self-describing
joblibbundle (pipeline + labels + metrics) replaces the.pkl+ hard-coded class list. - API — richer response (
language,label,index,confidence), new/predict/batchendpoint, typed Pydantic v2 schemas, OpenAPI metadata. - Labels — dataset typos
Portugeese/Sweedishare now returned asPortuguese/Swedish. - Docker — deprecated
tiangolobase image replaced withpython:3.12-slim; non-root user,HEALTHCHECK,execentrypoint,$PORTsupport. - Repo hygiene —
.dockerignoreactually applied, expanded.gitignore,LICENSE,pyproject.toml,render.yaml. - Quality — 23 pytest tests, Ruff config, CI pipeline with a container smoke test, reproducible
train.py.
⚠️ Breaking change:POST /predictnow returns four fields instead of one. Clients reading["language"]are unaffected.
Naive Bayes + word BoW in a notebook, served by FastAPI and shipped in a tiangolo Docker image.
Contributions are welcome!
- Fork the repo and create a branch:
git checkout -b feature/your-idea - Keep
ruff check .andpytestgreen before opening a PR. - If you retrain the model, regenerate the artifact and update the metrics table in this README.
- Never commit secrets — there are none needed for this service.
Released under the MIT License.
- FastAPI — modern, typed, fast web framework
- scikit-learn —
CountVectorizer+MultinomialNB - Uvicorn — ASGI server
- Joblib — efficient model persistence
- Render — one-click deployment
- Microsoft Azure — container hosting
- Kaggle — Language Detection dataset by basilb2s
Made with ☕, scikit-learn and a lot of predict() calls
⭐ If this helped you, consider starring the repo!