Update app.py
Browse files
app.py
CHANGED
|
@@ -115,6 +115,7 @@ def extract_audio_from_file(input_bytes):
|
|
| 115 |
|
| 116 |
@app.post("/transcribe")
|
| 117 |
async def transcribe_audio(request: AudioRequest):
|
|
|
|
| 118 |
try:
|
| 119 |
input_bytes = base64.b64decode(request.audio)
|
| 120 |
audio_array, sample_rate = extract_audio_from_file(input_bytes)
|
|
@@ -127,20 +128,23 @@ async def transcribe_audio(request: AudioRequest):
|
|
| 127 |
audio_array = librosa.resample(audio_array, orig_sr=sample_rate, target_sr=ASR_SAMPLING_RATE)
|
| 128 |
|
| 129 |
result = transcribe(audio_array, request.language)
|
| 130 |
-
|
|
|
|
| 131 |
except Exception as e:
|
| 132 |
logger.error(f"Error in transcribe_audio: {str(e)}", exc_info=True)
|
| 133 |
error_details = {
|
| 134 |
"error": str(e),
|
| 135 |
"traceback": traceback.format_exc()
|
| 136 |
}
|
|
|
|
| 137 |
return JSONResponse(
|
| 138 |
status_code=500,
|
| 139 |
-
content={"message": "An error occurred during transcription", "details": error_details}
|
| 140 |
)
|
| 141 |
|
| 142 |
@app.post("/synthesize")
|
| 143 |
async def synthesize_speech(request: TTSRequest):
|
|
|
|
| 144 |
logger.info(f"Synthesize request received: text='{request.text}', language='{request.language}', speed={request.speed}")
|
| 145 |
try:
|
| 146 |
# Extract the ISO code from the full language name
|
|
@@ -204,7 +208,8 @@ async def synthesize_speech(request: TTSRequest):
|
|
| 204 |
url = f"https://s3.{S3_REGION}.amazonaws.com/{S3_BUCKET}/{filename}"
|
| 205 |
logger.info(f"Public URL generated: {url}")
|
| 206 |
|
| 207 |
-
|
|
|
|
| 208 |
|
| 209 |
except NoCredentialsError:
|
| 210 |
logger.error("AWS credentials not available or invalid")
|
|
@@ -215,9 +220,10 @@ async def synthesize_speech(request: TTSRequest):
|
|
| 215 |
|
| 216 |
except ValueError as ve:
|
| 217 |
logger.error(f"ValueError in synthesize_speech: {str(ve)}", exc_info=True)
|
|
|
|
| 218 |
return JSONResponse(
|
| 219 |
status_code=400,
|
| 220 |
-
content={"message": "Invalid input", "details": str(ve)}
|
| 221 |
)
|
| 222 |
except Exception as e:
|
| 223 |
logger.error(f"Unexpected error in synthesize_speech: {str(e)}", exc_info=True)
|
|
@@ -226,57 +232,67 @@ async def synthesize_speech(request: TTSRequest):
|
|
| 226 |
"type": type(e).__name__,
|
| 227 |
"traceback": traceback.format_exc()
|
| 228 |
}
|
|
|
|
| 229 |
return JSONResponse(
|
| 230 |
status_code=500,
|
| 231 |
-
content={"message": "An unexpected error occurred during speech synthesis", "details": error_details}
|
| 232 |
)
|
| 233 |
finally:
|
| 234 |
logger.info("Synthesize request completed")
|
| 235 |
|
| 236 |
@app.post("/identify")
|
| 237 |
async def identify_language(request: AudioRequest):
|
|
|
|
| 238 |
try:
|
| 239 |
input_bytes = base64.b64decode(request.audio)
|
| 240 |
audio_array, sample_rate = extract_audio_from_file(input_bytes)
|
| 241 |
result = identify(audio_array)
|
| 242 |
-
|
|
|
|
| 243 |
except Exception as e:
|
| 244 |
logger.error(f"Error in identify_language: {str(e)}", exc_info=True)
|
| 245 |
error_details = {
|
| 246 |
"error": str(e),
|
| 247 |
"traceback": traceback.format_exc()
|
| 248 |
}
|
|
|
|
| 249 |
return JSONResponse(
|
| 250 |
status_code=500,
|
| 251 |
-
content={"message": "An error occurred during language identification", "details": error_details}
|
| 252 |
)
|
| 253 |
|
| 254 |
@app.get("/asr_languages")
|
| 255 |
async def get_asr_languages():
|
|
|
|
| 256 |
try:
|
| 257 |
-
|
|
|
|
| 258 |
except Exception as e:
|
| 259 |
logger.error(f"Error in get_asr_languages: {str(e)}", exc_info=True)
|
| 260 |
error_details = {
|
| 261 |
"error": str(e),
|
| 262 |
"traceback": traceback.format_exc()
|
| 263 |
}
|
|
|
|
| 264 |
return JSONResponse(
|
| 265 |
status_code=500,
|
| 266 |
-
content={"message": "An error occurred while fetching ASR languages", "details": error_details}
|
| 267 |
)
|
| 268 |
|
| 269 |
@app.get("/tts_languages")
|
| 270 |
async def get_tts_languages():
|
|
|
|
| 271 |
try:
|
| 272 |
-
|
|
|
|
| 273 |
except Exception as e:
|
| 274 |
logger.error(f"Error in get_tts_languages: {str(e)}", exc_info=True)
|
| 275 |
error_details = {
|
| 276 |
"error": str(e),
|
| 277 |
"traceback": traceback.format_exc()
|
| 278 |
}
|
|
|
|
| 279 |
return JSONResponse(
|
| 280 |
status_code=500,
|
| 281 |
-
content={"message": "An error occurred while fetching TTS languages", "details": error_details}
|
| 282 |
)
|
|
|
|
| 115 |
|
| 116 |
@app.post("/transcribe")
|
| 117 |
async def transcribe_audio(request: AudioRequest):
|
| 118 |
+
start_time = time.time()
|
| 119 |
try:
|
| 120 |
input_bytes = base64.b64decode(request.audio)
|
| 121 |
audio_array, sample_rate = extract_audio_from_file(input_bytes)
|
|
|
|
| 128 |
audio_array = librosa.resample(audio_array, orig_sr=sample_rate, target_sr=ASR_SAMPLING_RATE)
|
| 129 |
|
| 130 |
result = transcribe(audio_array, request.language)
|
| 131 |
+
processing_time = time.time() - start_time
|
| 132 |
+
return JSONResponse(content={"transcription": result, "processing_time_seconds": processing_time})
|
| 133 |
except Exception as e:
|
| 134 |
logger.error(f"Error in transcribe_audio: {str(e)}", exc_info=True)
|
| 135 |
error_details = {
|
| 136 |
"error": str(e),
|
| 137 |
"traceback": traceback.format_exc()
|
| 138 |
}
|
| 139 |
+
processing_time = time.time() - start_time
|
| 140 |
return JSONResponse(
|
| 141 |
status_code=500,
|
| 142 |
+
content={"message": "An error occurred during transcription", "details": error_details, "processing_time_seconds": processing_time}
|
| 143 |
)
|
| 144 |
|
| 145 |
@app.post("/synthesize")
|
| 146 |
async def synthesize_speech(request: TTSRequest):
|
| 147 |
+
start_time = time.time()
|
| 148 |
logger.info(f"Synthesize request received: text='{request.text}', language='{request.language}', speed={request.speed}")
|
| 149 |
try:
|
| 150 |
# Extract the ISO code from the full language name
|
|
|
|
| 208 |
url = f"https://s3.{S3_REGION}.amazonaws.com/{S3_BUCKET}/{filename}"
|
| 209 |
logger.info(f"Public URL generated: {url}")
|
| 210 |
|
| 211 |
+
processing_time = time.time() - start_time
|
| 212 |
+
return JSONResponse(content={"audio_url": url, "processing_time_seconds": processing_time})
|
| 213 |
|
| 214 |
except NoCredentialsError:
|
| 215 |
logger.error("AWS credentials not available or invalid")
|
|
|
|
| 220 |
|
| 221 |
except ValueError as ve:
|
| 222 |
logger.error(f"ValueError in synthesize_speech: {str(ve)}", exc_info=True)
|
| 223 |
+
processing_time = time.time() - start_time
|
| 224 |
return JSONResponse(
|
| 225 |
status_code=400,
|
| 226 |
+
content={"message": "Invalid input", "details": str(ve), "processing_time_seconds": processing_time}
|
| 227 |
)
|
| 228 |
except Exception as e:
|
| 229 |
logger.error(f"Unexpected error in synthesize_speech: {str(e)}", exc_info=True)
|
|
|
|
| 232 |
"type": type(e).__name__,
|
| 233 |
"traceback": traceback.format_exc()
|
| 234 |
}
|
| 235 |
+
processing_time = time.time() - start_time
|
| 236 |
return JSONResponse(
|
| 237 |
status_code=500,
|
| 238 |
+
content={"message": "An unexpected error occurred during speech synthesis", "details": error_details, "processing_time_seconds": processing_time}
|
| 239 |
)
|
| 240 |
finally:
|
| 241 |
logger.info("Synthesize request completed")
|
| 242 |
|
| 243 |
@app.post("/identify")
|
| 244 |
async def identify_language(request: AudioRequest):
|
| 245 |
+
start_time = time.time()
|
| 246 |
try:
|
| 247 |
input_bytes = base64.b64decode(request.audio)
|
| 248 |
audio_array, sample_rate = extract_audio_from_file(input_bytes)
|
| 249 |
result = identify(audio_array)
|
| 250 |
+
processing_time = time.time() - start_time
|
| 251 |
+
return JSONResponse(content={"language_identification": result, "processing_time_seconds": processing_time})
|
| 252 |
except Exception as e:
|
| 253 |
logger.error(f"Error in identify_language: {str(e)}", exc_info=True)
|
| 254 |
error_details = {
|
| 255 |
"error": str(e),
|
| 256 |
"traceback": traceback.format_exc()
|
| 257 |
}
|
| 258 |
+
processing_time = time.time() - start_time
|
| 259 |
return JSONResponse(
|
| 260 |
status_code=500,
|
| 261 |
+
content={"message": "An error occurred during language identification", "details": error_details, "processing_time_seconds": processing_time}
|
| 262 |
)
|
| 263 |
|
| 264 |
@app.get("/asr_languages")
|
| 265 |
async def get_asr_languages():
|
| 266 |
+
start_time = time.time()
|
| 267 |
try:
|
| 268 |
+
processing_time = time.time() - start_time
|
| 269 |
+
return JSONResponse(content={"languages": ASR_LANGUAGES, "processing_time_seconds": processing_time})
|
| 270 |
except Exception as e:
|
| 271 |
logger.error(f"Error in get_asr_languages: {str(e)}", exc_info=True)
|
| 272 |
error_details = {
|
| 273 |
"error": str(e),
|
| 274 |
"traceback": traceback.format_exc()
|
| 275 |
}
|
| 276 |
+
processing_time = time.time() - start_time
|
| 277 |
return JSONResponse(
|
| 278 |
status_code=500,
|
| 279 |
+
content={"message": "An error occurred while fetching ASR languages", "details": error_details, "processing_time_seconds": processing_time}
|
| 280 |
)
|
| 281 |
|
| 282 |
@app.get("/tts_languages")
|
| 283 |
async def get_tts_languages():
|
| 284 |
+
start_time = time.time()
|
| 285 |
try:
|
| 286 |
+
processing_time = time.time() - start_time
|
| 287 |
+
return JSONResponse(content={"languages": TTS_LANGUAGES, "processing_time_seconds": processing_time})
|
| 288 |
except Exception as e:
|
| 289 |
logger.error(f"Error in get_tts_languages: {str(e)}", exc_info=True)
|
| 290 |
error_details = {
|
| 291 |
"error": str(e),
|
| 292 |
"traceback": traceback.format_exc()
|
| 293 |
}
|
| 294 |
+
processing_time = time.time() - start_time
|
| 295 |
return JSONResponse(
|
| 296 |
status_code=500,
|
| 297 |
+
content={"message": "An error occurred while fetching TTS languages", "details": error_details, "processing_time_seconds": processing_time}
|
| 298 |
)
|