# emotion_detector.py import os import torchaudio import tempfile from speechbrain.inference.classifiers import AudioClassifier # Set HF_HOME to writable temp directory os.environ["HF_HOME"] = os.path.join(tempfile.gettempdir(), "hf_cache") # Also set Hugging Face cache dir explicitly (optional but helps) os.environ["HF_DATASETS_CACHE"] = os.environ["HF_HOME"] os.environ["TRANSFORMERS_CACHE"] = os.environ["HF_HOME"] os.environ["HUGGINGFACE_HUB_CACHE"] = os.environ["HF_HOME"] # Use a temp directory for SpeechBrain model saving model_dir = os.path.join(tempfile.gettempdir(), "emotion_model") # Load pretrained model classifier = AudioClassifier.from_hparams( source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP", savedir=model_dir ) EMOTION_EMOJIS = { "angry": "😠", "happy": "😄", "neutral": "😐", "sad": "😢", "fearful": "😨" } def detect_emotion(audio_path): signal, fs = torchaudio.load(audio_path) prediction = classifier.classify_file(audio_path) emotion = prediction[3] # Predicted label emoji = EMOTION_EMOJIS.get(emotion.lower(), "❓") return emotion, emoji