Spaces:
Running
Running
<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> |