Boltz79 commited on
Commit
8b1154e
·
verified ·
1 Parent(s): 7ff24b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -85
app.py CHANGED
@@ -2,110 +2,74 @@ import gradio as gr
2
  from transformers import pipeline
3
  import torch
4
 
5
- def load_models():
6
- """Load and verify models with error checking"""
7
  try:
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
- print(f"Using device: {device}")
10
-
11
- # Load Whisper for speech recognition
12
- transcriber = pipeline(
13
  "automatic-speech-recognition",
14
- model="openai/whisper-tiny",
15
- device=device
16
  )
17
 
18
- # Load emotion recognition model
19
- emotion_analyzer = pipeline(
20
  "text-classification",
21
- model="j-hartmann/emotion-english-distilroberta-base",
22
- device=device
23
  )
24
 
25
- return transcriber, emotion_analyzer
26
  except Exception as e:
27
- print(f"Error loading models: {str(e)}")
28
  return None, None
29
 
30
- def analyze_audio(audio_path):
31
- """
32
- Analyze audio for emotional content with detailed output
33
- """
34
- if audio_path is None:
35
- return "Please provide audio", "No audio detected"
36
-
37
  try:
38
- # Load models
39
- transcriber, emotion_analyzer = load_models()
40
- if transcriber is None or emotion_analyzer is None:
41
- return "Error loading models", "Model initialization failed"
42
-
43
- # Transcribe speech
44
- try:
45
- result = transcriber(audio_path)
46
- text = result["text"]
47
- if not text.strip():
48
- return "No speech detected", "Empty transcription"
49
- print(f"Transcribed text: {text}") # Debug output
50
- except Exception as e:
51
- return f"Transcription error: {str(e)}", "Failed to process audio"
52
 
 
 
53
  # Analyze emotion
54
- try:
55
- emotion_result = emotion_analyzer(text)[0]
56
- emotion = emotion_result["label"].title() # Capitalize emotion
57
- confidence = f"{emotion_result['score']:.2%}"
58
-
59
- # Map technical emotion labels to more natural descriptions
60
- emotion_mapping = {
61
- "Joy": "Happy/Joyful",
62
- "Sadness": "Sad/Melancholic",
63
- "Anger": "Angry/Frustrated",
64
- "Fear": "Anxious/Fearful",
65
- "Surprise": "Surprised/Astonished",
66
- "Love": "Warm/Affectionate",
67
- "Neutral": "Neutral/Calm"
68
- }
69
-
70
- display_emotion = emotion_mapping.get(emotion, emotion)
71
- return display_emotion, confidence
72
-
73
- except Exception as e:
74
- return f"Emotion analysis error: {str(e)}", "Analysis failed"
75
-
76
  except Exception as e:
77
- return f"Unexpected error: {str(e)}", "Process failed"
 
78
 
79
- # Create interface with better labeling
80
  interface = gr.Interface(
81
- fn=analyze_audio,
82
- inputs=gr.Audio(
83
- sources=["microphone", "upload"],
84
- type="filepath",
85
- label="Record or Upload Audio"
86
- ),
 
 
87
  outputs=[
88
- gr.Textbox(label="Detected Emotion"),
89
- gr.Textbox(label="Confidence Score")
90
  ],
91
  title="Speech Emotion Analyzer",
92
- description="""
93
- This tool analyzes the emotional tone of speech, detecting emotions like:
94
- - Happy/Joyful
95
- - Sad/Melancholic
96
- - Angry/Frustrated
97
- - Anxious/Fearful
98
- - Surprised/Astonished
99
- - Warm/Affectionate
100
- - Neutral/Calm
101
- """,
102
- theme=gr.themes.Base()
103
  )
104
 
105
  if __name__ == "__main__":
106
- interface.launch(
107
- debug=True,
108
- server_name="0.0.0.0",
109
- server_port=7860,
110
- share=True
111
- )
 
2
  from transformers import pipeline
3
  import torch
4
 
5
+ def create_analyzers():
6
+ """Initialize speech and emotion analyzers"""
7
  try:
8
+ # Use tiny whisper model for speed and reliability
9
+ speech_recognizer = pipeline(
 
 
 
10
  "automatic-speech-recognition",
11
+ model="openai/whisper-tiny.en",
12
+ chunk_length_s=30
13
  )
14
 
15
+ # Use smaller emotion classifier
16
+ emotion_classifier = pipeline(
17
  "text-classification",
18
+ model="SamLowe/roberta-base-go_emotions",
19
+ top_k=1
20
  )
21
 
22
+ return speech_recognizer, emotion_classifier
23
  except Exception as e:
24
+ print(f"Model loading error: {e}")
25
  return None, None
26
 
27
+ def analyze_tone(audio_file):
28
+ """Analyze the emotional tone of speech"""
29
+ if audio_file is None:
30
+ return "No input", "N/A"
31
+
 
 
32
  try:
33
+ # Get models
34
+ speech_recognizer, emotion_classifier = create_analyzers()
35
+
36
+ # Transcribe audio
37
+ transcription = speech_recognizer(audio_file)
38
+ text = transcription["text"]
39
+
40
+ if not text.strip():
41
+ return "No speech detected", "N/A"
 
 
 
 
 
42
 
43
+ print(f"Transcribed text: {text}") # For debugging
44
+
45
  # Analyze emotion
46
+ result = emotion_classifier(text)[0][0]
47
+ emotion = result['label'].replace('_', ' ').title()
48
+ confidence = f"{result['score']:.1%}"
49
+
50
+ return emotion, confidence
51
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  except Exception as e:
53
+ print(f"Analysis error: {e}")
54
+ return f"Error: {str(e)}", "N/A"
55
 
56
+ # Create minimal interface
57
  interface = gr.Interface(
58
+ fn=analyze_tone,
59
+ inputs=[
60
+ gr.Audio(
61
+ sources=["microphone", "upload"],
62
+ type="filepath",
63
+ label="Audio Input"
64
+ )
65
+ ],
66
  outputs=[
67
+ gr.Textbox(label="Emotion"),
68
+ gr.Textbox(label="Confidence")
69
  ],
70
  title="Speech Emotion Analyzer",
71
+ description="Record or upload audio to detect the emotional tone.",
 
 
 
 
 
 
 
 
 
 
72
  )
73
 
74
  if __name__ == "__main__":
75
+ interface.launch(server_name="0.0.0.0", share=True)