File size: 1,355 Bytes
b62fe62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}")