main.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import os
  2. import logging
  3. from fastapi import (
  4. FastAPI,
  5. Request,
  6. Depends,
  7. HTTPException,
  8. status,
  9. UploadFile,
  10. File,
  11. Form,
  12. )
  13. from fastapi.middleware.cors import CORSMiddleware
  14. from faster_whisper import WhisperModel
  15. from constants import ERROR_MESSAGES
  16. from utils.utils import (
  17. decode_token,
  18. get_current_user,
  19. get_verified_user,
  20. get_admin_user,
  21. )
  22. from utils.misc import calculate_sha256
  23. from config import (
  24. SRC_LOG_LEVELS,
  25. CACHE_DIR,
  26. UPLOAD_DIR,
  27. WHISPER_MODEL,
  28. WHISPER_MODEL_DIR,
  29. DEVICE_TYPE,
  30. )
  31. log = logging.getLogger(__name__)
  32. log.setLevel(SRC_LOG_LEVELS["AUDIO"])
  33. app = FastAPI()
  34. app.add_middleware(
  35. CORSMiddleware,
  36. allow_origins=["*"],
  37. allow_credentials=True,
  38. allow_methods=["*"],
  39. allow_headers=["*"],
  40. )
  41. # setting device type for whisper model
  42. whisper_device_type = DEVICE_TYPE if DEVICE_TYPE and DEVICE_TYPE == "cuda" else "cpu"
  43. log.info(f"whisper_device_type: {whisper_device_type}")
  44. @app.post("/transcribe")
  45. def transcribe(
  46. file: UploadFile = File(...),
  47. user=Depends(get_current_user),
  48. ):
  49. log.info(f"file.content_type: {file.content_type}")
  50. if file.content_type not in ["audio/mpeg", "audio/wav"]:
  51. raise HTTPException(
  52. status_code=status.HTTP_400_BAD_REQUEST,
  53. detail=ERROR_MESSAGES.FILE_NOT_SUPPORTED,
  54. )
  55. try:
  56. filename = file.filename
  57. file_path = f"{UPLOAD_DIR}/{filename}"
  58. contents = file.file.read()
  59. with open(file_path, "wb") as f:
  60. f.write(contents)
  61. f.close()
  62. model = WhisperModel(
  63. WHISPER_MODEL,
  64. device=whisper_device_type,
  65. compute_type="int8",
  66. download_root=WHISPER_MODEL_DIR,
  67. )
  68. segments, info = model.transcribe(file_path, beam_size=5)
  69. log.info(
  70. "Detected language '%s' with probability %f"
  71. % (info.language, info.language_probability)
  72. )
  73. transcript = "".join([segment.text for segment in list(segments)])
  74. return {"text": transcript.strip()}
  75. except Exception as e:
  76. log.exception(e)
  77. raise HTTPException(
  78. status_code=status.HTTP_400_BAD_REQUEST,
  79. detail=ERROR_MESSAGES.DEFAULT(e),
  80. )