Spaces:
Running
Running
from fastapi import APIRouter, File, UploadFile, HTTPException | |
from fastapi.responses import StreamingResponse | |
from pydantic import BaseModel | |
from ai4b import BhashiniClient | |
from fast_langdetect import detect | |
import io | |
import base64 | |
import os | |
router = APIRouter() | |
ULCA_USER_ID = os.getenv("ULCA_USER_ID") | |
ULCA_API_KEY = os.getenv("ULCA_API_KEY") | |
client = BhashiniClient(user_id=ULCA_USER_ID, api_key=ULCA_API_KEY) | |
class TTSRequest(BaseModel): | |
text: str | |
voice: str = "female" | |
SUPPORTED_LANGUAGES = {'pa', 'mr', 'bn', 'en', 'as', 'or', 'ta', 'te', 'kn', 'gu', 'hi', 'ml'} | |
def detect_language(text): | |
text = text.replace("\n", " ") | |
try: | |
result = detect(text, low_memory=False) | |
detected_lang = result['lang'] | |
if detected_lang in SUPPORTED_LANGUAGES: | |
return detected_lang | |
except: | |
pass | |
if any('\u0980' <= char <= '\u09FF' for char in text): | |
return 'brx' | |
elif any('\uABC0' <= char <= '\uABFF' for char in text): | |
return 'mni' | |
return 'en' | |
async def text_to_speech(request: TTSRequest): | |
try: | |
detected_language = detect_language(request.text) | |
tts_result = client.tts( | |
request.text, | |
source_language=detected_language, | |
gender=request.voice | |
) | |
audio_base64 = tts_result['pipelineResponse'][0]['audio'][0]['audioContent'] | |
audio_data = base64.b64decode(audio_base64) | |
return StreamingResponse(io.BytesIO(audio_data), media_type="audio/wav") | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
async def speech_to_text(file: UploadFile = File(...), source_language: str = "en"): | |
try: | |
audio_content = await file.read() | |
asr_result = client.asr(audio_content, source_language=source_language) | |
# Extract the transcribed text from the complex JSON structure | |
transcribed_text = asr_result['pipelineResponse'][0]['output'][0]['source'] | |
return {"text": transcribed_text} | |
except KeyError: | |
raise HTTPException(status_code=500, detail=f"Unexpected response structure from ASR service out:{asr_result}") | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |