Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,116 +1,78 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
-
import numpy as np
|
4 |
|
5 |
-
def
|
6 |
-
"""Initialize the speech-to-text and
|
7 |
try:
|
8 |
-
# Load
|
9 |
transcriber = pipeline(
|
10 |
"automatic-speech-recognition",
|
11 |
-
model="openai/whisper-small"
|
12 |
-
max_new_tokens=128
|
13 |
)
|
14 |
|
15 |
-
# Load RoBERTa
|
16 |
-
|
17 |
"sentiment-analysis",
|
18 |
model="cardiffnlp/twitter-roberta-base-sentiment-latest"
|
19 |
)
|
20 |
|
21 |
-
return transcriber,
|
22 |
-
|
23 |
except Exception as e:
|
24 |
-
raise RuntimeError(f"
|
25 |
|
26 |
-
def
|
27 |
-
"""
|
28 |
-
Analyze speech for transcription and emotional content.
|
29 |
-
Returns both the transcription and detailed sentiment analysis.
|
30 |
-
"""
|
31 |
try:
|
32 |
-
transcriber,
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
'LABEL_0': 'Negative',
|
43 |
'LABEL_1': 'Neutral',
|
44 |
'LABEL_2': 'Positive'
|
45 |
}
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
confidence = sentiment_result['score']
|
50 |
-
|
51 |
-
# Analyze sentiment of smaller chunks for longer texts
|
52 |
-
if len(transcription.split()) > 50:
|
53 |
-
# Split into sentences or chunks
|
54 |
-
chunks = transcription.split('.')
|
55 |
-
chunk_sentiments = [sentiment_model(chunk)[0] for chunk in chunks if len(chunk.strip()) > 0]
|
56 |
-
|
57 |
-
# Calculate average sentiment
|
58 |
-
avg_sentiment = np.mean([s['score'] for s in chunk_sentiments])
|
59 |
-
sentiment_variation = np.std([s['score'] for s in chunk_sentiments])
|
60 |
-
|
61 |
-
detailed_analysis = (
|
62 |
-
f"Overall Sentiment: {sentiment} ({confidence:.2%})\n"
|
63 |
-
f"Sentiment Stability: {1 - sentiment_variation:.2%}\n"
|
64 |
-
f"Text chunks analyzed: {len(chunk_sentiments)}"
|
65 |
-
)
|
66 |
-
else:
|
67 |
-
detailed_analysis = f"Sentiment: {sentiment} ({confidence:.2%})"
|
68 |
|
69 |
return {
|
70 |
-
"
|
71 |
-
"
|
72 |
-
"analysis": detailed_analysis
|
73 |
}
|
74 |
|
75 |
except Exception as e:
|
76 |
return {
|
77 |
-
"
|
78 |
-
"
|
79 |
-
"analysis": "Analysis failed"
|
80 |
}
|
81 |
|
82 |
def create_interface():
|
83 |
-
"""Create
|
84 |
return gr.Interface(
|
85 |
-
fn=
|
86 |
inputs=gr.Audio(
|
87 |
sources=["microphone", "upload"],
|
88 |
type="filepath",
|
89 |
-
label="
|
90 |
),
|
91 |
outputs=[
|
92 |
-
gr.Textbox(label="
|
93 |
-
gr.Textbox(label="
|
94 |
-
gr.Textbox(label="Detailed Analysis", lines=3)
|
95 |
],
|
96 |
-
title="
|
97 |
-
description=""
|
98 |
-
|
99 |
-
It provides detailed sentiment analysis for longer texts and handles
|
100 |
-
both audio uploads and microphone recordings.
|
101 |
-
""",
|
102 |
-
theme=gr.themes.Soft(),
|
103 |
-
examples=[]
|
104 |
)
|
105 |
|
106 |
-
|
107 |
-
"""Launch the application"""
|
108 |
interface = create_interface()
|
109 |
interface.launch(
|
110 |
share=True,
|
111 |
-
debug=True,
|
112 |
server_name="0.0.0.0"
|
113 |
-
)
|
114 |
-
|
115 |
-
if __name__ == "__main__":
|
116 |
-
main()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
+
def create_tone_analyzer():
|
5 |
+
"""Initialize the speech-to-text and tone analysis models"""
|
6 |
try:
|
7 |
+
# Load Whisper for speech recognition
|
8 |
transcriber = pipeline(
|
9 |
"automatic-speech-recognition",
|
10 |
+
model="openai/whisper-small"
|
|
|
11 |
)
|
12 |
|
13 |
+
# Load RoBERTa for tone analysis
|
14 |
+
tone_analyzer = pipeline(
|
15 |
"sentiment-analysis",
|
16 |
model="cardiffnlp/twitter-roberta-base-sentiment-latest"
|
17 |
)
|
18 |
|
19 |
+
return transcriber, tone_analyzer
|
|
|
20 |
except Exception as e:
|
21 |
+
raise RuntimeError(f"Failed to load models: {str(e)}")
|
22 |
|
23 |
+
def analyze_tone(audio_file):
|
24 |
+
"""Analyze the tone of speech"""
|
|
|
|
|
|
|
25 |
try:
|
26 |
+
transcriber, tone_analyzer = create_tone_analyzer()
|
27 |
|
28 |
+
# Convert speech to text
|
29 |
+
text = transcriber(audio_file)["text"]
|
30 |
|
31 |
+
# Analyze tone
|
32 |
+
result = tone_analyzer(text)[0]
|
33 |
|
34 |
+
# Convert model output to human-readable format
|
35 |
+
tone_mapping = {
|
36 |
'LABEL_0': 'Negative',
|
37 |
'LABEL_1': 'Neutral',
|
38 |
'LABEL_2': 'Positive'
|
39 |
}
|
40 |
|
41 |
+
tone = tone_mapping.get(result['label'], result['label'])
|
42 |
+
confidence = result['score']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
return {
|
45 |
+
"tone": f"{tone}",
|
46 |
+
"confidence": f"{confidence:.1%}"
|
|
|
47 |
}
|
48 |
|
49 |
except Exception as e:
|
50 |
return {
|
51 |
+
"tone": "Error",
|
52 |
+
"confidence": f"Failed to analyze: {str(e)}"
|
|
|
53 |
}
|
54 |
|
55 |
def create_interface():
|
56 |
+
"""Create the Gradio interface"""
|
57 |
return gr.Interface(
|
58 |
+
fn=analyze_tone,
|
59 |
inputs=gr.Audio(
|
60 |
sources=["microphone", "upload"],
|
61 |
type="filepath",
|
62 |
+
label="Record or Upload Audio"
|
63 |
),
|
64 |
outputs=[
|
65 |
+
gr.Textbox(label="Detected Tone"),
|
66 |
+
gr.Textbox(label="Confidence")
|
|
|
67 |
],
|
68 |
+
title="Speech Tone Analyzer",
|
69 |
+
description="Record or upload audio to analyze its emotional tone.",
|
70 |
+
theme=gr.themes.Soft()
|
|
|
|
|
|
|
|
|
|
|
71 |
)
|
72 |
|
73 |
+
if __name__ == "__main__":
|
|
|
74 |
interface = create_interface()
|
75 |
interface.launch(
|
76 |
share=True,
|
|
|
77 |
server_name="0.0.0.0"
|
78 |
+
)
|
|
|
|
|
|