Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, Form
|
| 2 |
from fastapi.responses import JSONResponse, FileResponse
|
| 3 |
import uvicorn
|
| 4 |
from pydantic import BaseModel
|
|
@@ -9,6 +9,11 @@ import base64
|
|
| 9 |
from asr import transcribe, ASR_LANGUAGES
|
| 10 |
from tts import synthesize, TTS_LANGUAGES
|
| 11 |
from lid import identify
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
app = FastAPI(title="MMS: Scaling Speech Technology to 1000+ languages")
|
| 14 |
|
|
@@ -23,39 +28,59 @@ class AudioRequest(BaseModel):
|
|
| 23 |
|
| 24 |
@app.post("/transcribe")
|
| 25 |
async def transcribe_audio(request: AudioRequest):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
@app.post("/synthesize")
|
| 33 |
async def synthesize_speech(request: TTSRequest):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
@app.post("/identify")
|
| 48 |
async def identify_language(request: AudioRequest):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
@app.get("/asr_languages")
|
| 56 |
async def get_asr_languages():
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
@app.get("/tts_languages")
|
| 60 |
async def get_tts_languages():
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Form, HTTPException
|
| 2 |
from fastapi.responses import JSONResponse, FileResponse
|
| 3 |
import uvicorn
|
| 4 |
from pydantic import BaseModel
|
|
|
|
| 9 |
from asr import transcribe, ASR_LANGUAGES
|
| 10 |
from tts import synthesize, TTS_LANGUAGES
|
| 11 |
from lid import identify
|
| 12 |
+
import logging
|
| 13 |
+
|
| 14 |
+
# Configure logging
|
| 15 |
+
logging.basicConfig(level=logging.INFO)
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
app = FastAPI(title="MMS: Scaling Speech Technology to 1000+ languages")
|
| 19 |
|
|
|
|
| 28 |
|
| 29 |
@app.post("/transcribe")
|
| 30 |
async def transcribe_audio(request: AudioRequest):
|
| 31 |
+
try:
|
| 32 |
+
audio_bytes = base64.b64decode(request.audio)
|
| 33 |
+
audio_array, sample_rate = sf.read(io.BytesIO(audio_bytes))
|
| 34 |
+
|
| 35 |
+
result = transcribe(audio_array, request.language)
|
| 36 |
+
return JSONResponse(content={"transcription": result})
|
| 37 |
+
except Exception as e:
|
| 38 |
+
logger.error(f"Error in transcribe_audio: {str(e)}")
|
| 39 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 40 |
|
| 41 |
@app.post("/synthesize")
|
| 42 |
async def synthesize_speech(request: TTSRequest):
|
| 43 |
+
try:
|
| 44 |
+
audio, filtered_text = synthesize(request.text, request.language, request.speed)
|
| 45 |
+
|
| 46 |
+
# Convert numpy array to bytes
|
| 47 |
+
buffer = io.BytesIO()
|
| 48 |
+
sf.write(buffer, audio, 22050, format='wav')
|
| 49 |
+
buffer.seek(0)
|
| 50 |
+
|
| 51 |
+
return FileResponse(
|
| 52 |
+
buffer,
|
| 53 |
+
media_type="audio/wav",
|
| 54 |
+
headers={"Content-Disposition": "attachment; filename=synthesized_audio.wav"}
|
| 55 |
+
)
|
| 56 |
+
except Exception as e:
|
| 57 |
+
logger.error(f"Error in synthesize_speech: {str(e)}")
|
| 58 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 59 |
|
| 60 |
@app.post("/identify")
|
| 61 |
async def identify_language(request: AudioRequest):
|
| 62 |
+
try:
|
| 63 |
+
audio_bytes = base64.b64decode(request.audio)
|
| 64 |
+
audio_array, sample_rate = sf.read(io.BytesIO(audio_bytes))
|
| 65 |
+
|
| 66 |
+
result = identify(audio_array)
|
| 67 |
+
return JSONResponse(content={"language_identification": result})
|
| 68 |
+
except Exception as e:
|
| 69 |
+
logger.error(f"Error in identify_language: {str(e)}")
|
| 70 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 71 |
|
| 72 |
@app.get("/asr_languages")
|
| 73 |
async def get_asr_languages():
|
| 74 |
+
try:
|
| 75 |
+
return JSONResponse(content=ASR_LANGUAGES)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
logger.error(f"Error in get_asr_languages: {str(e)}")
|
| 78 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 79 |
|
| 80 |
@app.get("/tts_languages")
|
| 81 |
async def get_tts_languages():
|
| 82 |
+
try:
|
| 83 |
+
return JSONResponse(content=TTS_LANGUAGES)
|
| 84 |
+
except Exception as e:
|
| 85 |
+
logger.error(f"Error in get_tts_languages: {str(e)}")
|
| 86 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|