File size: 3,649 Bytes
00c9df6
 
 
 
 
 
 
 
be4a8fe
00c9df6
be4a8fe
 
00c9df6
 
be4a8fe
 
 
00c9df6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be4a8fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import gradio as gr
import whisper
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import uuid
import csv
import os

# Load models
whisper_model = whisper.load_model("base")
tokenizer = AutoTokenizer.from_pretrained("yiyanghkust/finbert-tone")
model = AutoModelForSequenceClassification.from_pretrained("yiyanghkust/finbert-tone")
labels = ["Positive", "Negative", "Neutral"]

# Analysis Function
def analyze_sentiment_auto(audio_path, client_type):
    transcription_result = whisper_model.transcribe(audio_path, task="translate")
    transcript = transcription_result["text"].strip()

    inputs = tokenizer(transcript, return_tensors="pt", truncation=True)
    with torch.no_grad():
        logits = model(**inputs).logits
        prediction = torch.argmax(logits, dim=1).item()
    sentiment = labels[prediction]

    client_id = str(uuid.uuid4())[:8]
    result = {
        "client_id": client_id,
        "client_type": client_type,
        "sentiment": sentiment,
        "transcript": transcript
    }

    # Save to CSV
    file_exists = os.path.isfile("sentiment_results.csv")
    with open("sentiment_results.csv", mode='a', newline='', encoding='utf-8') as csv_file:
        fieldnames = ["client_id", "client_type", "sentiment", "transcript"]
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        if not file_exists:
            writer.writeheader()
        writer.writerow(result)

    return (
        transcript,
        f"πŸ†” {client_id}",
        f"πŸ“‹ {client_type}",
        f"{sentiment}"
    )

# Color map for sentiment
def sentiment_color(sentiment):
    return {
        "Positive": "#22c55e",  # Green
        "Negative": "#ef4444",  # Red
        "Neutral": "#facc15"    # Yellow
    }.get(sentiment, "#e5e7eb")

# Gradio UI
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    <h1 style='text-align: center; color: #1d4ed8;'>πŸŽ™οΈ Multilingual Voice Sentiment Analyzer</h1>
    <p style='text-align: center; font-size: 16px;'>
        Upload a voice note in any language. It will be auto-translated to English and analyzed using FinBERT.
    </p>
    """)

    with gr.Row():
        audio_input = gr.Audio(sources=["upload"], type="filepath", label="πŸ”Š Upload Voice File")
        client_type = gr.Dropdown(choices=["New", "Renewal", "Support"], value="New", label="πŸ‘€ Client Type")

    submit_btn = gr.Button("πŸš€ Analyze Sentiment", variant="primary")

    with gr.Row():
        transcript_output = gr.Textbox(label="πŸ“ English Transcript", lines=4, interactive=False)
        client_id_output = gr.Textbox(label="πŸ†” Client ID", interactive=False)
        client_type_output = gr.Textbox(label="πŸ‘€ Client Type", interactive=False)
        sentiment_output = gr.Textbox(label="πŸ“Š Sentiment", interactive=False)

    def process(audio, client_type):
        transcript, client_id, ctype, sentiment = analyze_sentiment_auto(audio, client_type)
        return (
            gr.update(value=transcript),
            gr.update(value=client_id),
            gr.update(value=ctype),
            gr.update(value=sentiment, label=f"πŸ“Š Sentiment: {sentiment}", elem_id="sentiment-output")
        )

    submit_btn.click(
        fn=process,
        inputs=[audio_input, client_type],
        outputs=[transcript_output, client_id_output, client_type_output, sentiment_output]
    )

    gr.Markdown("""
    <style>
    #sentiment-output textarea {
        font-weight: bold;
        text-align: center;
        font-size: 1.2em;
        color: white;
        background-color: #1f2937;
    }
    </style>
    """)

demo.launch()