Update index.js
Browse files
index.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
// index.js
|
2 |
-
|
3 |
-
|
4 |
-
// Define available models (mirror of constants.py AVAILABLE_MODELS)
|
5 |
const AVAILABLE_MODELS = [
|
6 |
{ name: "Moonshot Kimi-K2", id: "moonshotai/Kimi-K2-Instruct" },
|
7 |
{ name: "DeepSeek V3", id: "deepseek-ai/DeepSeek-V3-0324" },
|
@@ -15,14 +14,15 @@ const AVAILABLE_MODELS = [
|
|
15 |
{ name: "Qwen3-Coder-480B-A35B", id: "Qwen/Qwen3-Coder-480B-A35B-Instruct" }
|
16 |
];
|
17 |
|
18 |
-
|
|
|
19 |
const modelSelect = document.getElementById('modelSelect');
|
20 |
const inputText = document.getElementById('inputText');
|
21 |
const resultSection = document.getElementById('result');
|
22 |
const analyzeButton = document.getElementById('analyzeButton');
|
23 |
let sentimentPipeline;
|
24 |
|
25 |
-
// Populate
|
26 |
AVAILABLE_MODELS.forEach(model => {
|
27 |
const opt = document.createElement('option');
|
28 |
opt.value = model.id;
|
@@ -38,16 +38,17 @@ async function initPipeline(modelId) {
|
|
38 |
analyzeButton.disabled = false;
|
39 |
}
|
40 |
|
41 |
-
// Initialize with first model
|
42 |
initPipeline(modelSelect.value);
|
43 |
|
44 |
-
// Re-
|
45 |
modelSelect.addEventListener('change', () => {
|
46 |
initPipeline(modelSelect.value);
|
47 |
resultSection.textContent = '';
|
48 |
});
|
49 |
|
50 |
-
|
|
|
51 |
event.preventDefault();
|
52 |
const text = inputText.value.trim();
|
53 |
if (!text) return;
|
@@ -59,7 +60,7 @@ analyzeForm.addEventListener('submit', async event => {
|
|
59 |
try {
|
60 |
const output = await sentimentPipeline(text);
|
61 |
const { label, score } = output[0];
|
62 |
-
resultSection.textContent = `Sentiment: ${label} (Confidence: ${(score
|
63 |
} catch (err) {
|
64 |
resultSection.textContent = 'Error analyzing sentiment.';
|
65 |
}
|
|
|
1 |
// index.js
|
2 |
+
// Note: The Python backend defines AVAILABLE_MODELS in constants.py for server-side use.
|
3 |
+
// For the front-end, we replicate the list here so the UI can populate the dropdown.
|
|
|
4 |
const AVAILABLE_MODELS = [
|
5 |
{ name: "Moonshot Kimi-K2", id: "moonshotai/Kimi-K2-Instruct" },
|
6 |
{ name: "DeepSeek V3", id: "deepseek-ai/DeepSeek-V3-0324" },
|
|
|
14 |
{ name: "Qwen3-Coder-480B-A35B", id: "Qwen/Qwen3-Coder-480B-A35B-Instruct" }
|
15 |
];
|
16 |
|
17 |
+
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers';
|
18 |
+
|
19 |
const modelSelect = document.getElementById('modelSelect');
|
20 |
const inputText = document.getElementById('inputText');
|
21 |
const resultSection = document.getElementById('result');
|
22 |
const analyzeButton = document.getElementById('analyzeButton');
|
23 |
let sentimentPipeline;
|
24 |
|
25 |
+
// Populate the dropdown
|
26 |
AVAILABLE_MODELS.forEach(model => {
|
27 |
const opt = document.createElement('option');
|
28 |
opt.value = model.id;
|
|
|
38 |
analyzeButton.disabled = false;
|
39 |
}
|
40 |
|
41 |
+
// Initialize on load with first model
|
42 |
initPipeline(modelSelect.value);
|
43 |
|
44 |
+
// Re-initialize when user selects a different model
|
45 |
modelSelect.addEventListener('change', () => {
|
46 |
initPipeline(modelSelect.value);
|
47 |
resultSection.textContent = '';
|
48 |
});
|
49 |
|
50 |
+
// Handle form submit
|
51 |
+
document.getElementById('analyze-form').addEventListener('submit', async event => {
|
52 |
event.preventDefault();
|
53 |
const text = inputText.value.trim();
|
54 |
if (!text) return;
|
|
|
60 |
try {
|
61 |
const output = await sentimentPipeline(text);
|
62 |
const { label, score } = output[0];
|
63 |
+
resultSection.textContent = `Sentiment: ${label} (Confidence: ${(score*100).toFixed(2)}%)`;
|
64 |
} catch (err) {
|
65 |
resultSection.textContent = 'Error analyzing sentiment.';
|
66 |
}
|