Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,92 +1,53 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
|
5 |
-
|
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 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
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 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
# Transcribe audio
|
40 |
-
transcription = transcriber(audio_file)["text"]
|
41 |
|
42 |
-
# Analyze sentiment
|
43 |
-
sentiment_result =
|
44 |
|
45 |
-
|
|
|
46 |
"transcription": transcription,
|
47 |
"sentiment": sentiment_result["label"],
|
48 |
-
"confidence":
|
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 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
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 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
90 |
|
91 |
-
|
92 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Load Whisper for speech-to-text
|
5 |
+
whisper = pipeline("automatic-speech-recognition", model="openai/whisper-medium")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Load a sentiment analysis model
|
8 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
9 |
+
|
10 |
+
# Function to process audio and analyze tone
|
11 |
+
def analyze_call(audio_file):
|
|
|
|
|
|
|
|
|
|
|
12 |
try:
|
13 |
+
# Step 1: Transcribe audio to text using Whisper
|
14 |
+
transcription = whisper(audio_file)["text"]
|
|
|
|
|
|
|
15 |
|
16 |
+
# Step 2: Analyze sentiment of the transcription
|
17 |
+
sentiment_result = sentiment_analyzer(transcription)[0]
|
18 |
|
19 |
+
# Prepare the output
|
20 |
+
output = {
|
21 |
"transcription": transcription,
|
22 |
"sentiment": sentiment_result["label"],
|
23 |
+
"confidence": round(sentiment_result["score"], 4)
|
24 |
}
|
25 |
+
return output
|
26 |
except Exception as e:
|
27 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# Gradio Interface
|
30 |
+
def gradio_interface(audio):
|
31 |
+
if audio is None:
|
32 |
+
return "Please record or upload an audio file."
|
33 |
+
result = analyze_call(audio)
|
34 |
+
if "error" in result:
|
35 |
+
return f"Error: {result['error']}"
|
36 |
+
return (
|
37 |
+
f"**Transcription:** {result['transcription']}\n\n"
|
38 |
+
f"**Sentiment:** {result['sentiment']}\n\n"
|
39 |
+
f"**Confidence:** {result['confidence']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
)
|
41 |
|
42 |
+
# Create Gradio app
|
43 |
+
interface = gr.Interface(
|
44 |
+
fn=gradio_interface,
|
45 |
+
inputs=gr.Audio(source="microphone", type="filepath", label="Record or Upload Audio"),
|
46 |
+
outputs=gr.Textbox(label="Analysis Result", lines=5),
|
47 |
+
title="Real-Time Call Analysis",
|
48 |
+
description="Record or upload audio to analyze tone and sentiment in real time.",
|
49 |
+
live=False # Set to False to avoid constant re-runs
|
50 |
+
)
|
51 |
|
52 |
+
# Launch the app
|
53 |
+
interface.launch()
|