Spaces:
Running
Running
File size: 2,603 Bytes
90585e8 71a1747 90585e8 71a1747 90585e8 71a1747 90585e8 71a1747 90585e8 71a1747 90585e8 71a1747 90585e8 71a1747 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 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 |
<!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://cdn.jsdelivr.net/npm/@xenova/[email protected]"></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">I love this movie! It's amazing!</textarea>
<button onclick="analyzeSentiment()">Analyze Sentiment</button>
<div id="result"></div>
<script>
// Use the pipeline from Xenova's transformers package
let pipeline;
async function loadPipeline() {
try {
pipeline = await transformers.pipeline('sentiment-analysis');
document.querySelector('button').disabled = false;
} catch (error) {
console.error('Error loading pipeline:', error);
document.getElementById('result').innerHTML = `<p>Error: ${error.message}</p>`;
}
}
async function analyzeSentiment() {
const input = document.getElementById('input').value;
const result = document.getElementById('result');
if (!input.trim()) {
result.innerHTML = '<p>Please enter some text to analyze.</p>';
return;
}
result.innerHTML = '<p>Analyzing...</p>';
try {
if (!pipeline) {
throw new Error('Pipeline not loaded. Please wait and try again.');
}
// 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) {
console.error('Error during sentiment analysis:', error);
result.innerHTML = `<p>Error: ${error.message}</p>`;
}
}
// Load the pipeline when the page loads
document.querySelector('button').disabled = true;
loadPipeline();
</script>
</body>
</html> |