mgbam commited on
Commit
3380f8e
·
verified ·
1 Parent(s): 0bcbcf1

Update static/index.js

Browse files
Files changed (1) hide show
  1. static/index.js +117 -115
static/index.js CHANGED
@@ -1,131 +1,133 @@
1
- /* static/index.js
2
  ----------------------------------------------------------
3
- AnyCoderAI – interactive front‑end
4
  ---------------------------------------------------------- */
5
 
6
- (() => {
7
- /* ---------- 1. MODEL CATALOG -------------------------------- */
8
- const AVAILABLE_MODELS = [
9
- { name: "Qwen/Qwen332B", id: "Qwen/Qwen3-32B" },
10
- { name: "Moonshot Kimi‑K2", id: "moonshotai/Kimi-K2-Instruct", provider: "groq" },
11
- { name: "DeepSeek V3", id: "deepseek-ai/DeepSeek-V3-0324" },
12
- { name: "DeepSeek R1", id: "deepseek-ai/DeepSeek-R1-0528" },
13
- { name: "ERNIE‑4.5‑VL", id: "baidu/ERNIE-4.5-VL-424B-A47B-Base-PT" },
14
- { name: "MiniMax M1", id: "MiniMaxAI/MiniMax-M1-80k" },
15
- { name: "Qwen3235B‑A22B", id: "Qwen/Qwen3-235B-A22B" },
16
- { name: "SmolLM33B", 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", provider: "openai" },
19
- { name: "Gemini Pro", id: "gemini/pro", provider: "gemini" },
20
- { name: "Fireworks V1", id: "fireworks-ai/fireworks-v1", provider: "fireworks" }
21
- ];
22
 
23
- /* ---------- 2. DOM SHORTCUTS -------------------------------- */
24
- const $ = (q) => document.querySelector(q);
25
- const modelSel = $('#model');
26
- const promptIn = $('#prompt');
27
- const fileIn = $('#reference-file');
28
- const urlIn = $('#website-url');
29
- const langSel = $('#language');
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
- /* ---------- 3. INIT MODEL DROPDOWN -------------------------- */
38
- for (const m of AVAILABLE_MODELS) {
39
- const opt = document.createElement('option');
40
- opt.value = m.id;
41
- opt.textContent = m.name;
42
- opt.dataset.provider = m.provider || '';
43
- modelSel.appendChild(opt);
44
- }
 
 
 
 
 
45
 
46
- /* ---------- 4. TABS ---------------------------------------- */
47
- function activateTab(btn) {
48
- const tabs = btn.parentElement.querySelectorAll('[role="tab"]');
49
- const panels = btn.closest('section').querySelectorAll('[role="tabpanel"]');
50
- tabs.forEach(t => {
51
- const sel = t === btn;
52
- t.setAttribute('aria-selected', sel);
53
- t.tabIndex = sel ? 0 : -1;
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
- /* ---------- 5. UTILITIES ----------------------------------- */
62
- const toBase64 = (file) =>
63
- new Promise((res, rej) => {
64
- const reader = new FileReader();
65
- reader.onload = () => res(reader.result.split(',')[1]);
66
- reader.onerror = rej;
67
- reader.readAsDataURL(file);
68
- });
69
 
70
- const addHistory = (text) => {
71
- const li = document.createElement('li');
72
- li.textContent = `${new Date().toLocaleTimeString()} — ${text.slice(0, 40)}…`;
73
- histList.prepend(li);
74
- };
 
 
 
75
 
76
- const resetUI = () => {
77
- promptIn.value = '';
78
- fileIn.value = '';
79
- urlIn.value = '';
80
- codeOut.textContent = '';
81
- previewOut.srcdoc = '';
82
- histList.innerHTML = '';
83
- };
 
 
 
 
 
 
 
 
84
 
85
- /* ---------- 6. CLEAR SESSION ------------------------------- */
86
- clearBtn.addEventListener('click', resetUI);
 
 
87
 
88
- /* ---------- 7. GENERATE (CALL BACK‑END) -------------------- */
89
- genBtn.addEventListener('click', async () => {
90
- const prompt = promptIn.value.trim();
91
- if (!prompt) { alert('Please provide a prompt.'); return; }
92
 
93
- genBtn.disabled = true;
94
- genBtn.textContent = 'Generating…';
 
 
 
 
 
 
95
 
96
- const payload = {
97
- prompt,
98
- model_name: modelSel.options[modelSel.selectedIndex].textContent,
99
- language: langSel.value,
100
- web_search: searchChk.checked,
101
- website_url: urlIn.value || null
102
- };
103
 
104
- /* optional file */
105
- if (fileIn.files.length) {
106
- const file = fileIn.files[0];
107
- payload.file_name = file.name;
108
- payload.file_data = await toBase64(file);
109
- }
 
 
 
 
110
 
111
- try {
112
- const res = await fetch('/predict', {
113
- method: 'POST',
114
- headers: { 'Content-Type': 'application/json' },
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
- codeOut.textContent = code || '// (empty)';
121
- previewOut.srcdoc = preview || `<pre>${code.replace(/</g, '&lt;')}</pre>`;
122
- addHistory(prompt);
123
- } catch (err) {
124
- console.error(err);
125
- alert('Generation failed see console for details.');
126
- } finally {
127
- genBtn.disabled = false;
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 KimiK2", 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: "SmolLM33B", id: "HuggingFaceTB/SmolLM3-3B" },
16
+ { name: "GLM4.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,'&lt;')}</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
+ });