Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,92 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
# Load
|
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 |
-
live=True, # Enable real-time processing
|
34 |
-
title="Real-Time Call Analysis",
|
35 |
-
description="Upload or record audio to analyze tone and sentiment."
|
36 |
-
)
|
37 |
|
38 |
-
|
39 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
def create_speech_analyzer():
|
5 |
+
# Initialize models with error handling
|
6 |
+
try:
|
7 |
+
# Load Whisper model for speech recognition
|
8 |
+
transcriber = pipeline(
|
9 |
+
"automatic-speech-recognition",
|
10 |
+
model="openai/whisper-medium",
|
11 |
+
max_new_tokens=128
|
12 |
+
)
|
13 |
+
|
14 |
+
# Load sentiment analysis model
|
15 |
+
sentiment_model = pipeline(
|
16 |
+
"sentiment-analysis",
|
17 |
+
model="distilbert-base-uncased-finetuned-sst-2-english"
|
18 |
+
)
|
19 |
+
|
20 |
+
return transcriber, sentiment_model
|
21 |
|
22 |
+
except Exception as e:
|
23 |
+
raise RuntimeError(f"Error loading models: {str(e)}")
|
24 |
+
|
25 |
+
def analyze_speech(audio_file):
|
26 |
+
"""
|
27 |
+
Analyze speech audio for transcription and sentiment.
|
28 |
|
29 |
+
Args:
|
30 |
+
audio_file: Path to audio file or audio data
|
31 |
+
|
32 |
+
Returns:
|
33 |
+
dict: Contains transcription, sentiment and confidence score
|
34 |
+
"""
|
35 |
+
try:
|
36 |
+
# Get model instances
|
37 |
+
transcriber, sentiment_model = create_speech_analyzer()
|
38 |
+
|
39 |
+
# Transcribe audio
|
40 |
+
transcription = transcriber(audio_file)["text"]
|
41 |
+
|
42 |
+
# Analyze sentiment
|
43 |
+
sentiment_result = sentiment_model(transcription)[0]
|
44 |
+
|
45 |
+
return {
|
46 |
+
"transcription": transcription,
|
47 |
+
"sentiment": sentiment_result["label"],
|
48 |
+
"confidence": f"{sentiment_result['score']:.2%}"
|
49 |
+
}
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
return {
|
53 |
+
"transcription": f"Error processing audio: {str(e)}",
|
54 |
+
"sentiment": "ERROR",
|
55 |
+
"confidence": "0%"
|
56 |
+
}
|
57 |
+
|
58 |
+
def create_interface():
|
59 |
+
"""Create and configure the Gradio interface"""
|
60 |
+
return gr.Interface(
|
61 |
+
fn=analyze_speech,
|
62 |
+
inputs=gr.Audio(
|
63 |
+
source="microphone",
|
64 |
+
type="filepath",
|
65 |
+
label="Upload or Record Audio"
|
66 |
+
),
|
67 |
+
outputs=[
|
68 |
+
gr.Textbox(label="Transcription"),
|
69 |
+
gr.Textbox(label="Sentiment Analysis"),
|
70 |
+
gr.Textbox(label="Confidence Score")
|
71 |
+
],
|
72 |
+
title="Real-Time Speech Sentiment Analyzer",
|
73 |
+
description="""
|
74 |
+
This tool transcribes speech and analyzes its sentiment in real-time.
|
75 |
+
Upload an audio file or record directly through your microphone.
|
76 |
+
""",
|
77 |
+
theme=gr.themes.Soft(),
|
78 |
+
examples=[], # Add example audio files here if desired
|
79 |
+
cache_examples=True
|
80 |
+
)
|
81 |
|
82 |
+
def main():
|
83 |
+
# Create and launch the interface
|
84 |
+
interface = create_interface()
|
85 |
+
interface.launch(
|
86 |
+
share=True, # Enable sharing via public URL
|
87 |
+
debug=True, # Enable debug mode for better error messages
|
88 |
+
server_name="0.0.0.0" # Allow external connections
|
89 |
+
)
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
+
if __name__ == "__main__":
|
92 |
+
main()
|