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}")