Spaces:
Running
Running
Update index.html
Browse files- index.html +30 -4
index.html
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
<title>Transformers.js Sentiment Analysis</title>
|
| 7 |
-
<script src="https://
|
| 8 |
<style>
|
| 9 |
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
|
| 10 |
textarea { width: 100%; height: 100px; }
|
|
@@ -14,18 +14,39 @@
|
|
| 14 |
</head>
|
| 15 |
<body>
|
| 16 |
<h1>Transformers.js Sentiment Analysis</h1>
|
| 17 |
-
<textarea id="input" placeholder="Enter text for sentiment analysis"
|
| 18 |
<button onclick="analyzeSentiment()">Analyze Sentiment</button>
|
| 19 |
<div id="result"></div>
|
| 20 |
|
| 21 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
async function analyzeSentiment() {
|
| 23 |
const input = document.getElementById('input').value;
|
| 24 |
const result = document.getElementById('result');
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try {
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
// Perform sentiment analysis
|
| 31 |
const sentiment = await pipeline(input);
|
|
@@ -37,9 +58,14 @@
|
|
| 37 |
<p>Score: ${sentiment[0].score.toFixed(4)}</p>
|
| 38 |
`;
|
| 39 |
} catch (error) {
|
|
|
|
| 40 |
result.innerHTML = `<p>Error: ${error.message}</p>`;
|
| 41 |
}
|
| 42 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
</script>
|
| 44 |
</body>
|
| 45 |
</html>
|
|
|
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
<title>Transformers.js Sentiment Analysis</title>
|
| 7 |
+
<script src="https://cdn.jsdelivr.net/npm/@xenova/transformers@2.5.0"></script>
|
| 8 |
<style>
|
| 9 |
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
|
| 10 |
textarea { width: 100%; height: 100px; }
|
|
|
|
| 14 |
</head>
|
| 15 |
<body>
|
| 16 |
<h1>Transformers.js Sentiment Analysis</h1>
|
| 17 |
+
<textarea id="input" placeholder="Enter text for sentiment analysis">I love this movie! It's amazing!</textarea>
|
| 18 |
<button onclick="analyzeSentiment()">Analyze Sentiment</button>
|
| 19 |
<div id="result"></div>
|
| 20 |
|
| 21 |
<script>
|
| 22 |
+
// Use the pipeline from Xenova's transformers package
|
| 23 |
+
let pipeline;
|
| 24 |
+
|
| 25 |
+
async function loadPipeline() {
|
| 26 |
+
try {
|
| 27 |
+
pipeline = await transformers.pipeline('sentiment-analysis');
|
| 28 |
+
document.querySelector('button').disabled = false;
|
| 29 |
+
} catch (error) {
|
| 30 |
+
console.error('Error loading pipeline:', error);
|
| 31 |
+
document.getElementById('result').innerHTML = `<p>Error: ${error.message}</p>`;
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
async function analyzeSentiment() {
|
| 36 |
const input = document.getElementById('input').value;
|
| 37 |
const result = document.getElementById('result');
|
| 38 |
|
| 39 |
+
if (!input.trim()) {
|
| 40 |
+
result.innerHTML = '<p>Please enter some text to analyze.</p>';
|
| 41 |
+
return;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
result.innerHTML = '<p>Analyzing...</p>';
|
| 45 |
+
|
| 46 |
try {
|
| 47 |
+
if (!pipeline) {
|
| 48 |
+
throw new Error('Pipeline not loaded. Please wait and try again.');
|
| 49 |
+
}
|
| 50 |
|
| 51 |
// Perform sentiment analysis
|
| 52 |
const sentiment = await pipeline(input);
|
|
|
|
| 58 |
<p>Score: ${sentiment[0].score.toFixed(4)}</p>
|
| 59 |
`;
|
| 60 |
} catch (error) {
|
| 61 |
+
console.error('Error during sentiment analysis:', error);
|
| 62 |
result.innerHTML = `<p>Error: ${error.message}</p>`;
|
| 63 |
}
|
| 64 |
}
|
| 65 |
+
|
| 66 |
+
// Load the pipeline when the page loads
|
| 67 |
+
document.querySelector('button').disabled = true;
|
| 68 |
+
loadPipeline();
|
| 69 |
</script>
|
| 70 |
</body>
|
| 71 |
</html>
|