File size: 4,456 Bytes
7e3c986 b7a229d a6299ef b7a229d a6299ef b7a229d bb94e39 b7a229d 405b2f1 b7a229d 7e3c986 bb94e39 7e3c986 a6299ef 7e3c986 bb94e39 7e3c986 b7a229d bb94e39 b7a229d 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 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
from language.fr_mos import translateFRMOS
from language.correction_service import CorrectionService
import os
from huggingface_hub import login
if token := os.getenv("HF_TOKEN"):
login(token=token)
app = FastAPI(
version='1.0.0',
root_path='/api',
)
correctionService = CorrectionService()
@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')
if sourceLang in ['eng_Latn', 'fra_Latn']:
text = correctionService.correctText(text=text, sourceLang=sourceLang)
if not text.endswith("."):
text += "."
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("/codeLi/translateText")
async def translateMoore(request: Request):
body: dict = await request.json()
try:
text = body.get('text')
sourceLang = body.get('sourceLang')
targetLang = body.get('targetLang')
if sourceLang == 'fra_Latn':
text = correctionService.correctText(text=text, sourceLang=sourceLang)
if not text.endswith("."):
text += "."
translatedText = translateFRMOS(text=text, sourceLang=sourceLang, targetLang=targetLang)
return { 'translatedText': translatedText }
except Exception as e:
print(f"Translate mos error: {e}")
raise HTTPException(status_code=400, detail=f"Translate mos 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}")
|