File size: 4,009 Bytes
19544dd
b9bd274
 
 
 
 
 
934b606
b9bd274
 
 
 
 
 
 
934b606
b9bd274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
934b606
 
b9bd274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
934b606
b9bd274
 
 
934b606
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9bd274
 
 
 
 
 
 
19544dd
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sentiment Analysis Web App</title>
    <link rel="stylesheet" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
    <div class="container">
        <h1>Sentiment Analysis</h1>
        <textarea id="textInput" placeholder="Enter text..."></textarea>
        <button onclick="classifySentiment()">Classify</button>
        <div id="result" class="result-container"></div>
	<canvas id="chart"></canvas>
    </div>

    <script>
        async function query(data) {
            try {
                const response = await fetch(
                    "https://api-inference.huggingface.co/models/ahmedrachid/FinancialBERT-Sentiment-Analysis",
                    {
                        headers: { Authorization: "Bearer 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" },
                        method: "POST",
                        body: JSON.stringify(data),
                    }
                );

                if (!response.ok) {
                    throw new Error(`HTTP error! Status: ${response.status}`);
                }

                const result = await response.json();
                return result;
            } catch (error) {
                console.error("Error during API request:", error);
                return { error: "Failed to get predictions from the model." };
            }
        }

        function classifySentiment() {
            const textInput = document.getElementById("textInput").value;
const chartCanvas = document.getElementById("chart");


            if (textInput.trim() === "") {
                alert("Please enter text for sentiment analysis.");
                return;
            }

            const data = { "inputs": textInput };

            // Call the query function and handle the response
            query(data).then((response) => {
                console.log(JSON.stringify(response));

                const resultDiv = document.getElementById("result");

                if (response && Array.isArray(response) && response.length > 0) {
                    const predictions = response[0];

                    // Display the results for each sentiment label and score
                    resultDiv.innerHTML = predictions.map((prediction) => {
                        return `
                            <div class="result-item">
            <p>Sentiment: ${prediction.label}</p>
            <p>Confidence Score: ${prediction.score*100}</p>
        </div>
                        `;
                    }).join('');

               const labels = predictions.map(prediction => prediction.label);
                    const scores = predictions.map(prediction => prediction.score * 100); // Scale scores to percentage

                    const ctx = chartCanvas.getContext('2d');
                    const chart = new Chart(ctx, {
                        type: 'bar',
                        data: {
                            labels: labels,
                            datasets: [{
                                label: 'Confidence Scores (%)',
                                data: scores,
                                backgroundColor: 'rgba(75, 192, 192, 0.2)',
                                borderColor: 'rgba(75, 192, 192, 1)',
                                borderWidth: 1
                            }]
                        },
                        options: {
                            scales: {
                                y: {
                                    beginAtZero: true,
                                    max: 100
                                }
                            }
                        }
                    });
                } else {
                    resultDiv.textContent = "Unable to determine sentiment.";
                }
            });
        }
    </script>
</body>
</html>