Spaces:
Edmond98
/
Running on A100

Edmond7 commited on
Commit
9104ce6
1 Parent(s): 9575eea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -67,6 +67,9 @@ class TTSRequest(BaseModel):
67
  language: str
68
  speed: float
69
 
 
 
 
70
  async def get_api_key(api_key_header: str = Security(api_key_header)):
71
  if api_key_header == API_KEY:
72
  return api_key_header
@@ -321,12 +324,26 @@ async def identify_language_file(file: UploadFile = File(...), api_key: APIKey =
321
  content={"message": "An error occurred during language identification", "details": error_details, "processing_time_seconds": processing_time}
322
  )
323
 
324
- @app.get("/asr_languages")
325
- async def get_asr_languages(api_key: APIKey = Depends(get_api_key)):
326
  start_time = time.time()
327
  try:
 
 
 
 
 
 
 
328
  processing_time = time.time() - start_time
329
- return JSONResponse(content={"languages": ASR_LANGUAGES, "processing_time_seconds": processing_time})
 
 
 
 
 
 
 
330
  except Exception as e:
331
  logger.error(f"Error in get_asr_languages: {str(e)}", exc_info=True)
332
  error_details = {
@@ -339,12 +356,23 @@ async def get_asr_languages(api_key: APIKey = Depends(get_api_key)):
339
  content={"message": "An error occurred while fetching ASR languages", "details": error_details, "processing_time_seconds": processing_time}
340
  )
341
 
342
- @app.get("/tts_languages")
343
- async def get_tts_languages(api_key: APIKey = Depends(get_api_key)):
344
  start_time = time.time()
345
  try:
 
 
 
 
 
 
 
 
346
  processing_time = time.time() - start_time
347
- return JSONResponse(content={"languages": TTS_LANGUAGES, "processing_time_seconds": processing_time})
 
 
 
348
  except Exception as e:
349
  logger.error(f"Error in get_tts_languages: {str(e)}", exc_info=True)
350
  error_details = {
@@ -355,4 +383,4 @@ async def get_tts_languages(api_key: APIKey = Depends(get_api_key)):
355
  return JSONResponse(
356
  status_code=500,
357
  content={"message": "An error occurred while fetching TTS languages", "details": error_details, "processing_time_seconds": processing_time}
358
- )
 
67
  language: str
68
  speed: float
69
 
70
+ class LanguageRequest(BaseModel):
71
+ language: str
72
+
73
  async def get_api_key(api_key_header: str = Security(api_key_header)):
74
  if api_key_header == API_KEY:
75
  return api_key_header
 
324
  content={"message": "An error occurred during language identification", "details": error_details, "processing_time_seconds": processing_time}
325
  )
326
 
327
+ @app.post("/asr_languages")
328
+ async def get_asr_languages(request: LanguageRequest, api_key: APIKey = Depends(get_api_key)):
329
  start_time = time.time()
330
  try:
331
+ if request.language.lower() not in [lang.lower() for lang in ASR_LANGUAGES]:
332
+ raise ValueError(f"Unsupported language: {request.language}")
333
+
334
+ matching_languages = [lang for lang in ASR_LANGUAGES if lang.lower().startswith(request.language.lower())]
335
+ processing_time = time.time() - start_time
336
+ return JSONResponse
337
+ matching_languages = [lang for lang in ASR_LANGUAGES if lang.lower().startswith(request.language.lower())]
338
  processing_time = time.time() - start_time
339
+ return JSONResponse(content={"languages": matching_languages, "processing_time_seconds": processing_time})
340
+ except ValueError as ve:
341
+ logger.error(f"ValueError in get_asr_languages: {str(ve)}", exc_info=True)
342
+ processing_time = time.time() - start_time
343
+ return JSONResponse(
344
+ status_code=400,
345
+ content={"message": "Invalid input", "details": str(ve), "processing_time_seconds": processing_time}
346
+ )
347
  except Exception as e:
348
  logger.error(f"Error in get_asr_languages: {str(e)}", exc_info=True)
349
  error_details = {
 
356
  content={"message": "An error occurred while fetching ASR languages", "details": error_details, "processing_time_seconds": processing_time}
357
  )
358
 
359
+ @app.post("/tts_languages")
360
+ async def get_tts_languages(request: LanguageRequest, api_key: APIKey = Depends(get_api_key)):
361
  start_time = time.time()
362
  try:
363
+ if request.language.lower() not in [lang.lower() for lang in TTS_LANGUAGES]:
364
+ raise ValueError(f"Unsupported language: {request.language}")
365
+
366
+ matching_languages = [lang for lang in TTS_LANGUAGES if lang.lower().startswith(request.language.lower())]
367
+ processing_time = time.time() - start_time
368
+ return JSONResponse(content={"languages": matching_languages, "processing_time_seconds": processing_time})
369
+ except ValueError as ve:
370
+ logger.error(f"ValueError in get_tts_languages: {str(ve)}", exc_info=True)
371
  processing_time = time.time() - start_time
372
+ return JSONResponse(
373
+ status_code=400,
374
+ content={"message": "Invalid input", "details": str(ve), "processing_time_seconds": processing_time}
375
+ )
376
  except Exception as e:
377
  logger.error(f"Error in get_tts_languages: {str(e)}", exc_info=True)
378
  error_details = {
 
383
  return JSONResponse(
384
  status_code=500,
385
  content={"message": "An error occurred while fetching TTS languages", "details": error_details, "processing_time_seconds": processing_time}
386
+ )