import whisper import os # --- CONFIGURATION --- # "base" is a good balance of speed and accuracy. # Use "tiny" for super speed, or "small" for better accuracy. WHISPER_MODEL_SIZE = "base" class AudioAnalyzer: def __init__(self): print(f"Loading Whisper AI ({WHISPER_MODEL_SIZE})...") self.model = whisper.load_model(WHISPER_MODEL_SIZE) print("Whisper AI loaded successfully.") def process_audio(self, audio_path): """ Takes an audio file path, converts to text, and extracts basic features. """ if not os.path.exists(audio_path): return {"error": "File not found"} print(f"Transcribing {audio_path}...") # 1. TRANSCRIBE (Speech -> Text) result = self.model.transcribe(audio_path, language="en", fp16=False) transcribed_text = result["text"].strip() # 2. EXTRACT META-FEATURES (Bonus Signals) # We can detect 'slow speech' (Psychomotor retardation) common in Depression duration_seconds = result['segments'][-1]['end'] if result['segments'] else 0 word_count = len(transcribed_text.split()) wpm = (word_count / duration_seconds) * 60 if duration_seconds > 0 else 0 # Interpret Rate of Speech speech_rate_label = "Normal" if wpm < 110: speech_rate_label = "Slow (Possible Depression Indicator)" elif wpm > 160: speech_rate_label = "Fast (Possible Anxiety/ADHD Indicator)" return { "text": transcribed_text, "wpm": round(wpm, 1), "rate_label": speech_rate_label } # --- TEST IT --- if __name__ == "__main__": # You need a dummy audio file to test this. # If on Colab, upload a file named 'test_audio.mp3' analyzer = AudioAnalyzer() # Example usage (Uncomment if you have a file) # output = analyzer.process_audio("test_audio.mp3") # print(output)