Update index.js
Browse files
index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
-
// index.js
|
| 2 |
-
|
| 3 |
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers';
|
| 4 |
|
| 5 |
-
// Front-end copy of the
|
| 6 |
const AVAILABLE_MODELS = [
|
| 7 |
{ name: "Moonshot Kimi-K2", id: "moonshotai/Kimi-K2-Instruct" },
|
| 8 |
{ name: "DeepSeek V3", id: "deepseek-ai/DeepSeek-V3-0324" },
|
|
@@ -14,64 +12,74 @@ const AVAILABLE_MODELS = [
|
|
| 14 |
{ name: "GLM-4.1V-9B-Thinking", id: "THUDM/GLM-4.1V-9B-Thinking" },
|
| 15 |
{ name: "Qwen3-235B-A22B-Instruct-2507", id: "Qwen/Qwen3-235B-A22B-Instruct-2507" },
|
| 16 |
{ name: "Qwen3-Coder-480B-A35B", id: "Qwen/Qwen3-Coder-480B-A35B-Instruct" },
|
| 17 |
-
// New providers:
|
| 18 |
{ name: "OpenAI GPT-4", id: "openai/gpt-4" },
|
| 19 |
-
{ name: "Gemini Pro", id: "gemini/pro" }
|
|
|
|
| 20 |
];
|
| 21 |
|
|
|
|
| 22 |
const modelSelect = document.getElementById('modelSelect');
|
| 23 |
-
const
|
| 24 |
-
const
|
| 25 |
-
const
|
| 26 |
-
const resultSection = document.getElementById('result');
|
| 27 |
|
| 28 |
-
let
|
| 29 |
|
| 30 |
-
// Populate
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
// Initialize pipeline for
|
| 39 |
async function initPipeline(modelId) {
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
}
|
| 46 |
|
| 47 |
-
//
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
modelSelect.addEventListener('change', () => {
|
| 52 |
-
resultSection.textContent = '';
|
| 53 |
-
initPipeline(modelSelect.value);
|
| 54 |
-
});
|
| 55 |
-
|
| 56 |
-
// Handle form submission
|
| 57 |
-
analyzeForm.addEventListener('submit', async (event) => {
|
| 58 |
-
event.preventDefault();
|
| 59 |
-
const text = inputText.value.trim();
|
| 60 |
-
if (!text) return;
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
|
| 66 |
try {
|
| 67 |
-
const
|
| 68 |
-
const
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
} catch (err) {
|
| 71 |
console.error(err);
|
| 72 |
-
|
| 73 |
}
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers';
|
| 2 |
|
| 3 |
+
// Front-end copy of the available models for 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" },
|
|
|
|
| 12 |
{ name: "GLM-4.1V-9B-Thinking", id: "THUDM/GLM-4.1V-9B-Thinking" },
|
| 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: "OpenAI GPT-4", id: "openai/gpt-4" },
|
| 16 |
+
{ name: "Gemini Pro", id: "gemini/pro" },
|
| 17 |
+
{ name: "Fireworks AI", id: "fireworks-ai/fireworks-v1" }
|
| 18 |
];
|
| 19 |
|
| 20 |
+
// DOM Elements
|
| 21 |
const modelSelect = document.getElementById('modelSelect');
|
| 22 |
+
const promptInput = document.getElementById('promptInput');
|
| 23 |
+
const generateBtn = document.getElementById('generateBtn');
|
| 24 |
+
const codeOutput = document.getElementById('codeOutput');
|
|
|
|
| 25 |
|
| 26 |
+
let codePipeline = null;
|
| 27 |
|
| 28 |
+
// Populate model dropdown
|
| 29 |
+
function populateModels() {
|
| 30 |
+
AVAILABLE_MODELS.forEach(model => {
|
| 31 |
+
const option = document.createElement('option');
|
| 32 |
+
option.value = model.id;
|
| 33 |
+
option.textContent = model.name;
|
| 34 |
+
modelSelect.appendChild(option);
|
| 35 |
+
});
|
| 36 |
+
}
|
| 37 |
|
| 38 |
+
// Initialize pipeline for code generation
|
| 39 |
async function initPipeline(modelId) {
|
| 40 |
+
generateBtn.disabled = true;
|
| 41 |
+
generateBtn.textContent = 'Loading model…';
|
| 42 |
+
codePipeline = await pipeline('text-generation', modelId, { trustRemoteCode: true });
|
| 43 |
+
generateBtn.textContent = 'Generate Code';
|
| 44 |
+
generateBtn.disabled = false;
|
| 45 |
}
|
| 46 |
|
| 47 |
+
// Handle generation
|
| 48 |
+
async function handleGenerate() {
|
| 49 |
+
const prompt = promptInput.value.trim();
|
| 50 |
+
if (!prompt) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
generateBtn.disabled = true;
|
| 53 |
+
generateBtn.textContent = 'Generating…';
|
| 54 |
+
codeOutput.textContent = '';
|
| 55 |
|
| 56 |
try {
|
| 57 |
+
const outputs = await codePipeline(prompt, { max_new_tokens: 512 });
|
| 58 |
+
const generated = Array.isArray(outputs)
|
| 59 |
+
? outputs.map(o => o.generated_text).join('')
|
| 60 |
+
: outputs.generated_text;
|
| 61 |
+
codeOutput.textContent = generated;
|
| 62 |
} catch (err) {
|
| 63 |
console.error(err);
|
| 64 |
+
codeOutput.textContent = 'Error generating code.';
|
| 65 |
}
|
| 66 |
|
| 67 |
+
generateBtn.textContent = 'Generate Code';
|
| 68 |
+
generateBtn.disabled = false;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// Event bindings
|
| 72 |
+
window.addEventListener('DOMContentLoaded', async () => {
|
| 73 |
+
populateModels();
|
| 74 |
+
await initPipeline(modelSelect.value);
|
| 75 |
+
});
|
| 76 |
+
|
| 77 |
+
modelSelect.addEventListener('change', () => {
|
| 78 |
+
codeOutput.textContent = '';
|
| 79 |
+
initPipeline(modelSelect.value);
|
| 80 |
});
|
| 81 |
+
|
| 82 |
+
generateBtn.addEventListener('click', handleGenerate);
|
| 83 |
+
|
| 84 |
+
// Expose functions (for debugging)
|
| 85 |
+
window._codeApp = { initPipeline, handleGenerate };
|