Tan_lang_chatbot — FastAPI + Supabase + HTML/CSS/JS
Mtafsiri wa lugha za asili za Tanzania: Sukuma · Kihaya (Ruhaya) · Kinyakyusa · Kiswahili, kwa maandishi au sauti, ukiwa na hali maalum ya Daktari ↔ Mgonjwa.
Tan_lang_chatbot/
├── backend/
│ ├── main.py # FastAPI app (REST API + serves the frontend)
│ ├── load_to_supabase.py # Seeds the Supabase table from the CSVs
│ ├── supabase_schema.sql # Tables, grants, RLS policies, indexes
│ ├── requirements.txt
│ └── .env.example
├── model/
│ ├── __init__.py
│ └── translator.py # Translation model (detect, exact, fuzzy, composed)
├── data/
│ ├── locallang_dataset_part1.csv # dataset (part 1). Part 2 not included here
└── frontend/
├── index.html # Conversation Mode UI
├── styles.css # Clinical Teal theme
└── app.js # Speech-to-text, text-to-speech, API calls
1. Run it locally
cd LOCAL_LANGUAGE
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r backend/requirements.txt
cp backend/.env.example backend/.env # optional — CSV mode works with no config
uvicorn backend.main:app --reload --port 8000
Open http://localhost:8000. The backend serves frontend/index.html automatically,
so no second server is needed. Use Chrome or Edge — speech recognition needs the
Web Speech API (Firefox does not support it; typing still works everywhere).
Install as a package
The published web application installs its backend dependencies automatically:
python -m pip install --upgrade Tan_lang_chatbot
tan-lang-chatbot
Then open http://127.0.0.1:8000. You do not need to install
backend/requirements.txt separately.
2. Optional: use Supabase instead of the CSVs
- Create a project on supabase.com.
- SQL Editor → paste and run
backend/supabase_schema.sql. - In
.envset:USE_SUPABASE=true SUPABASE_URL=https://xxxx.supabase.co SUPABASE_SERVICE_KEY=your-service-role-key - Seed the data (one-time, ~56k rows):
python backend/load_to_supabase.py - Restart uvicorn.
GET /api/healthwill report"source": "supabase".
2b. Optional: enable online AI fallback
If a phrase is missing from the local CSV dataset, you can enable an external AI translation API as a fallback. This is useful for phrases that are not yet in the local lexicon, while still keeping the local dataset as the first source of truth.
Set these in backend/.env:
AI_TRANSLATION_ENABLED=true
AI_API_URL=https://api.openai.com/v1
AI_API_KEY=your-api-key
AI_MODEL=gpt-4o-mini
AI_TEMPERATURE=0.1
This works with OpenAI-compatible endpoints too, including Azure OpenAI, DeepSeek, and OpenRouter. The app will only call the online API when the local dataset does not return a reliable translation.
3. API
| Method | Path | Body / Query | Purpose |
|---|---|---|---|
| GET | /api/health |
— | Status, row count, data source |
| GET | /api/languages |
— | Supported languages |
| POST | /api/translate |
{text, source_language, target_language} |
Main translation |
| POST | /api/detect |
{text} |
Language detection only |
| POST | /api/compare |
{text} |
Same word across all languages |
| GET | /api/search |
?q=maji&limit=20 |
Dictionary lookup |
| POST | /api/reload |
— | Reload dataset without restarting |
source_language may be auto, Kiswahili, Sukuma, Ruhaya (= Kihaya) or Kinyakyusa.
Example:
curl -X POST http://localhost:8000/api/translate \
-H "Content-Type: application/json" \
-d '{"text":"amezi","source_language":"auto","target_language":"Kiswahili"}'
Response:
{
"source_language": "Ruhaya",
"target_language": "Kiswahili",
"input": "amezi",
"translation": "Maji",
"category": "Health",
"confidence": "high",
"found_in_dataset": true,
"match_type": "exact"
}
4. How the engine works
- Normalize — lowercase, strip punctuation and accents.
- Detect — token scoring against each language's vocabulary.
- Exact match in the dataset →
confidence: high. - Fuzzy match (trigram similarity ≥ 0.72) →
confidence: medium, plus a “did you mean …” suggestion. - Composed — for sentences, translates word by word →
confidence: low, marked for verification by a native speaker. - No hallucination — if nothing reliable is found the API says so instead of inventing a word.
5. Clinical conversation mode
- Choose the patient's language (Kihaya / Sukuma / Kinyakyusa) at the top.
- The patient's microphone translates into Kiswahili for the doctor.
- The doctor's microphone translates back into the patient's language.
- Each turn appears as a bubble with the original, the translation, a confidence badge, category and an example sentence when available.
- The voice toggle reads every translation aloud (
speechSynthesis). - The site language (Kiswahili / English) is switchable at top-right and remembered
in
localStorage.
6. Deploying
- Backend: any Python host (Render, Railway, Fly.io, a VPS with
uvicornbehind nginx). - Frontend: served by FastAPI by default; to host it separately set
window.LOCALLANG_API = "https://your-api-host"beforeapp.jsloads and add that origin toALLOWED_ORIGINSin.env.
Medical disclaimer: translations marked medium or low confidence must be confirmed with a native speaker before being used in clinical decisions.
Release files for Tan-lang-chatbot 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tan_lang_chatbot-0.1.2.tar.gz | 3.3 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tan_lang_chatbot-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.8 MB
Release files / tan_lang_chatbot-0.1.2.tar.gz
| Download URL | tan_lang_chatbot-0.1.2.tar.gz |
|---|---|
| Size | 3.3 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
091b4473e35538b0619b102e9ce2470dcfaf4fbe0f884ccf16534c634a5a2872
|
|
BLAKE2b-256 checksum How to use checksums |
a1f2517291d72c9d06ffd30b3b73e4e93229995572fe674d04fa9eac74d46f79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / tan_lang_chatbot-0.1.2-py3-none-any.whl
| Download URL | tan_lang_chatbot-0.1.2-py3-none-any.whl |
|---|---|
| Size | 3.5 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
39c080d2bcfc591fde1f5bf669ca95c5776d7ce12c7088836edab47583a99e20
|
|
BLAKE2b-256 checksum How to use checksums |
4462042a20a840abc4a9833bfdfd7d7894c2865f2c856b22282fa6a0c7175aa8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|