app.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import tempfile
  2. import os
  3. import numpy as np
  4. import time
  5. import logging
  6. import os
  7. import sys
  8. import asyncio
  9. from threading import Lock
  10. from fastapi import FastAPI, UploadFile, File, WebSocket, WebSocketDisconnect, HTTPException
  11. from faster_whisper import WhisperModel
  12. def env_bool(name: str, default: bool = False) -> bool:
  13. return os.getenv(name, str(default)).lower() in ("1", "true", "yes", "on")
  14. LOG_LEVEL = os.getenv("WHISPER_LOG_LEVEL", "INFO").upper()
  15. VAD_ENABLED = env_bool("WHISPER_VAD", True)
  16. MODEL_SIZE = str(os.getenv("MODEL_SIZE", "small").lower())
  17. MIN_AUDIO_SEC = float(os.getenv("MIN_AUDIO_SEC", "2.0"))
  18. MAX_AUDIO_SEC = float(os.getenv("MAX_AUDIO_SEC", "10.0"))
  19. logging.basicConfig(
  20. level=getattr(logging, LOG_LEVEL, logging.INFO),
  21. format="%(asctime)s [%(levelname)s] [%(name)s] %(message)s",
  22. stream=sys.stdout,
  23. force=True
  24. )
  25. logger = logging.getLogger("whisper-ws")
  26. logger.setLevel(getattr(logging, LOG_LEVEL, logging.INFO))
  27. logger.info(f"Log level = {LOG_LEVEL}")
  28. logger.info(f"VAD enabled = {VAD_ENABLED}")
  29. app = FastAPI()
  30. # =========================
  31. # Whisper model (shared)
  32. # =========================
  33. model = WhisperModel(MODEL_SIZE, device="cuda", compute_type="float32")
  34. model_lock = Lock()
  35. def transcribe_audio(audio):
  36. # Consommer le générateur sous le verrou, hors de la boucle asyncio.
  37. with model_lock:
  38. segments, info = model.transcribe(
  39. audio, language="fr", vad_filter=VAD_ENABLED,
  40. beam_size=1, condition_on_previous_text=False,
  41. )
  42. return " ".join(seg.text for seg in segments).strip(), info.language
  43. @app.get("/healthz")
  44. def healthz():
  45. return {"status": "ready", "model": MODEL_SIZE}
  46. # =========================
  47. # REST API (Swagger / tests)
  48. # =========================
  49. @app.post("/transcribe")
  50. def transcribe(file: UploadFile = File(...)):
  51. # Discord envoie au maximum 20 secondes de WAV stéréo 48 kHz.
  52. with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
  53. tmp_path = tmp.name
  54. try:
  55. data = file.file.read(8 * 1024 * 1024 + 1)
  56. if len(data) > 8 * 1024 * 1024:
  57. raise HTTPException(413, "Audio trop volumineux")
  58. tmp.write(data)
  59. tmp.flush()
  60. text, language = transcribe_audio(tmp_path)
  61. return {"text": text, "language": language}
  62. finally:
  63. os.remove(tmp_path)
  64. # =========================
  65. # WebSocket STT (streaming)
  66. # =========================
  67. SAMPLE_RATE = 16000
  68. @app.websocket("/ws/transcribe")
  69. async def ws_transcribe(ws: WebSocket):
  70. await ws.accept()
  71. logger.info("WebSocket connecté !")
  72. try:
  73. while True:
  74. data = await ws.receive_bytes()
  75. logger.debug(f"Chunk reçu: {len(data)} bytes")
  76. pcm = np.frombuffer(data, dtype=np.int16).astype(np.float32) / 32768.0
  77. energy = float(np.abs(pcm).mean())
  78. logger.debug(f"Énergie audio = {energy:.6f}")
  79. # silence gate léger
  80. if np.abs(pcm).mean() < 0.00005:
  81. logger.debug("Chunk silencieux ignoré")
  82. continue
  83. duration = len(pcm) / SAMPLE_RATE
  84. logger.info(f"Transcription chunk {duration:.2f}s")
  85. text, _ = await asyncio.to_thread(transcribe_audio, pcm)
  86. logger.info(f"Texte: '{text}'")
  87. if text:
  88. await ws.send_json({
  89. "type": "final",
  90. "text": text
  91. })
  92. except WebSocketDisconnect:
  93. logger.info("WebSocket déconnecté")
  94. except Exception:
  95. logger.exception("Erreur WebSocket")
  96. await ws.close()