Update static/index.js
Browse files- static/index.js +78 -0
static/index.js
CHANGED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// index.js
|
2 |
+
|
3 |
+
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers';
|
4 |
+
|
5 |
+
// Front‑end copy of the Python AVAILABLE_MODELS list for dropdown population:
|
6 |
+
const AVAILABLE_MODELS = [
|
7 |
+
{ name: "Moonshot Kimi‑K2", id: "moonshotai/Kimi‑K2‑Instruct" },
|
8 |
+
{ name: "DeepSeek V3", id: "deepseek‑ai/DeepSeek‑V3‑0324" },
|
9 |
+
{ name: "DeepSeek R1", id: "deepseek‑ai/DeepSeek‑R1‑0528" },
|
10 |
+
{ name: "ERNIE‑4.5‑VL", id: "baidu/ERNIE‑4.5‑VL‑424B‑A47B‑Base‑PT" },
|
11 |
+
{ name: "MiniMax M1", id: "MiniMaxAI/MiniMax‑M1‑80k" },
|
12 |
+
{ name: "Qwen3‑235B‑A22B", id: "Qwen/Qwen3‑235B‑A22B" },
|
13 |
+
{ name: "Qwen3‑235B‑A22B‑Instruct‑2507", id: "Qwen/Qwen3‑235B‑A22B‑Instruct‑2507" },
|
14 |
+
{ name: "Qwen3‑Coder‑480B‑A35B", id: "Qwen/Qwen3‑Coder‑480B‑A35B‑Instruct" },
|
15 |
+
{ name: "Qwen3‑32B", id: "Qwen/Qwen3‑32B" },
|
16 |
+
{ name: "SmolLM3‑3B", id: "HuggingFaceTB/SmolLM3‑3B" },
|
17 |
+
{ name: "GLM‑4.1V‑9B‑Thinking", id: "THUDM/GLM‑4.1V‑9B‑Thinking" },
|
18 |
+
{ name: "OpenAI GPT‑4", id: "openai/gpt‑4" },
|
19 |
+
{ name: "Gemini Pro", id: "gemini/pro" },
|
20 |
+
{ name: "Fireworks AI", id: "fireworks‑ai/fireworks‑v1" }
|
21 |
+
];
|
22 |
+
|
23 |
+
const modelSelect = document.getElementById('modelSelect');
|
24 |
+
const analyzeForm = document.getElementById('analyze-form');
|
25 |
+
const inputText = document.getElementById('inputText');
|
26 |
+
const analyzeButton = document.getElementById('analyzeButton');
|
27 |
+
const resultSection = document.getElementById('result');
|
28 |
+
|
29 |
+
let sentimentPipeline = null;
|
30 |
+
|
31 |
+
// Populate the model dropdown
|
32 |
+
AVAILABLE_MODELS.forEach(model => {
|
33 |
+
const option = document.createElement('option');
|
34 |
+
option.value = model.id;
|
35 |
+
option.textContent = model.name;
|
36 |
+
modelSelect.appendChild(option);
|
37 |
+
});
|
38 |
+
|
39 |
+
// Initialize pipeline for the selected model
|
40 |
+
async function initPipeline(modelId) {
|
41 |
+
analyzeButton.disabled = true;
|
42 |
+
analyzeButton.textContent = 'Loading model…';
|
43 |
+
sentimentPipeline = await pipeline('sentiment-analysis', modelId);
|
44 |
+
analyzeButton.textContent = 'Analyze Sentiment';
|
45 |
+
analyzeButton.disabled = false;
|
46 |
+
}
|
47 |
+
|
48 |
+
// On page load: initialize with the first model
|
49 |
+
initPipeline(modelSelect.value);
|
50 |
+
|
51 |
+
// When user changes model: reinitialize pipeline
|
52 |
+
modelSelect.addEventListener('change', () => {
|
53 |
+
resultSection.textContent = '';
|
54 |
+
initPipeline(modelSelect.value);
|
55 |
+
});
|
56 |
+
|
57 |
+
// Handle form submission
|
58 |
+
analyzeForm.addEventListener('submit', async (event) => {
|
59 |
+
event.preventDefault();
|
60 |
+
const text = inputText.value.trim();
|
61 |
+
if (!text) return;
|
62 |
+
|
63 |
+
analyzeButton.disabled = true;
|
64 |
+
analyzeButton.textContent = 'Analyzing…';
|
65 |
+
resultSection.textContent = '';
|
66 |
+
|
67 |
+
try {
|
68 |
+
const [output] = await sentimentPipeline(text);
|
69 |
+
const { label, score } = output;
|
70 |
+
resultSection.textContent = `Sentiment: ${label} (Confidence: ${(score * 100).toFixed(2)}%)`;
|
71 |
+
} catch (err) {
|
72 |
+
console.error(err);
|
73 |
+
resultSection.textContent = 'Error analyzing sentiment.';
|
74 |
+
}
|
75 |
+
|
76 |
+
analyzeButton.textContent = 'Analyze Sentiment';
|
77 |
+
analyzeButton.disabled = false;
|
78 |
+
});
|