Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,75 +1,132 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
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
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
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 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
)
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
if __name__ == "__main__":
|
75 |
-
|
|
|
|
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)
|