main.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 SRC_LOG_LEVELS, CACHE_DIR, UPLOAD_DIR, WHISPER_MODEL, WHISPER_MODEL_DIR
  24. log = logging.getLogger(__name__)
  25. log.setLevel(SRC_LOG_LEVELS["AUDIO"])
  26. app = FastAPI()
  27. app.add_middleware(
  28. CORSMiddleware,
  29. allow_origins=["*"],
  30. allow_credentials=True,
  31. allow_methods=["*"],
  32. allow_headers=["*"],
  33. )
  34. @app.post("/transcribe")
  35. def transcribe(
  36. file: UploadFile = File(...),
  37. user=Depends(get_current_user),
  38. ):
  39. log.info(f"file.content_type: {file.content_type}")
  40. if file.content_type not in ["audio/mpeg", "audio/wav"]:
  41. raise HTTPException(
  42. status_code=status.HTTP_400_BAD_REQUEST,
  43. detail=ERROR_MESSAGES.FILE_NOT_SUPPORTED,
  44. )
  45. try:
  46. filename = file.filename
  47. file_path = f"{UPLOAD_DIR}/{filename}"
  48. contents = file.file.read()
  49. with open(file_path, "wb") as f:
  50. f.write(contents)
  51. f.close()
  52. model = WhisperModel(
  53. WHISPER_MODEL,
  54. device="auto",
  55. compute_type="int8",
  56. download_root=WHISPER_MODEL_DIR,
  57. )
  58. segments, info = model.transcribe(file_path, beam_size=5)
  59. log.info(
  60. "Detected language '%s' with probability %f"
  61. % (info.language, info.language_probability)
  62. )
  63. transcript = "".join([segment.text for segment in list(segments)])
  64. return {"text": transcript.strip()}
  65. except Exception as e:
  66. log.exception(e)
  67. raise HTTPException(
  68. status_code=status.HTTP_400_BAD_REQUEST,
  69. detail=ERROR_MESSAGES.DEFAULT(e),
  70. )