Spaces:
Sleeping
Sleeping
File size: 4,879 Bytes
b368e21 1a05dd7 |
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
"""
Created By: ishwor subedi
Date: 2024-07-31
"""
import os
import tempfile
from fastapi.responses import JSONResponse
from fastapi import Form
from fastapi import UploadFile, HTTPException, status
from src.models.models import TextToSpeechRequest
from fastapi.routing import APIRouter
from src.pipeline.speech_transcription_pipeline import SpeechTranscriptionPipeline
speech_translator_router = APIRouter(tags=["SpeechTranscription"])
pipeline = SpeechTranscriptionPipeline()
@speech_translator_router.post(
"/text_to_speech",
description="""
** For language refer below points**
**Supported Locales:**
- **English:**
- **Australia:**
- **Language:** en
- **TLD:** com.au
- **United Kingdom:**
- **Language:** en
- **TLD:** co.uk
- **United States:**
- **Language:** en
- **TLD:** us
- **Canada:**
- **Language:** en
- **TLD:** ca
- **India:**
- **Language:** en
- **TLD:** co.in
- **Ireland:**
- **Language:** en
- **TLD:** ie
- **South Africa:**
- **Language:** en
- **TLD:** co.za
- **Nigeria:**
- **Language:** en
- **TLD:** com.ng
- **French:**
- **Canada:**
- **Language:** fr
- **TLD:** ca
- **France:**
- **Language:** fr
- **TLD:** fr
- **Mandarin:**
- **China Mainland:**
- **Language:** zh-CN
- **TLD:** any
- **Taiwan:**
- **Language:** zh-TW
- **TLD:** any
- **Portuguese:**
- **Brazil:**
- **Language:** pt
- **TLD:** com.br
- **Portugal:**
- **Language:** pt
- **TLD:** pt
- **Spanish:**
- **Mexico:**
- **Language:** es
- **TLD:** com.mx
- **Spain:**
- **Language:** es
- **TLD:** es
- **United States:**
- **Language:** es
- **TLD:** us
"""
)
async def text_to_speech(request: TextToSpeechRequest):
try:
audio_bytes = 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": status.HTTP_200_OK}, status_code=200)
except ValueError as ve:
raise HTTPException(status_code=400, detail=str(ve))
except Exception as e:
raise HTTPException(status_code=500, detail="Internal Server Error")
@speech_translator_router.post(
"/speech_to_text",
description="""
** Specify the language used in the audio **
**Supported Languages:**
**Major Languages:**
- **English:** en
- **Mandarin Chinese:** zh
- **Spanish:** es
- **French:** fr
- **German:** de
- **Italian:** it
- **Japanese:** ja
- **Korean:** ko
- **Russian:** ru
- **Portuguese:** pt
- **Arabic:** ar
**Additional Languages:**
- **Indic Languages:**
- **Hindi:** hi
- **Bengali:** bn
- **Tamil:** ta
- **Telugu:** te
- **Southeast Asian Languages:**
- **Vietnamese:** vi
- **Thai:** th
- **Indonesian:** id
- **Malay:** ms
- **African Languages:**
- **Swahili:** sw
- **Yoruba:** yo
- **Hausa:** ha
- **European Languages:**
- **Polish:** pl
- **Dutch:** nl
- **Swedish:** sv
- **Norwegian:** no
"""
)
async def speech_to_text(audio: UploadFile, lang: str = Form(...)):
try:
audio_bytes = await audio.read()
if not audio_bytes:
raise ValueError("Empty audio file")
except Exception as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid audio file"
)
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
temp_audio_file.write(audio_bytes)
temp_audio_file_path = temp_audio_file.name
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Could not process audio file"
)
try:
transcript = pipeline.speech_to_text(temp_audio_file_path, lang)
except FileNotFoundError as fnfe:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Temporary file not found"
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Error processing speech-to-text"
)
finally:
if os.path.exists(temp_audio_file_path):
os.remove(temp_audio_file_path)
return JSONResponse(content={"transcript": transcript, "status_code": status.HTTP_200_OK}, status_code=200)
|