File size: 2,292 Bytes
287c28c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import uvicorn
from fastapi import FastAPI
from fastapi.responses import Response
from fastapi.middleware.cors import CORSMiddleware
from TTS.utils.synthesizer import Synthesizer
from src.inference import TextToSpeechEngine
from src.models.request import TTSRequest
SUPPORTED_LANGUAGES = {
'as' : "Assamese - অসমীয়া",
'bn' : "Bangla - বাংলা",
'brx': "Boro - बड़ो",
'en' : "English (Indian accent)",
'en+hi': "English+Hindi (Hinglish code-mixed)",
'gu' : "Gujarati - ગુજરાતી",
'hi' : "Hindi - हिंदी",
'kn' : "Kannada - ಕನ್ನಡ",
'ml' : "Malayalam - മലയാളം",
'mni': "Manipuri - মিতৈলোন",
'mr' : "Marathi - मराठी",
'or' : "Oriya - ଓଡ଼ିଆ",
'pa' : "Panjabi - ਪੰਜਾਬੀ",
'raj': "Rajasthani - राजस्थानी",
'ta' : "Tamil - தமிழ்",
'te' : "Telugu - తెలుగు",
}
models = {}
for lang in SUPPORTED_LANGUAGES:
models[lang] = Synthesizer(
tts_checkpoint=f'checkpoints/{lang}/fastpitch/best_model.pth',
tts_config_path=f'checkpoints/{lang}/fastpitch/config.json',
tts_speakers_file=f'checkpoints/{lang}/fastpitch/speakers.pth',
# tts_speakers_file=None,
tts_languages_file=None,
vocoder_checkpoint=f'checkpoints/{lang}/hifigan/best_model.pth',
vocoder_config=f'checkpoints/{lang}/hifigan/config.json',
encoder_checkpoint="",
encoder_config="",
use_cuda=True,
)
print(f"Synthesizer loaded for {lang}.")
print("*"*100)
engine = TextToSpeechEngine(models)
api = FastAPI()
api.add_middleware(
CORSMiddleware,
allow_origins=["*"],
# allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@api.get("/supported_languages")
def get_supported_languages():
return SUPPORTED_LANGUAGES
@api.get("/")
def homepage():
return "AI4Bharat Text-To-Speech API"
@api.post("/")
async def batch_tts(request: TTSRequest, response: Response):
return engine.infer_from_request(request)
if __name__ == "__main__":
# uvicorn server:api --host 0.0.0.0 --port 5050 --log-level info
uvicorn.run("server:api", host="0.0.0.0", port=5050, log_level="info")
|