Afrinetwork7 commited on
Commit
4d56027
1 Parent(s): 06e4c74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -25
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
- audio_bytes = base64.b64decode(request.audio)
27
- audio_array, sample_rate = sf.read(io.BytesIO(audio_bytes))
28
-
29
- result = transcribe(audio_array, request.language)
30
- return JSONResponse(content={"transcription": result})
 
 
 
 
31
 
32
  @app.post("/synthesize")
33
  async def synthesize_speech(request: TTSRequest):
34
- audio, filtered_text = synthesize(request.text, request.language, request.speed)
35
-
36
- # Convert numpy array to bytes
37
- buffer = io.BytesIO()
38
- sf.write(buffer, audio, 22050, format='wav')
39
- buffer.seek(0)
40
-
41
- return FileResponse(
42
- buffer,
43
- media_type="audio/wav",
44
- headers={"Content-Disposition": "attachment; filename=synthesized_audio.wav"}
45
- )
 
 
 
 
46
 
47
  @app.post("/identify")
48
  async def identify_language(request: AudioRequest):
49
- audio_bytes = base64.b64decode(request.audio)
50
- audio_array, sample_rate = sf.read(io.BytesIO(audio_bytes))
51
-
52
- result = identify(audio_array)
53
- return JSONResponse(content={"language_identification": result})
 
 
 
 
54
 
55
  @app.get("/asr_languages")
56
  async def get_asr_languages():
57
- return JSONResponse(content=ASR_LANGUAGES)
 
 
 
 
58
 
59
  @app.get("/tts_languages")
60
  async def get_tts_languages():
61
- return JSONResponse(content=TTS_LANGUAGES)
 
 
 
 
 
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)}")