AI-API-AR / main.py
Anicet
first commit
b62fe62
Raw
History Blame Contribute Delete
1.36 kB
from fastapi import FastAPI, Request, HTTPException
from language.saudi_arabe.ar_stt import arSTT
from language.saudi_arabe.ar_tts import arTTS
app = FastAPI(
version='1.0.0',
root_path='/api',
)
@app.post("/mms/speechToText")
async def mmsSpeechToText(request: Request):
body: dict = await request.json()
try:
audioBase64 = body.get('audioBase64')
sourceLang = body.get('sourceLang')
if sourceLang == 'ar':
data = arSTT(audioBase64=audioBase64)
return data
else:
raise HTTPException(status_code=400, detail=f"STT error: Invalid sourceLang - {sourceLang}")
except Exception as e:
print(f"STT error: {e}")
raise HTTPException(status_code=400, detail=f"STT error: {e}")
@app.post("/mms/textToSpeech")
async def mmsTextToSpeech(request: Request):
body: dict = await request.json()
try:
text = body.get('text')
sourceLang = body.get('sourceLang')
if sourceLang == 'ar':
audioBase64 = arTTS(text=text)
return { 'audioBase64': audioBase64 }
else:
raise HTTPException(status_code=400, detail=f"STT error: Invalid sourceLang - {sourceLang}")
except Exception as e:
print(f"TTS error: {e}")
raise HTTPException(status_code=400, detail=f"TTS error: {e}")