Samfredoly commited on
Commit
bd29a08
·
verified ·
1 Parent(s): 7023ff7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -38
app.py CHANGED
@@ -298,42 +298,43 @@ def find_matching_filename(transcribed_filename: str, reference_map: Dict[str, s
298
  log_message(f"⚠️ No matching filename found for: {transcribed_filename}", "WARNING")
299
  return None
300
 
301
- def check_ffmpeg() -> bool:
302
- """Check if ffmpeg is installed"""
303
- try:
304
- subprocess.run(["ffmpeg", "-version"], capture_output=True, check=True)
305
- return True
306
- except (FileNotFoundError, subprocess.CalledProcessError):
307
- return False
308
-
309
  def transcribe_audio(wav_path: str) -> Optional[Dict[str, Any]]:
310
- """Transcribe audio file using Whisper."""
311
  log_message(f"🎤 Transcribing audio file: {wav_path}", "INFO")
312
 
313
  try:
314
- # Check ffmpeg first
315
- if not check_ffmpeg():
316
- log_message("❌ ffmpeg not found. Install with: pip install ffmpeg-python or use system package manager", "ERROR")
317
- log_message(" Windows (Chocolatey): choco install ffmpeg", "ERROR")
318
- log_message(" Windows (Scoop): scoop install ffmpeg", "ERROR")
319
- log_message(" Linux (Ubuntu): sudo apt-get install ffmpeg", "ERROR")
320
- log_message(" macOS (Homebrew): brew install ffmpeg", "ERROR")
321
- return None
322
-
323
- import whisper
324
-
325
- # Load model
326
- log_message(f"Loading Whisper {WHISPER_MODEL} model...", "INFO")
327
- model = whisper.load_model(WHISPER_MODEL)
328
 
329
  # Transcribe
330
- result = model.transcribe(wav_path)
 
 
 
 
 
 
 
331
 
332
  log_message(f"✅ Successfully transcribed: {wav_path}", "INFO")
333
- return result
334
 
335
- except ImportError:
336
- log_message("❌ Whisper not installed. Install with: pip install openai-whisper", "ERROR")
 
 
337
  return None
338
  except Exception as e:
339
  log_message(f"❌ Failed to transcribe {wav_path}: {str(e)}", "ERROR")
@@ -680,17 +681,12 @@ async def startup_event():
680
  """Auto-start processing when server starts"""
681
  log_message("🚀 Server startup: Checking dependencies...", "INFO")
682
 
683
- # Check ffmpeg
684
- if not check_ffmpeg():
685
- log_message("⚠️ WARNING: ffmpeg is not installed!", "WARNING")
686
- log_message(" Whisper requires ffmpeg for audio processing", "WARNING")
687
- log_message(" Install with one of the following:", "WARNING")
688
- log_message(" - Windows (Chocolatey): choco install ffmpeg", "WARNING")
689
- log_message(" - Windows (Scoop): scoop install ffmpeg", "WARNING")
690
- log_message(" - Linux (Ubuntu): sudo apt-get install ffmpeg", "WARNING")
691
- log_message(" - macOS (Homebrew): brew install ffmpeg", "WARNING")
692
- else:
693
- log_message("✅ ffmpeg found", "INFO")
694
 
695
  log_message("🚀 Server startup: Auto-starting processing loop", "INFO")
696
 
 
298
  log_message(f"⚠️ No matching filename found for: {transcribed_filename}", "WARNING")
299
  return None
300
 
 
 
 
 
 
 
 
 
301
  def transcribe_audio(wav_path: str) -> Optional[Dict[str, Any]]:
302
+ """Transcribe audio file using Whisper from Transformers."""
303
  log_message(f"🎤 Transcribing audio file: {wav_path}", "INFO")
304
 
305
  try:
306
+ from transformers import pipeline
307
+ import librosa
308
+
309
+ # Load audio with librosa
310
+ log_message(f"Loading audio file: {wav_path}", "INFO")
311
+ audio, sr = librosa.load(wav_path, sr=16000)
312
+
313
+ # Initialize Whisper pipeline
314
+ log_message(f"Loading Whisper {WHISPER_MODEL} model from Transformers...", "INFO")
315
+ pipe = pipeline(
316
+ "automatic-speech-recognition",
317
+ model=f"openai/whisper-{WHISPER_MODEL}",
318
+ device=0 if __import__('torch').cuda.is_available() else -1 # GPU if available, else CPU
319
+ )
320
 
321
  # Transcribe
322
+ log_message("Transcribing audio...", "INFO")
323
+ result = pipe(audio)
324
+
325
+ # Format result to match openai-whisper format
326
+ formatted_result = {
327
+ "text": result["text"],
328
+ "segments": [{"text": result["text"]}]
329
+ }
330
 
331
  log_message(f"✅ Successfully transcribed: {wav_path}", "INFO")
332
+ return formatted_result
333
 
334
+ except ImportError as e:
335
+ missing_lib = str(e)
336
+ log_message(f"❌ Missing library. Install with: pip install transformers librosa torch torchaudio", "ERROR")
337
+ log_message(f" Error: {missing_lib}", "ERROR")
338
  return None
339
  except Exception as e:
340
  log_message(f"❌ Failed to transcribe {wav_path}: {str(e)}", "ERROR")
 
681
  """Auto-start processing when server starts"""
682
  log_message("🚀 Server startup: Checking dependencies...", "INFO")
683
 
684
+ try:
685
+ import transformers
686
+ log_message(" Transformers found", "INFO")
687
+ except ImportError:
688
+ log_message("⚠️ WARNING: Transformers not installed!", "WARNING")
689
+ log_message(" Install with: pip install transformers librosa torch torchaudio", "WARNING")
 
 
 
 
 
690
 
691
  log_message("🚀 Server startup: Auto-starting processing loop", "INFO")
692