gopichandra commited on
Commit
be4a8fe
Β·
verified Β·
1 Parent(s): 2e17d37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -24
app.py CHANGED
@@ -6,18 +6,15 @@ import uuid
6
  import csv
7
  import os
8
 
9
- # Load Whisper model
10
  whisper_model = whisper.load_model("base")
11
-
12
- # Load FinBERT model
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
- # Main function
19
- def analyze_sentiment(audio_path, client_type):
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 result
47
-
48
- # Gradio Interface
49
- interface = gr.Interface(
50
- fn=analyze_sentiment,
51
- inputs=[
52
- gr.Audio(sources=["upload"], type="filepath", label="Upload Voice File"),
53
- gr.Dropdown(choices=["New", "Renewal", "Support"], label="Client Type")
54
- ],
55
- outputs="json",
56
- title="πŸ“Š Voice Sentiment Analyzer",
57
- description="Upload a voice note. Select client type and analyze sentiment using FinBERT."
58
- )
59
-
60
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()