File size: 2,705 Bytes
19544dd
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
<!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">
</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>
    </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;

            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}</p>
        </div>
                        `;
                    }).join('');
                } else {
                    resultDiv.textContent = "Unable to determine sentiment.";
                }
            });
        }
    </script>
</body>
</html>