Spaces:
Runtime error
Runtime error
Update app.py
Browse files42 unmodified lines43444546474849505152535433 unmodified lines8889909192939495969798991004 unmodified lines1051061071081091101111121138 unmodified lines1221231241251261271286 unmodified lines135136137138139140141142143192 unmodified lines33633733833934034134242 unmodified linesdevice=self.device)# Load CORRECT audio emotion recognition modelself.emotion_model = pipeline("audio-classification",model="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",device=self.device)[1]# Conversation historyself.conversations = {}33 unmodified linesreturn f"Transcription error: {str(e)}"def recognize_emotion(self, audio_path):"""Recognize emotion from audio using proper audio model"""try:if audio_path is None:return "neutral"result = self.emotion_model(audio_path)return result[0]["label"].lower()except Exception as e:print(f"Emotion recognition error: {e}")return "neutral"4 unmodified linesif text.startswith("Transcription error") or not text.strip():return "I'm sorry, I couldn't understand what you said. Could you please try again?"# Build context-aware promptemotion_prompt = f"[User seems {emotion}] " if emotion != "neutral" else ""prompt = f"{emotion_prompt}User: {text}\nMaya:"# Tokenize with proper attention maskinputs = self.llm_tokenizer(8 unmodified linesoutputs = self.llm_model.generate(input_ids=inputs.input_ids,attention_mask=inputs.attention_mask,max_new_tokens=80,temperature=0.7,do_sample=True,pad_token_id=self.llm_tokenizer.eos_token_id,6 unmodified linesskip_special_tokens=True).strip()# Clean up responseif not response:response = "I understand. Could you tell me more about that?"return response192 unmodified linesoutputs=[transcription_output, audio_output, conversation_history])# Launch the app - FIXED: Removed show_tips parameterif __name__ == "__main__":demo.launch(server_name="0.0.0.0",42 unmodified lines43444546474849505152535433 unmodified lines8889909192939495969798991001011021031041051061071081091101111121131141151164 unmodified lines1211221231241251261271281291301311321331341351361371381391401411421431441458 unmodified lines1541551561571581591606 unmodified lines167168169170171172173174175192 unmodified lines36836937037137237337442 unmodified linesdevice=self.device)# Load WORKING audio emotion recognition modelself.emotion_model = pipeline("audio-classification",model="superb/wav2vec2-base-superb-er",device=self.device)# Conversation historyself.conversations = {}33 unmodified linesreturn f"Transcription error: {str(e)}"def recognize_emotion(self, audio_path):"""Recognize emotion from audio using working model"""try:if audio_path is None:return "neutral"result = self.emotion_model(audio_path)emotion_label = result[0]["label"].lower()# Map SUPERB emotions to common emotionsemotion_mapping = {"ang": "angry","hap": "happy","exc": "excited","sad": "sad","fru": "frustrated","fea": "fearful","sur": "surprised","neu": "neutral","dis": "disgusted"}return emotion_mapping.get(emotion_label, emotion_label)except Exception as e:print(f"Emotion recognition error: {e}")return "neutral"4 unmodified linesif text.startswith("Transcription error") or not text.strip():return "I'm sorry, I couldn't understand what you said. Could you please try again?"# Build context-aware prompt with emotionemotion_responses = {"angry": "I understand you're feeling frustrated. Let me help you with that.","sad": "I can sense you're feeling down. I'm here to listen and support you.","happy": "I love your positive energy! That's wonderful to hear.","excited": "Your enthusiasm is contagious! Tell me more about it.","fearful": "I can hear the concern in your voice. Let's work through this together.","surprised": "That sounds quite unexpected! What happened?","frustrated": "I can tell this is bothering you. Let's see how I can help.","neutral": "I'm listening. Please go on."}emotion_context = emotion_responses.get(emotion, "I'm here to help.")# Simple but effective response generationif len(text.split()) < 3:return f"{emotion_context} Could you tell me more about that?"prompt = f"User ({emotion}): {text}\nMaya (helpful assistant):"# Tokenize with proper attention maskinputs = self.llm_tokenizer(8 unmodified linesoutputs = self.llm_model.generate(input_ids=inputs.input_ids,attention_mask=inputs.attention_mask,max_new_tokens=60,temperature=0.7,do_sample=True,pad_token_id=self.llm_tokenizer.eos_token_id,6 unmodified linesskip_special_tokens=True).strip()# Clean up and add emotion context if response is emptyif not response or len(response) < 5:return emotion_contextreturn response192 unmodified linesoutputs=[transcription_output, audio_output, conversation_history])# Launch the appif __name__ == "__main__":demo.launch(server_name="0.0.0.0",
app.py+45-13