File size: 3,271 Bytes
7e3c986
 
 
 
cc36729
a6299ef
 
cc36729
a6299ef
 
 
7e3c986
 
 
 
 
 
 
 
a6299ef
7e3c986
 
 
 
 
 
 
 
 
 
 
 
 
 
a6299ef
 
7e3c986
 
 
 
 
 
 
 
 
 
 
 
a6299ef
 
7e3c986
 
 
 
 
 
 
 
 
 
 
 
a6299ef
 
7e3c986
 
 
a6299ef
 
 
 
 
 
 
 
 
 
7e3c986
 
 
 
 
a6299ef
 
7e3c986
 
 
a6299ef
7e3c986
a6299ef
 
 
 
 
 
 
 
7e3c986
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from fastapi import FastAPI, Request, HTTPException
from functions.translation import translateText
from functions.speech_to_text import speechToText
from functions.text_to_speech import textToSpeech

from language.moore.mos_stt import mooreSTT
from language.moore.mos_tts import mooreTTS

from language.dioula.dyu_stt import dioulaSTT
from language.dioula.dyu_tts import dioulaTTS



app = FastAPI(
    version='1.0.0',
    root_path='/api',
)


@app.post("/nllb/translateText")
async def translate(request: Request):
    body: dict = await request.json()
    try:
        text = body.get('text')
        sourceLang = body.get('sourceLang')
        targetLang = body.get('targetLang')

        translatedText = translateText(text=text, sourceLang=sourceLang, targetLang=targetLang)
        return { 'translatedText': translatedText }
    except Exception as e:
        print(f"Translate error: {e}")
        raise HTTPException(status_code=400, detail=f"Translate error: {e}")


@app.post("/whisper/speechToText")
async def whisperSpeechToText(request: Request):
    body: dict = await request.json()
    try:
        audioBase64 = body.get('audioBase64')
        sourceLang = body.get('sourceLang')
        
        data = speechToText(audioBase64=audioBase64, sourceLang=sourceLang)
        return data
    except Exception as e:
        print(f"STT error: {e}")
        raise HTTPException(status_code=400, detail=f"STT error: {e}")


@app.post("/edge/textToSpeech")
async def edgeTextToSpeech(request: Request):
    body: dict = await request.json()
    try:
        text = body.get('text')
        voice = body.get('voice')

        audioBase64 = await textToSpeech(text=text, voice=voice)
        return { 'audioBase64': audioBase64 }
    except Exception as e:
        print(f"TTS error: {e}")
        raise HTTPException(status_code=400, detail=f"TTS error: {e}")


@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 == 'mos':
            data = mooreSTT(audioBase64=audioBase64)
            return data
        elif sourceLang == 'dyu':
            data = dioulaSTT(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 == 'mos':
            audioBase64 = mooreTTS(text=text)
            return { 'audioBase64': audioBase64 }
        elif sourceLang == 'dyu':
            audioBase64 = dioulaTTS(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}")