builder / index.js
mgbam's picture
Update index.js
1db8d78 verified
raw
history blame
1.25 kB
// index.js
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers';
const analyzeForm = document.getElementById('analyze-form');
const inputText = document.getElementById('inputText');
const resultSection = document.getElementById('result');
const analyzeButton = document.getElementById('analyzeButton');
let sentimentPipeline;
async function initPipeline() {
analyzeButton.disabled = true;
analyzeButton.textContent = 'Loading model...';
sentimentPipeline = await pipeline('sentiment-analysis');
analyzeButton.textContent = 'Analyze Sentiment';
analyzeButton.disabled = false;
}
analyzeForm.addEventListener('submit', async (event) => {
event.preventDefault();
const text = inputText.value.trim();
if (!text) return;
analyzeButton.disabled = true;
analyzeButton.textContent = 'Analyzing...';
resultSection.textContent = '';
try {
const output = await sentimentPipeline(text);
const { label, score } = output[0];
resultSection.textContent = `Sentiment: ${label} (Confidence: ${(score * 100).toFixed(2)}%)`;
} catch (error) {
resultSection.textContent = 'Error analyzing sentiment.';
}
analyzeButton.textContent = 'Analyze Sentiment';
analyzeButton.disabled = false;
});