Update static/index.js
Browse files- static/index.js +117 -115
static/index.js
CHANGED
|
@@ -1,131 +1,133 @@
|
|
| 1 |
-
/* static/index.js
|
| 2 |
----------------------------------------------------------
|
| 3 |
-
AnyCoder
|
| 4 |
---------------------------------------------------------- */
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
const searchChk = $('#web-search');
|
| 31 |
-
const codeOut = $('#code-output');
|
| 32 |
-
const previewOut = $('#preview');
|
| 33 |
-
const histList = $('#history-list');
|
| 34 |
-
const genBtn = $('#generate');
|
| 35 |
-
const clearBtn = $('#clear');
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
panels.forEach(p => p.hidden = p.id !== t.getAttribute('aria-controls'));
|
| 55 |
-
});
|
| 56 |
-
}
|
| 57 |
-
document
|
| 58 |
-
.querySelectorAll('.tabs[role="tablist"] button')
|
| 59 |
-
.forEach(btn => btn.addEventListener('click', () => activateTab(btn)));
|
| 60 |
|
| 61 |
-
|
| 62 |
-
const
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
reader.readAsDataURL(file);
|
| 68 |
-
});
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
genBtn.addEventListener('click', async () => {
|
| 90 |
-
const prompt = promptIn.value.trim();
|
| 91 |
-
if (!prompt) { alert('Please provide a prompt.'); return; }
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
};
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
body: JSON.stringify(payload)
|
| 116 |
-
});
|
| 117 |
-
if (!res.ok) throw new Error(await res.text());
|
| 118 |
-
const { code, preview } = await res.json();
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
genBtn.textContent = 'Generate Code';
|
| 129 |
-
}
|
| 130 |
-
});
|
| 131 |
-
})();
|
|
|
|
| 1 |
+
/* static/index.js – FULL FILE
|
| 2 |
----------------------------------------------------------
|
| 3 |
+
AnyCoder AI front‑end logic
|
| 4 |
---------------------------------------------------------- */
|
| 5 |
|
| 6 |
+
/* ~~~~~~~~~~~~~ 1. MODEL & LANGUAGE LISTS ~~~~~~~~~~~~~ */
|
| 7 |
+
export const AVAILABLE_MODELS = [
|
| 8 |
+
{ name: "Qwen/Qwen3‑32B", id: "Qwen/Qwen3-32B" },
|
| 9 |
+
{ name: "Moonshot Kimi‑K2", id: "moonshotai/Kimi-K2-Instruct", provider: "groq" },
|
| 10 |
+
{ name: "DeepSeek V3", id: "deepseek-ai/DeepSeek-V3-0324" },
|
| 11 |
+
{ name: "DeepSeek R1", id: "deepseek-ai/DeepSeek-R1-0528" },
|
| 12 |
+
{ name: "ERNIE‑4.5‑VL", id: "baidu/ERNIE-4.5-VL-424B-A47B-Base-PT" },
|
| 13 |
+
{ name: "MiniMax M1", id: "MiniMaxAI/MiniMax-M1-80k" },
|
| 14 |
+
{ name: "Qwen3‑235B‑A22B", id: "Qwen/Qwen3-235B-A22B" },
|
| 15 |
+
{ name: "SmolLM3‑3B", id: "HuggingFaceTB/SmolLM3-3B" },
|
| 16 |
+
{ name: "GLM‑4.1V‑9B‑Thinking", id: "THUDM/GLM-4.1V-9B-Thinking" },
|
| 17 |
+
{ name: "OpenAI GPT‑4", id: "openai/gpt-4", provider: "openai" },
|
| 18 |
+
{ name: "Gemini Pro", id: "gemini/pro", provider: "gemini" },
|
| 19 |
+
{ name: "Fireworks V1", id: "fireworks-ai/fireworks-v1", provider: "fireworks" },
|
| 20 |
+
// keep this array in sync with models.py if you add more!
|
| 21 |
+
];
|
| 22 |
|
| 23 |
+
const LANGUAGES = [
|
| 24 |
+
"python","c","cpp","markdown","latex","json","html","css",
|
| 25 |
+
"javascript","jinja2","typescript","yaml","dockerfile","shell",
|
| 26 |
+
"r","sql","sql-msSQL","sql-mySQL","sql-mariaDB","sql-sqlite",
|
| 27 |
+
"sql-cassandra","sql-plSQL","sql-hive","sql-pgSQL","sql-gql",
|
| 28 |
+
"sql-gpSQL","sql-sparkSQL","sql-esper"
|
| 29 |
+
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
/* ~~~~~~~~~~~~~ 2. DOM REFERENCES ~~~~~~~~~~~~~ */
|
| 32 |
+
const $ = (q) => document.querySelector(q);
|
| 33 |
+
const modelSelect = $('#model');
|
| 34 |
+
const promptInput = $('#prompt');
|
| 35 |
+
const fileInput = $('#reference-file');
|
| 36 |
+
const websiteInput = $('#website-url');
|
| 37 |
+
const langSelect = $('#language');
|
| 38 |
+
const webSearchChk = $('#web-search');
|
| 39 |
+
const codeOut = $('#code-output');
|
| 40 |
+
const previewFrame = $('#preview');
|
| 41 |
+
const historyList = $('#history-list');
|
| 42 |
+
const genBtn = $('#generate');
|
| 43 |
+
const clearBtn = $('#clear');
|
| 44 |
|
| 45 |
+
/* ~~~~~~~~~~~~~ 3. POPULATE SELECTS ~~~~~~~~~~~~~ */
|
| 46 |
+
AVAILABLE_MODELS.forEach(m => {
|
| 47 |
+
const opt = document.createElement('option');
|
| 48 |
+
opt.value = m.id;
|
| 49 |
+
opt.textContent = m.name;
|
| 50 |
+
opt.dataset.provider = m.provider || '';
|
| 51 |
+
modelSelect.appendChild(opt);
|
| 52 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
LANGUAGES.forEach(lang => {
|
| 55 |
+
const opt = document.createElement('option');
|
| 56 |
+
opt.value = lang;
|
| 57 |
+
opt.textContent = lang;
|
| 58 |
+
langSelect.appendChild(opt);
|
| 59 |
+
});
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
/* ~~~~~~~~~~~~~ 4. TAB HANDLING ~~~~~~~~~~~~~ */
|
| 62 |
+
document.querySelectorAll('.tabs[role="tablist"] button')
|
| 63 |
+
.forEach(btn => btn.addEventListener('click', () => {
|
| 64 |
+
const tabs = btn.parentElement.querySelectorAll('[role="tab"]');
|
| 65 |
+
const panels = btn.parentElement.parentElement.querySelectorAll('[role="tabpanel"]');
|
| 66 |
+
tabs.forEach(t => { t.setAttribute('aria-selected', t === btn); });
|
| 67 |
+
panels.forEach(p => { p.hidden = p.id !== btn.getAttribute('aria-controls'); });
|
| 68 |
+
}));
|
| 69 |
|
| 70 |
+
/* ~~~~~~~~~~~~~ 5. CLEAR SESSION ~~~~~~~~~~~~~ */
|
| 71 |
+
clearBtn.addEventListener('click', () => {
|
| 72 |
+
promptInput.value = '';
|
| 73 |
+
fileInput.value = '';
|
| 74 |
+
websiteInput.value = '';
|
| 75 |
+
codeOut.textContent = '';
|
| 76 |
+
previewFrame.srcdoc = '';
|
| 77 |
+
historyList.innerHTML = '';
|
| 78 |
+
});
|
| 79 |
+
|
| 80 |
+
/* ~~~~~~~~~~~~~ 6. HISTORY HELPER ~~~~~~~~~~~~~ */
|
| 81 |
+
function logHistory(text) {
|
| 82 |
+
const li = document.createElement('li');
|
| 83 |
+
li.textContent = `${new Date().toLocaleTimeString()} – ${text.slice(0,40)}…`;
|
| 84 |
+
historyList.prepend(li);
|
| 85 |
+
}
|
| 86 |
|
| 87 |
+
/* ~~~~~~~~~~~~~ 7. GENERATE → BACKEND ~~~~~~~~~~~~~ */
|
| 88 |
+
genBtn.addEventListener('click', async () => {
|
| 89 |
+
const prompt = promptInput.value.trim();
|
| 90 |
+
if (!prompt) { alert('Please provide a prompt.'); return; }
|
| 91 |
|
| 92 |
+
genBtn.disabled = true; genBtn.textContent = 'Generating…';
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
/* assemble payload */
|
| 95 |
+
const body = {
|
| 96 |
+
prompt,
|
| 97 |
+
model_id: modelSelect.value,
|
| 98 |
+
language: langSelect.value,
|
| 99 |
+
web_search: webSearchChk.checked,
|
| 100 |
+
website_url: websiteInput.value || null
|
| 101 |
+
};
|
| 102 |
|
| 103 |
+
/* optional file */
|
| 104 |
+
if (fileInput.files.length) {
|
| 105 |
+
const file = fileInput.files[0];
|
| 106 |
+
body.file_name = file.name;
|
| 107 |
+
body.file_data = await file.text();
|
| 108 |
+
}
|
|
|
|
| 109 |
|
| 110 |
+
/* post to Gradio back‑end */
|
| 111 |
+
try {
|
| 112 |
+
const res = await fetch('/run/predict', {
|
| 113 |
+
method: 'POST',
|
| 114 |
+
headers: { 'Content-Type':'application/json' },
|
| 115 |
+
body: JSON.stringify(body)
|
| 116 |
+
});
|
| 117 |
+
if (!res.ok) throw new Error(await res.text());
|
| 118 |
+
const { data } = await res.json(); // Gradio wraps result in {data:[...]}
|
| 119 |
+
const [code] = data;
|
| 120 |
|
| 121 |
+
codeOut.textContent = code;
|
| 122 |
+
previewFrame.srcdoc = langSelect.value === 'html'
|
| 123 |
+
? code
|
| 124 |
+
: `<pre style="white-space:pre-wrap">${code.replace(/</g,'<')}</pre>`;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
logHistory(prompt);
|
| 127 |
+
} catch (err) {
|
| 128 |
+
console.error(err);
|
| 129 |
+
codeOut.textContent = `/* ERROR: ${err.message} */`;
|
| 130 |
+
} finally {
|
| 131 |
+
genBtn.disabled = false; genBtn.textContent = 'Generate Code';
|
| 132 |
+
}
|
| 133 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|