JenishMakwana commited on
Commit
9fd899f
·
1 Parent(s): 937ee31

feat: add VoiceService with audio transcription using FFmpeg and Whisper model integration

Browse files
backend/app/services/voice_service.py CHANGED
@@ -9,10 +9,28 @@ import torch
9
  class VoiceService:
10
  def transcribe(self, audio_path: str) -> str:
11
  from .model_manager import speech_model_manager
 
 
 
12
 
13
  try:
14
- # Load audio using librosa at 16000 Hz
15
- audio, sr = librosa.load(audio_path, sr=16000)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  processor = speech_model_manager.get_stt_processor()
18
  model = speech_model_manager.get_stt_model()
@@ -30,6 +48,7 @@ class VoiceService:
30
  return transcription.strip()
31
  except Exception as e:
32
  print(f"Transcription error: {e}")
 
33
  return ""
34
 
35
  def create_voice_router(self):
 
9
  class VoiceService:
10
  def transcribe(self, audio_path: str) -> str:
11
  from .model_manager import speech_model_manager
12
+ import traceback
13
+ import subprocess
14
+ import os
15
 
16
  try:
17
+ # Explicitly convert webm to wav using ffmpeg since librosa sometimes fails on webm
18
+ wav_path = audio_path + ".wav"
19
+ try:
20
+ subprocess.run(
21
+ ["ffmpeg", "-y", "-i", audio_path, "-ar", "16000", "-ac", "1", wav_path],
22
+ check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
23
+ )
24
+ except subprocess.CalledProcessError as e:
25
+ print(f"FFmpeg failed: {e.stderr.decode()}")
26
+ return ""
27
+
28
+ # Load audio using librosa at 16000 Hz from the guaranteed WAV file
29
+ audio, sr = librosa.load(wav_path, sr=16000)
30
+
31
+ # Cleanup wav
32
+ if os.path.exists(wav_path):
33
+ os.remove(wav_path)
34
 
35
  processor = speech_model_manager.get_stt_processor()
36
  model = speech_model_manager.get_stt_model()
 
48
  return transcription.strip()
49
  except Exception as e:
50
  print(f"Transcription error: {e}")
51
+ traceback.print_exc()
52
  return ""
53
 
54
  def create_voice_router(self):