Spaces:
Sleeping
Sleeping
Delete speech_api.py
Browse files- speech_api.py +0 -71
speech_api.py
DELETED
@@ -1,71 +0,0 @@
|
|
1 |
-
from fastapi import APIRouter, File, UploadFile, HTTPException
|
2 |
-
from fastapi.responses import StreamingResponse
|
3 |
-
from pydantic import BaseModel
|
4 |
-
from ai4b import BhashiniClient
|
5 |
-
from fast_langdetect import detect
|
6 |
-
import io
|
7 |
-
import base64
|
8 |
-
import os
|
9 |
-
|
10 |
-
router = APIRouter()
|
11 |
-
|
12 |
-
ULCA_USER_ID = os.getenv("ULCA_USER_ID")
|
13 |
-
ULCA_API_KEY = os.getenv("ULCA_API_KEY")
|
14 |
-
|
15 |
-
client = BhashiniClient(user_id=ULCA_USER_ID, api_key=ULCA_API_KEY)
|
16 |
-
|
17 |
-
class TTSRequest(BaseModel):
|
18 |
-
text: str
|
19 |
-
voice: str = "female"
|
20 |
-
|
21 |
-
SUPPORTED_LANGUAGES = {'pa', 'mr', 'bn', 'en', 'as', 'or', 'ta', 'te', 'kn', 'gu', 'hi', 'ml'}
|
22 |
-
|
23 |
-
def detect_language(text):
|
24 |
-
text = text.replace("\n", " ")
|
25 |
-
try:
|
26 |
-
result = detect(text, low_memory=False)
|
27 |
-
detected_lang = result['lang']
|
28 |
-
if detected_lang in SUPPORTED_LANGUAGES:
|
29 |
-
return detected_lang
|
30 |
-
except:
|
31 |
-
pass
|
32 |
-
|
33 |
-
if any('\u0980' <= char <= '\u09FF' for char in text):
|
34 |
-
return 'brx'
|
35 |
-
elif any('\uABC0' <= char <= '\uABFF' for char in text):
|
36 |
-
return 'mni'
|
37 |
-
|
38 |
-
return 'en'
|
39 |
-
|
40 |
-
@router.post("/tts")
|
41 |
-
async def text_to_speech(request: TTSRequest):
|
42 |
-
try:
|
43 |
-
detected_language = detect_language(request.text)
|
44 |
-
|
45 |
-
tts_result = client.tts(
|
46 |
-
request.text,
|
47 |
-
source_language=detected_language,
|
48 |
-
gender=request.voice
|
49 |
-
)
|
50 |
-
|
51 |
-
audio_base64 = tts_result['pipelineResponse'][0]['audio'][0]['audioContent']
|
52 |
-
audio_data = base64.b64decode(audio_base64)
|
53 |
-
|
54 |
-
return StreamingResponse(io.BytesIO(audio_data), media_type="audio/wav")
|
55 |
-
except Exception as e:
|
56 |
-
raise HTTPException(status_code=500, detail=str(e))
|
57 |
-
|
58 |
-
@router.post("/asr")
|
59 |
-
async def speech_to_text(file: UploadFile = File(...), source_language: str = "en"):
|
60 |
-
try:
|
61 |
-
audio_content = await file.read()
|
62 |
-
asr_result = client.asr(audio_content, source_language=source_language)
|
63 |
-
|
64 |
-
# Extract the transcribed text from the complex JSON structure
|
65 |
-
transcribed_text = asr_result['pipelineResponse'][0]['output'][0]['source']
|
66 |
-
|
67 |
-
return {"text": transcribed_text}
|
68 |
-
except KeyError:
|
69 |
-
raise HTTPException(status_code=500, detail=f"Unexpected response structure from ASR service out:{asr_result}")
|
70 |
-
except Exception as e:
|
71 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|