File size: 1,551 Bytes
11a0fc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
let chart;

async function fetchMessages() {
    const res = await fetch("http://127.0.0.1:8000/get_messages");
    const data = await res.json();

    const container = document.getElementById("chat-container");
    container.innerHTML = "";

    data.forEach(msg => {
        const div = document.createElement("div");
        div.className = "chat-card";

        let color = "gray";
        if (msg.sentiment === "Positive") color = "green";
        else if (msg.sentiment === "Negative") color = "red";
        else color = "orange";

        div.innerHTML = `
            <strong>${msg.author}</strong>: ${msg.text}<br>
            ${msg.sentiment} (${msg.confidence})
            <div style="background:lightgray;height:6px;">
                <div style="width:${msg.confidence*100}%;background:${color};height:6px;"></div>
            </div>
        `;
        container.appendChild(div);
    });
}

async function loadChart() {
    const res = await fetch("http://127.0.0.1:8000/sentiment_summary");
    const data = await res.json();

    const ctx = document.getElementById("sentimentChart");

    if (chart) chart.destroy();

    chart = new Chart(ctx, {
        type: "bar",
        data: {
            labels: ["Positive", "Neutral", "Negative"],
            datasets: [{
                label: "Sentiment Count",
                data: [data.counts.Positive, data.counts.Neutral, data.counts.Negative]
            }]
        }
    });
}

setInterval(() => {
    fetchMessages();
    loadChart();
}, 15000);

fetchMessages();
loadChart();