mgbam commited on
Commit
3c5da9c
·
verified ·
1 Parent(s): 8c0571c

Update static/index.js

Browse files
Files changed (1) hide show
  1. static/index.js +115 -115
static/index.js CHANGED
@@ -1,131 +1,131 @@
1
  /* static/index.js
2
  ----------------------------------------------------------
3
- AnyCoder AI front‑end logic
4
  ---------------------------------------------------------- */
5
 
6
- /* ~~~~~~~~~~~~~~~~~~~~~~~ 1. MODEL LIST ~~~~~~~~~~~~~~~~~~~~~~~ */
7
- 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
- ];
 
21
 
22
- /* ~~~~~~~~~~~~~~~~~~~~~~~ 2. DOM HOOKS ~~~~~~~~~~~~~~~~~~~~~~~ */
23
- const $ = (q) => document.querySelector(q);
24
- const modelSelect = $('#model');
25
- const promptInput = $('#prompt');
26
- const fileInput = $('#reference-file');
27
- const websiteInput = $('#website-url');
28
- const langSelect = $('#language');
29
- const webSearchChk = $('#web-search');
30
- const codeOut = $('#code-output');
31
- const previewFrame = $('#preview');
32
- const historyList = $('#history-list');
33
- const genBtn = $('#generate');
34
- const clearBtn = $('#clear');
35
 
36
- /* ~~~~~~~~~~~~~~~~~~~~~~~ 3. POPULATE MODELS ~~~~~~~~~~~~~~~~~~ */
37
- for (const m of AVAILABLE_MODELS) {
38
- const opt = document.createElement('option');
39
- opt.value = m.id;
40
- opt.textContent = m.name;
41
- opt.dataset.provider = m.provider || '';
42
- modelSelect.appendChild(opt);
43
- }
44
 
45
- /* ~~~~~~~~~~~~~~~~~~~~~~~ 4. TAB HANDLING ~~~~~~~~~~~~~~~~~~~~~ */
46
- function activateTab(tabBtn) {
47
- const tabList = tabBtn.parentElement;
48
- const tabs = tabList.querySelectorAll('[role="tab"]');
49
- const panels = tabList.parentElement.querySelectorAll('[role="tabpanel"]');
50
- tabs.forEach((t) => {
51
- const selected = t === tabBtn;
52
- t.setAttribute('aria-selected', selected);
53
- panels
54
- .forEach((p) => (p.hidden = p.id !== t.getAttribute('aria-controls')));
55
- });
56
- }
57
- document.querySelectorAll('.tabs[role="tablist"] button')
58
- .forEach((btn) => btn.addEventListener('click', () => activateTab(btn)));
59
 
60
- /* ~~~~~~~~~~~~~~~~~~~~~~~ 5. CLEAR SESSION ~~~~~~~~~~~~~~~~~~~~ */
61
- clearBtn.addEventListener('click', () => {
62
- promptInput.value = '';
63
- fileInput.value = '';
64
- websiteInput.value = '';
65
- codeOut.textContent = '';
66
- previewFrame.srcdoc = '';
67
- historyList.innerHTML = '';
68
- });
69
 
70
- /* ~~~~~~~~~~~~~~~~~~~~~~~ 6. HELPERS ~~~~~~~~~~~~~~~~~~~~~~~~~~ */
71
- function addHistoryEntry(prompt) {
72
- const li = document.createElement('li');
73
- li.textContent =
74
- `${new Date().toLocaleTimeString()} – ${prompt.slice(0, 40)}…`;
75
- historyList.prepend(li);
76
- }
77
 
78
- /* ~~~~~~~~~~~~~~~~~~~~~~~ 7. GENERATE (CALL BACKEND) ~~~~~~~~~~ */
79
- genBtn.addEventListener('click', async () => {
80
- const prompt = promptInput.value.trim();
81
- if (!prompt) {
82
- alert('Please provide a prompt.');
83
- return;
84
- }
 
85
 
86
- genBtn.disabled = true;
87
- genBtn.textContent = 'Generating…';
88
 
89
- /* Build request payload */
90
- const body = {
91
- prompt,
92
- model_id: modelSelect.value,
93
- language: langSelect.value,
94
- web_search: webSearchChk.checked,
95
- website_url: websiteInput.value || null
96
- };
97
 
98
- /* File upload (optional) */
99
- if (fileInput.files.length) {
100
- const file = fileInput.files[0];
101
- body.file_name = file.name;
102
- body.file_data = await file.arrayBuffer(); // could base64 if needed
103
- }
104
 
105
- try {
106
- /* ---- POST to /predict ---------------------------------- */
107
- const res = await fetch('/predict', {
108
- method: 'POST',
109
- headers: { 'Content-Type': 'application/json' },
110
- body: JSON.stringify(body)
111
- });
112
- if (!res.ok) throw new Error(res.statusText);
113
- const data = await res.json();
114
 
115
- /* ---- Display results ----------------------------------- */
116
- codeOut.textContent = data.code;
117
- if (langSelect.value === 'html') {
118
- previewFrame.srcdoc = data.code;
119
- } else {
120
- previewFrame.srcdoc =
121
- `<pre style="white-space:pre-wrap">${data.code.replace(/</g, '&lt;')}</pre>`;
122
  }
123
- addHistoryEntry(prompt);
124
- } catch (err) {
125
- console.error(err);
126
- alert('Error generating code – check console.');
127
- } finally {
128
- genBtn.disabled = false;
129
- genBtn.textContent = 'Generate Code';
130
- }
131
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ })();