PEFT
Joblib
Safetensors
Swahili
English
afrolingo-2
kenya
swahili
sheng
swaenglish
asr
tts
voice-cloning
dialect-classification
fastapi
Instructions to use MRM5/AfroLingo with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use MRM5/AfroLingo with PEFT:
Task type is invalid.
- Notebooks
- Google Colab
- Kaggle
Ensure native Python float serialization in FastAPI router for NumPy 2.x
Browse files- sdk/afrolingo2_engine.py +4 -4
- sdk/fastapi_afrolingo2_router.py +44 -8
sdk/afrolingo2_engine.py
CHANGED
|
@@ -154,7 +154,7 @@ class AfroLingo2Engine:
|
|
| 154 |
"sample_rate": sr,
|
| 155 |
"dirty_snr_db": dirty_snr,
|
| 156 |
"clean_snr_db": clean_snr,
|
| 157 |
-
"snr_gain_db": round(clean_snr - dirty_snr, 2),
|
| 158 |
"output_path": str(out_path) if out_path else None,
|
| 159 |
}
|
| 160 |
|
|
@@ -320,9 +320,9 @@ class AfroLingo2Engine:
|
|
| 320 |
"detected_register": inferred_gender,
|
| 321 |
"reference_f0_hz": round(ref_prof["f0_med"], 1),
|
| 322 |
"cloned_f0_hz": round(cloned_prof["f0_med"], 1),
|
| 323 |
-
"f0_pitch_error_hz": round(f0_err, 2),
|
| 324 |
-
"speaker_cosine_similarity_pct": round(sim_pct, 2),
|
| 325 |
-
"mel_cepstral_distortion_db": round(mcd_db, 2),
|
| 326 |
"duration_s": round(len(cloned_wav) / 16000, 2),
|
| 327 |
}
|
| 328 |
|
|
|
|
| 154 |
"sample_rate": sr,
|
| 155 |
"dirty_snr_db": dirty_snr,
|
| 156 |
"clean_snr_db": clean_snr,
|
| 157 |
+
"snr_gain_db": round(float(clean_snr - dirty_snr), 2),
|
| 158 |
"output_path": str(out_path) if out_path else None,
|
| 159 |
}
|
| 160 |
|
|
|
|
| 320 |
"detected_register": inferred_gender,
|
| 321 |
"reference_f0_hz": round(ref_prof["f0_med"], 1),
|
| 322 |
"cloned_f0_hz": round(cloned_prof["f0_med"], 1),
|
| 323 |
+
"f0_pitch_error_hz": round(float(f0_err), 2),
|
| 324 |
+
"speaker_cosine_similarity_pct": round(float(sim_pct), 2),
|
| 325 |
+
"mel_cepstral_distortion_db": round(float(mcd_db), 2),
|
| 326 |
"duration_s": round(len(cloned_wav) / 16000, 2),
|
| 327 |
}
|
| 328 |
|
sdk/fastapi_afrolingo2_router.py
CHANGED
|
@@ -6,7 +6,8 @@
|
|
| 6 |
import os
|
| 7 |
import tempfile
|
| 8 |
from pathlib import Path
|
| 9 |
-
from typing import Optional
|
|
|
|
| 10 |
from fastapi import APIRouter, UploadFile, File, Form
|
| 11 |
from fastapi.responses import FileResponse
|
| 12 |
from pydantic import BaseModel, Field
|
|
@@ -19,6 +20,21 @@ engine = AfroLingo2Engine(AFROLINGO2_REPO)
|
|
| 19 |
router = APIRouter(prefix="/api/v1/afrolingo2", tags=["AfroLingo-2 Speech Intelligence"])
|
| 20 |
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
class TTSRequest(BaseModel):
|
| 23 |
text: str = Field(..., example="Karibu kwenye mfumo wa AfroLingo-2 ulioundwa na Qhala.")
|
| 24 |
gender: str = Field("female", description="'female' (Zuri) or 'male' (Rafiki)")
|
|
@@ -52,7 +68,7 @@ def api_generate_speech(req: TTSRequest):
|
|
| 52 |
text=req.text, out_path=out_file, gender=req.gender, engine=req.engine_type, use_lexicon=req.use_lexicon
|
| 53 |
)
|
| 54 |
res.pop("waveform", None)
|
| 55 |
-
return res
|
| 56 |
|
| 57 |
|
| 58 |
@router.post("/voice/clone", summary="2. Clone a Custom Voice from Uploaded Reference Audio")
|
|
@@ -65,35 +81,39 @@ async def api_clone_custom_voice(
|
|
| 65 |
ref_tmp.write(await reference_audio.read())
|
| 66 |
ref_tmp.close()
|
| 67 |
out_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
|
| 68 |
-
|
| 69 |
text=text, reference_audio_path=ref_tmp.name, out_path=out_file, preferred_gender=preferred_gender
|
| 70 |
)
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
@router.post("/dialect/normalize", summary="3. Normalize Sheng & Swaenglish -> Kiswahili Sanifu")
|
| 74 |
def api_normalize_sheng(req: ShengNormalizeRequest):
|
| 75 |
out_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name if req.synthesize_audio else None
|
| 76 |
-
|
| 77 |
text=req.text, synthesize_audio=req.synthesize_audio, out_audio_path=out_file, gender=req.gender
|
| 78 |
)
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
@router.post("/dubbing/sync", summary="4. Isochronous Time-Synced Neural Dubbing (Sanifu & Sheng)")
|
| 82 |
def api_dub_time_synced(req: DubbingRequest):
|
| 83 |
out_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
|
| 84 |
-
|
| 85 |
script_text=req.script_text,
|
| 86 |
out_path=out_file,
|
| 87 |
target_duration_s=req.target_duration_s,
|
| 88 |
mode=req.mode,
|
| 89 |
gender=req.gender,
|
| 90 |
)
|
|
|
|
| 91 |
|
| 92 |
|
| 93 |
@router.post("/emotion/synthesize", summary="5. Expressive Voice Tone & Emotion Conditioning")
|
| 94 |
def api_emotion_synthesize(req: EmotionTTSRequest):
|
| 95 |
out_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
@router.post("/audio/clean", summary="6. Studio DSP Audio Cleaning (Dirty -> Clean WAV + SNR Gain)")
|
|
@@ -104,7 +124,7 @@ async def api_clean_audio(audio_file: UploadFile = File(...)):
|
|
| 104 |
out_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
|
| 105 |
res = engine.clean_audio(in_tmp.name, out_path=out_file)
|
| 106 |
res.pop("clean_wav", None)
|
| 107 |
-
return res
|
| 108 |
|
| 109 |
|
| 110 |
@router.post("/geo/predict-county", summary="7. Kenyan County & Dialect Geo-Localization (96.88% Accuracy)")
|
|
@@ -112,7 +132,23 @@ async def api_predict_county(audio_file: UploadFile = File(...)):
|
|
| 112 |
in_tmp = tempfile.NamedTemporaryFile(suffix=Path(audio_file.filename).suffix or ".wav", delete=False)
|
| 113 |
in_tmp.write(await audio_file.read())
|
| 114 |
in_tmp.close()
|
| 115 |
-
return engine.predict_county(in_tmp.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
|
| 118 |
@router.get("/audio/download", summary="Download Any Generated/Cloned/Cleaned Audio File")
|
|
|
|
| 6 |
import os
|
| 7 |
import tempfile
|
| 8 |
from pathlib import Path
|
| 9 |
+
from typing import Optional, Any
|
| 10 |
+
import numpy as np
|
| 11 |
from fastapi import APIRouter, UploadFile, File, Form
|
| 12 |
from fastapi.responses import FileResponse
|
| 13 |
from pydantic import BaseModel, Field
|
|
|
|
| 20 |
router = APIRouter(prefix="/api/v1/afrolingo2", tags=["AfroLingo-2 Speech Intelligence"])
|
| 21 |
|
| 22 |
|
| 23 |
+
def _to_py(obj: Any) -> Any:
|
| 24 |
+
"""Recursively converts NumPy scalars/arrays into native Python types for FastAPI JSON encoding."""
|
| 25 |
+
if isinstance(obj, dict):
|
| 26 |
+
return {k: _to_py(v) for k, v in obj.items()}
|
| 27 |
+
if isinstance(obj, (list, tuple)):
|
| 28 |
+
return [_to_py(v) for v in obj]
|
| 29 |
+
if isinstance(obj, (np.floating, float)):
|
| 30 |
+
return float(obj)
|
| 31 |
+
if isinstance(obj, (np.integer, int)):
|
| 32 |
+
return int(obj)
|
| 33 |
+
if isinstance(obj, np.ndarray):
|
| 34 |
+
return obj.tolist()
|
| 35 |
+
return obj
|
| 36 |
+
|
| 37 |
+
|
| 38 |
class TTSRequest(BaseModel):
|
| 39 |
text: str = Field(..., example="Karibu kwenye mfumo wa AfroLingo-2 ulioundwa na Qhala.")
|
| 40 |
gender: str = Field("female", description="'female' (Zuri) or 'male' (Rafiki)")
|
|
|
|
| 68 |
text=req.text, out_path=out_file, gender=req.gender, engine=req.engine_type, use_lexicon=req.use_lexicon
|
| 69 |
)
|
| 70 |
res.pop("waveform", None)
|
| 71 |
+
return _to_py(res)
|
| 72 |
|
| 73 |
|
| 74 |
@router.post("/voice/clone", summary="2. Clone a Custom Voice from Uploaded Reference Audio")
|
|
|
|
| 81 |
ref_tmp.write(await reference_audio.read())
|
| 82 |
ref_tmp.close()
|
| 83 |
out_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
|
| 84 |
+
res = engine.clone_custom_voice(
|
| 85 |
text=text, reference_audio_path=ref_tmp.name, out_path=out_file, preferred_gender=preferred_gender
|
| 86 |
)
|
| 87 |
+
return _to_py(res)
|
| 88 |
|
| 89 |
|
| 90 |
@router.post("/dialect/normalize", summary="3. Normalize Sheng & Swaenglish -> Kiswahili Sanifu")
|
| 91 |
def api_normalize_sheng(req: ShengNormalizeRequest):
|
| 92 |
out_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name if req.synthesize_audio else None
|
| 93 |
+
res = engine.normalize_sheng_to_sanifu(
|
| 94 |
text=req.text, synthesize_audio=req.synthesize_audio, out_audio_path=out_file, gender=req.gender
|
| 95 |
)
|
| 96 |
+
return _to_py(res)
|
| 97 |
|
| 98 |
|
| 99 |
@router.post("/dubbing/sync", summary="4. Isochronous Time-Synced Neural Dubbing (Sanifu & Sheng)")
|
| 100 |
def api_dub_time_synced(req: DubbingRequest):
|
| 101 |
out_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
|
| 102 |
+
res = engine.dub_time_synced(
|
| 103 |
script_text=req.script_text,
|
| 104 |
out_path=out_file,
|
| 105 |
target_duration_s=req.target_duration_s,
|
| 106 |
mode=req.mode,
|
| 107 |
gender=req.gender,
|
| 108 |
)
|
| 109 |
+
return _to_py(res)
|
| 110 |
|
| 111 |
|
| 112 |
@router.post("/emotion/synthesize", summary="5. Expressive Voice Tone & Emotion Conditioning")
|
| 113 |
def api_emotion_synthesize(req: EmotionTTSRequest):
|
| 114 |
out_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
|
| 115 |
+
res = engine.synthesize_with_emotion(text=req.text, out_path=out_file, tone=req.tone, gender=req.gender)
|
| 116 |
+
return _to_py(res)
|
| 117 |
|
| 118 |
|
| 119 |
@router.post("/audio/clean", summary="6. Studio DSP Audio Cleaning (Dirty -> Clean WAV + SNR Gain)")
|
|
|
|
| 124 |
out_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False).name
|
| 125 |
res = engine.clean_audio(in_tmp.name, out_path=out_file)
|
| 126 |
res.pop("clean_wav", None)
|
| 127 |
+
return _to_py(res)
|
| 128 |
|
| 129 |
|
| 130 |
@router.post("/geo/predict-county", summary="7. Kenyan County & Dialect Geo-Localization (96.88% Accuracy)")
|
|
|
|
| 132 |
in_tmp = tempfile.NamedTemporaryFile(suffix=Path(audio_file.filename).suffix or ".wav", delete=False)
|
| 133 |
in_tmp.write(await audio_file.read())
|
| 134 |
in_tmp.close()
|
| 135 |
+
return _to_py(engine.predict_county(in_tmp.name))
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
@router.post("/asr/transcribe", summary="8. Hybrid Speech-to-Text Transcription (AfroLingo-2 Master)")
|
| 139 |
+
async def api_transcribe(audio_file: UploadFile = File(...)):
|
| 140 |
+
in_tmp = tempfile.NamedTemporaryFile(suffix=Path(audio_file.filename).suffix or ".wav", delete=False)
|
| 141 |
+
in_tmp.write(await audio_file.read())
|
| 142 |
+
in_tmp.close()
|
| 143 |
+
return _to_py(engine.transcribe(in_tmp.name))
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
@router.post("/diarization/turns", summary="9. Multi-Speaker Turn-by-Turn Interview Diarization")
|
| 147 |
+
async def api_diarize(audio_file: UploadFile = File(...)):
|
| 148 |
+
in_tmp = tempfile.NamedTemporaryFile(suffix=Path(audio_file.filename).suffix or ".wav", delete=False)
|
| 149 |
+
in_tmp.write(await audio_file.read())
|
| 150 |
+
in_tmp.close()
|
| 151 |
+
return _to_py({"turns": engine.diarize_interview(in_tmp.name)})
|
| 152 |
|
| 153 |
|
| 154 |
@router.get("/audio/download", summary="Download Any Generated/Cloned/Cleaned Audio File")
|