Boltz79 commited on
Commit
defc213
·
verified ·
1 Parent(s): 34c835a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -64
app.py CHANGED
@@ -1,75 +1,132 @@
1
  import gradio as gr
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)
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ from textblob import TextBlob
4
+ import speech_recognition as sr
5
 
6
+ class SentimentAnalyzer:
7
+ def __init__(self):
8
+ self.recognizer = sr.Recognizer()
9
+
10
+ def audio_to_text(self, audio):
11
+ """Convert audio to text using speech recognition"""
12
+ try:
13
+ # Get audio data from Gradio input
14
+ sample_rate, audio_data = audio
15
+
16
+ # Convert audio data to audio file format that speech_recognition can use
17
+ import io
18
+ import scipy.io.wavfile as wav
19
+ byte_io = io.BytesIO()
20
+ wav.write(byte_io, sample_rate, audio_data.astype(np.int16))
21
+ byte_io.seek(0)
22
+
23
+ # Use speech recognition
24
+ with sr.AudioFile(byte_io) as source:
25
+ audio_data = self.recognizer.record(source)
26
+ text = self.recognizer.recognize_google(audio_data)
27
+ return text
28
+ except Exception as e:
29
+ return f"Error in speech recognition: {str(e)}"
30
+
31
+ def analyze_sentiment(self, text):
32
+ """Analyze sentiment using TextBlob"""
33
+ try:
34
+ blob = TextBlob(text)
35
+ # Get polarity (-1 to 1) and subjectivity (0 to 1)
36
+ polarity = blob.sentiment.polarity
37
+ subjectivity = blob.sentiment.subjectivity
38
+
39
+ # Determine sentiment category
40
+ if polarity > 0:
41
+ sentiment = "Positive"
42
+ elif polarity < 0:
43
+ sentiment = "Negative"
44
+ else:
45
+ sentiment = "Neutral"
46
+
47
+ # Format results
48
+ results_text = f"""
49
+ Detected Text: "{text}"
50
 
51
+ Analysis Results:
52
+ - Overall Sentiment: {sentiment}
53
+ - Polarity Score: {polarity:.2f} (-1 to +1)
54
+ - Subjectivity Score: {subjectivity:.2f} (0 to 1)
55
+ """
56
+
57
+ # Prepare plot data
58
+ plot_data = {
59
+ "labels": ["Polarity", "Subjectivity"],
60
+ "values": [polarity * 100, subjectivity * 100] # Convert to percentage for visualization
61
+ }
62
+
63
+ return results_text, plot_data
64
+
65
+ except Exception as e:
66
+ return f"Error in sentiment analysis: {str(e)}", None
67
+
68
+ def create_interface():
69
+ analyzer = SentimentAnalyzer()
70
 
71
+ def process_audio(audio):
72
+ if audio is None:
73
+ return "Please provide an audio input.", None
74
 
75
+ # Convert audio to text
76
+ text = analyzer.audio_to_text(audio)
77
+ if text.startswith("Error"):
78
+ return text, None
 
 
 
 
79
 
80
+ # Analyze sentiment
81
+ return analyzer.analyze_sentiment(text)
82
+
83
+ # Create Gradio interface
84
+ with gr.Blocks() as interface:
85
+ gr.Markdown("# 🎤 Speech Sentiment Analysis")
86
+ gr.Markdown("""
87
+ Speak or upload an audio file to analyze its emotional content.
88
+ The system will convert speech to text and analyze the sentiment.
89
+ """)
90
 
91
+ with gr.Row():
92
+ with gr.Column():
93
+ audio_input = gr.Audio(
94
+ label="Upload or Record Audio",
95
+ type="numpy",
96
+ sources=["microphone", "upload"]
97
+ )
98
+ analyze_btn = gr.Button("Analyze Sentiment")
99
+
100
+ with gr.Column():
101
+ output_text = gr.Textbox(
102
+ label="Analysis Results",
103
+ lines=8
104
+ )
105
+ output_plot = gr.BarPlot(
106
+ title="Sentiment Scores",
107
+ x_title="Metrics",
108
+ y_title="Score (%)"
109
+ )
110
 
111
+ analyze_btn.click(
112
+ fn=process_audio,
113
+ inputs=[audio_input],
114
+ outputs=[output_text, output_plot]
 
 
 
 
 
 
 
 
115
  )
116
+
117
+ gr.Markdown("""
118
+ ### How to Use:
119
+ 1. Click the microphone button to record or upload an audio file
120
+ 2. Click "Analyze Sentiment" to process
121
+ 3. View the results showing:
122
+ - Detected text from speech
123
+ - Overall sentiment (Positive/Negative/Neutral)
124
+ - Polarity score (-100% to +100%)
125
+ - Subjectivity score (0% to 100%)
126
+ """)
127
+
128
+ return interface
129
 
130
  if __name__ == "__main__":
131
+ demo = create_interface()
132
+ demo.launch(share=True)