File size: 1,668 Bytes
90585e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Transformers.js Sentiment Analysis</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/transformers/2.5.0/transformers.min.js"></script>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
        textarea { width: 100%; height: 100px; }
        button { margin-top: 10px; }
        #result { margin-top: 20px; }
    </style>
</head>
<body>
    <h1>Transformers.js Sentiment Analysis</h1>
    <textarea id="input" placeholder="Enter text for sentiment analysis"></textarea>
    <button onclick="analyzeSentiment()">Analyze Sentiment</button>
    <div id="result"></div>

    <script>
        async function analyzeSentiment() {
            const input = document.getElementById('input').value;
            const result = document.getElementById('result');

            try {
                // Load the sentiment analysis pipeline
                const pipeline = await transformers.pipeline('sentiment-analysis');

                // Perform sentiment analysis
                const sentiment = await pipeline(input);

                // Display the result
                result.innerHTML = `
                    <h3>Sentiment Analysis Result:</h3>
                    <p>Label: ${sentiment[0].label}</p>
                    <p>Score: ${sentiment[0].score.toFixed(4)}</p>
                `;
            } catch (error) {
                result.innerHTML = `<p>Error: ${error.message}</p>`;
            }
        }
    </script>
</body>
</html>