|
|
""" |
|
|
Created By: ishwor subedi |
|
|
Date: 2024-08-13 |
|
|
""" |
|
|
from fastapi import HTTPException |
|
|
from fastapi.routing import APIRouter |
|
|
from src.models.models import SpeechToTextRequest, TextToSpeechRequest |
|
|
from src.pipeline.speech_processing_pipeline import SpeechTranscriptionPipeline |
|
|
from fastapi.responses import JSONResponse |
|
|
|
|
|
speech_transcription_router = APIRouter() |
|
|
|
|
|
speech_transcription_pipeline = SpeechTranscriptionPipeline() |
|
|
|
|
|
|
|
|
@speech_transcription_router.post("/speech_to_text") |
|
|
async def speech_to_text(request: SpeechToTextRequest): |
|
|
try: |
|
|
transcript = speech_transcription_pipeline.speech_to_text(request.audio, request.lang) |
|
|
json = {'transcript': transcript, 'status_code': 200} |
|
|
return json |
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail=str) |
|
|
|
|
|
|
|
|
@speech_transcription_router.post("/text_to_speech") |
|
|
async def text_to_speech(request: TextToSpeechRequest): |
|
|
try: |
|
|
audio_bytes = speech_transcription_pipeline.text_to_speech(request.text, request.lang, request.tld) |
|
|
if not audio_bytes: |
|
|
raise ValueError("Audio generation failed.") |
|
|
return JSONResponse(content={"audio": audio_bytes, "status_code": 200}, status_code=200) |
|
|
|
|
|
except Exception as e: |
|
|
raise HTTPException(status_code=500, detail="Internal Server Error") |
|
|
|