mgbam commited on
Commit
a2648f3
·
verified ·
1 Parent(s): 8a660e5

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +26 -22
index.js CHANGED
@@ -1,8 +1,8 @@
1
- --- index.js ---
2
- ```javascript
3
  import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers';
4
 
5
- // Frontend copy of AVAILABLE_MODELS from constants.py
6
  const AVAILABLE_MODELS = [
7
  { name: "Moonshot Kimi-K2", id: "moonshotai/Kimi-K2-Instruct" },
8
  { name: "DeepSeek V3", id: "deepseek-ai/DeepSeek-V3-0324" },
@@ -16,55 +16,59 @@ const AVAILABLE_MODELS = [
16
  { name: "Qwen3-Coder-480B-A35B", id: "Qwen/Qwen3-Coder-480B-A35B-Instruct" }
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 dropdown
26
  AVAILABLE_MODELS.forEach(model => {
27
- const opt = document.createElement('option');
28
- opt.value = model.id;
29
- opt.textContent = model.name;
30
- modelSelect.appendChild(opt);
31
  });
32
 
 
33
  async function initPipeline(modelId) {
34
  analyzeButton.disabled = true;
35
- analyzeButton.textContent = 'Loading model...';
36
  sentimentPipeline = await pipeline('sentiment-analysis', modelId);
37
  analyzeButton.textContent = 'Analyze Sentiment';
38
  analyzeButton.disabled = false;
39
  }
40
 
41
- // Initialize with first model
42
  initPipeline(modelSelect.value);
43
 
44
- // Re-init on change
45
  modelSelect.addEventListener('change', () => {
46
- initPipeline(modelSelect.value);
47
  resultSection.textContent = '';
 
48
  });
49
 
50
  // Handle form submission
51
- document.getElementById('analyze-form').addEventListener('submit', async event => {
52
  event.preventDefault();
53
  const text = inputText.value.trim();
54
  if (!text) return;
55
 
56
  analyzeButton.disabled = true;
57
- analyzeButton.textContent = 'Analyzing...';
58
  resultSection.textContent = '';
59
 
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
  }
67
 
68
  analyzeButton.textContent = 'Analyze Sentiment';
69
  analyzeButton.disabled = false;
70
- });
 
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" },
 
16
  { name: "Qwen3-Coder-480B-A35B", id: "Qwen/Qwen3-Coder-480B-A35B-Instruct" }
17
  ];
18
 
19
+ const modelSelect = document.getElementById('modelSelect');
20
+ const analyzeForm = document.getElementById('analyze-form');
21
+ const inputText = document.getElementById('inputText');
22
  const analyzeButton = document.getElementById('analyzeButton');
23
+ const resultSection = document.getElementById('result');
24
+
25
+ let sentimentPipeline = null;
26
 
27
+ // Populate the model dropdown
28
  AVAILABLE_MODELS.forEach(model => {
29
+ const option = document.createElement('option');
30
+ option.value = model.id;
31
+ option.textContent = model.name;
32
+ modelSelect.appendChild(option);
33
  });
34
 
35
+ // Initialize pipeline for the selected model
36
  async function initPipeline(modelId) {
37
  analyzeButton.disabled = true;
38
+ analyzeButton.textContent = 'Loading model';
39
  sentimentPipeline = await pipeline('sentiment-analysis', modelId);
40
  analyzeButton.textContent = 'Analyze Sentiment';
41
  analyzeButton.disabled = false;
42
  }
43
 
44
+ // On page load: initialize with the first model
45
  initPipeline(modelSelect.value);
46
 
47
+ // When user changes model: reinitialize pipeline
48
  modelSelect.addEventListener('change', () => {
 
49
  resultSection.textContent = '';
50
+ initPipeline(modelSelect.value);
51
  });
52
 
53
  // Handle form submission
54
+ analyzeForm.addEventListener('submit', async (event) => {
55
  event.preventDefault();
56
  const text = inputText.value.trim();
57
  if (!text) return;
58
 
59
  analyzeButton.disabled = true;
60
+ analyzeButton.textContent = 'Analyzing';
61
  resultSection.textContent = '';
62
 
63
  try {
64
+ const [output] = await sentimentPipeline(text);
65
+ const { label, score } = output;
66
+ resultSection.textContent = `Sentiment: ${label} (Confidence: ${(score * 100).toFixed(2)}%)`;
67
  } catch (err) {
68
+ console.error(err);
69
  resultSection.textContent = 'Error analyzing sentiment.';
70
  }
71
 
72
  analyzeButton.textContent = 'Analyze Sentiment';
73
  analyzeButton.disabled = false;
74
+ });