Update app.py
Browse files
app.py
CHANGED
@@ -6,18 +6,15 @@ import uuid
|
|
6 |
import csv
|
7 |
import os
|
8 |
|
9 |
-
# Load
|
10 |
whisper_model = whisper.load_model("base")
|
11 |
-
|
12 |
-
|
13 |
-
finbert_model = "yiyanghkust/finbert-tone"
|
14 |
-
tokenizer = AutoTokenizer.from_pretrained(finbert_model)
|
15 |
-
model = AutoModelForSequenceClassification.from_pretrained(finbert_model)
|
16 |
labels = ["Positive", "Negative", "Neutral"]
|
17 |
|
18 |
-
#
|
19 |
-
def
|
20 |
-
transcription_result = whisper_model.transcribe(audio_path)
|
21 |
transcript = transcription_result["text"].strip()
|
22 |
|
23 |
inputs = tokenizer(transcript, return_tensors="pt", truncation=True)
|
@@ -43,18 +40,67 @@ def analyze_sentiment(audio_path, client_type):
|
|
43 |
writer.writeheader()
|
44 |
writer.writerow(result)
|
45 |
|
46 |
-
return
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import csv
|
7 |
import os
|
8 |
|
9 |
+
# Load models
|
10 |
whisper_model = whisper.load_model("base")
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("yiyanghkust/finbert-tone")
|
12 |
+
model = AutoModelForSequenceClassification.from_pretrained("yiyanghkust/finbert-tone")
|
|
|
|
|
|
|
13 |
labels = ["Positive", "Negative", "Neutral"]
|
14 |
|
15 |
+
# Analysis Function
|
16 |
+
def analyze_sentiment_auto(audio_path, client_type):
|
17 |
+
transcription_result = whisper_model.transcribe(audio_path, task="translate")
|
18 |
transcript = transcription_result["text"].strip()
|
19 |
|
20 |
inputs = tokenizer(transcript, return_tensors="pt", truncation=True)
|
|
|
40 |
writer.writeheader()
|
41 |
writer.writerow(result)
|
42 |
|
43 |
+
return (
|
44 |
+
transcript,
|
45 |
+
f"π {client_id}",
|
46 |
+
f"π {client_type}",
|
47 |
+
f"{sentiment}"
|
48 |
+
)
|
49 |
+
|
50 |
+
# Color map for sentiment
|
51 |
+
def sentiment_color(sentiment):
|
52 |
+
return {
|
53 |
+
"Positive": "#22c55e", # Green
|
54 |
+
"Negative": "#ef4444", # Red
|
55 |
+
"Neutral": "#facc15" # Yellow
|
56 |
+
}.get(sentiment, "#e5e7eb")
|
57 |
+
|
58 |
+
# Gradio UI
|
59 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
60 |
+
gr.Markdown("""
|
61 |
+
<h1 style='text-align: center; color: #1d4ed8;'>ποΈ Multilingual Voice Sentiment Analyzer</h1>
|
62 |
+
<p style='text-align: center; font-size: 16px;'>
|
63 |
+
Upload a voice note in any language. It will be auto-translated to English and analyzed using FinBERT.
|
64 |
+
</p>
|
65 |
+
""")
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
audio_input = gr.Audio(sources=["upload"], type="filepath", label="π Upload Voice File")
|
69 |
+
client_type = gr.Dropdown(choices=["New", "Renewal", "Support"], value="New", label="π€ Client Type")
|
70 |
+
|
71 |
+
submit_btn = gr.Button("π Analyze Sentiment", variant="primary")
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
transcript_output = gr.Textbox(label="π English Transcript", lines=4, interactive=False)
|
75 |
+
client_id_output = gr.Textbox(label="π Client ID", interactive=False)
|
76 |
+
client_type_output = gr.Textbox(label="π€ Client Type", interactive=False)
|
77 |
+
sentiment_output = gr.Textbox(label="π Sentiment", interactive=False)
|
78 |
+
|
79 |
+
def process(audio, client_type):
|
80 |
+
transcript, client_id, ctype, sentiment = analyze_sentiment_auto(audio, client_type)
|
81 |
+
return (
|
82 |
+
gr.update(value=transcript),
|
83 |
+
gr.update(value=client_id),
|
84 |
+
gr.update(value=ctype),
|
85 |
+
gr.update(value=sentiment, label=f"π Sentiment: {sentiment}", elem_id="sentiment-output")
|
86 |
+
)
|
87 |
+
|
88 |
+
submit_btn.click(
|
89 |
+
fn=process,
|
90 |
+
inputs=[audio_input, client_type],
|
91 |
+
outputs=[transcript_output, client_id_output, client_type_output, sentiment_output]
|
92 |
+
)
|
93 |
+
|
94 |
+
gr.Markdown("""
|
95 |
+
<style>
|
96 |
+
#sentiment-output textarea {
|
97 |
+
font-weight: bold;
|
98 |
+
text-align: center;
|
99 |
+
font-size: 1.2em;
|
100 |
+
color: white;
|
101 |
+
background-color: #1f2937;
|
102 |
+
}
|
103 |
+
</style>
|
104 |
+
""")
|
105 |
+
|
106 |
+
demo.launch()
|