mgbam commited on
Commit
973db88
·
verified ·
1 Parent(s): 4476a53

Update static/index.js

Browse files
Files changed (1) hide show
  1. static/index.js +79 -118
static/index.js CHANGED
@@ -1,133 +1,94 @@
1
- /* static/index.js FULL FILE
2
- ----------------------------------------------------------
3
- AnyCoder AI frontend 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: "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,'&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
- });
 
1
+ /* static/index.js FULL FILE */
2
+ const MODELS = [
3
+ {name:"Qwen/Qwen332B",id:"Qwen/Qwen3-32B"},
4
+ {name:"Moonshot Kimi‑K2",id:"moonshotai/Kimi-K2-Instruct",provider:"groq"},
5
+ {name:"DeepSeek V3",id:"deepseek-ai/DeepSeek-V3-0324"},
6
+ {name:"DeepSeek R1",id:"deepseek-ai/DeepSeek-R1-0528"},
7
+ {name:"ERNIE‑4.5‑VL",id:"baidu/ERNIE-4.5-VL-424B-A47B-Base-PT"},
8
+ {name:"MiniMax M1",id:"MiniMaxAI/MiniMax-M1-80k"},
9
+ {name:"Qwen3235B‑A22B",id:"Qwen/Qwen3-235B-A22B"},
10
+ {name:"SmolLM3‑3B",id:"HuggingFaceTB/SmolLM3-3B"},
11
+ {name:"GLM‑4.1V‑9B‑Thinking",id:"THUDM/GLM-4.1V-9B-Thinking"},
12
+ {name:"OpenAI GPT‑4",id:"openai/gpt-4",provider:"openai"},
13
+ {name:"Gemini Pro",id:"gemini/pro",provider:"gemini"},
14
+ {name:"Fireworks V1",id:"fireworks-ai/fireworks-v1",provider:"fireworks"}
 
 
 
 
 
 
15
  ];
16
+ const LANGS = [
17
+ "python","c","cpp","markdown","latex","json","html","css","javascript",
18
+ "jinja2","typescript","yaml","dockerfile","shell","r","sql","sql-msSQL",
19
+ "sql-mySQL","sql-mariaDB","sql-sqlite","sql-cassandra","sql-plSQL",
20
+ "sql-hive","sql-pgSQL","sql-gql","sql-gpSQL","sql-sparkSQL","sql-esper"
 
 
21
  ];
22
+ /* ---------- dom ---------- */
23
+ const $ = q => document.querySelector(q);
24
+ const modelSel = $('#model');
25
+ const promptIn = $('#prompt');
26
+ const fileIn = $('#reference-file');
27
+ const urlIn = $('#website-url');
28
+ const langSel = $('#language');
29
+ const wsCheck = $('#web-search');
30
+ const codeOut = $('#code-output');
31
+ const preview = $('#preview');
32
+ const histList = $('#history-list');
33
+ const genBtn = $('#generate');
34
+ const clrBtn = $('#clear');
35
 
36
+ /* ---------- populate ---------- */
37
+ MODELS.forEach(m=>{
38
+ const o=document.createElement('option');
39
+ o.value=m.id;o.textContent=m.name;o.dataset.provider=m.provider||'';modelSel.appendChild(o);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  });
41
+ LANGS.forEach(l=>{
42
+ const o=document.createElement('option');
43
+ o.value=o.textContent=l;langSel.appendChild(o);
 
 
 
44
  });
45
 
46
+ /* ---------- tabs ---------- */
47
  document.querySelectorAll('.tabs[role="tablist"] button')
48
+ .forEach(btn=>btn.addEventListener('click',()=>{
49
+ const list=btn.parentElement;
50
+ list.querySelectorAll('[role="tab"]').forEach(t=>{
51
+ const sel=t===btn;t.setAttribute('aria-selected',sel);
52
+ list.parentElement.querySelector(`#${t.getAttribute('aria-controls')}`).hidden=!sel;
53
+ });
54
  }));
55
 
56
+ /* ---------- clear ---------- */
57
+ clrBtn.onclick=()=>{
58
+ promptIn.value='';fileIn.value='';urlIn.value='';
59
+ codeOut.textContent='';preview.srcdoc='';histList.innerHTML='';
60
+ };
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ /* ---------- helper ---------- */
63
+ const addHist=p=>{
64
+ const li=document.createElement('li');
65
+ li.textContent=`${new Date().toLocaleTimeString()} ${p.slice(0,40)}…`;
66
+ histList.prepend(li);
67
+ };
68
 
69
+ /* ---------- generate ---------- */
70
+ genBtn.onclick=async()=>{
71
+ const prompt=promptIn.value.trim();
72
+ if(!prompt){alert('Please provide a prompt');return;}
73
+ genBtn.disabled=true;genBtn.textContent='Generating…';
74
 
75
+ const body={
76
+ prompt, model_id:modelSel.value, language:langSel.value,
77
+ web_search:wsCheck.checked, website_url:urlIn.value||null
 
 
 
 
78
  };
79
+ if(fileIn.files.length){
80
+ body.file_name=fileIn.files[0].name;
81
+ body.file_data=Array.from(new Uint8Array(await fileIn.files[0].arrayBuffer())); // binary to int[]
 
 
 
82
  }
83
 
84
+ try{
85
+ const res=await fetch('/run/predict',{
86
+ method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});
87
+ if(!res.ok) throw new Error(await res.text());
88
+ const {code}=await res.json();
89
+ codeOut.textContent=code;
90
+ preview.srcdoc=(langSel.value==='html')?code:`<pre>${code.replace(/</g,'&lt;')}</pre>`;
91
+ addHist(prompt);
92
+ }catch(e){console.error(e);alert('Error see console');}
93
+ genBtn.disabled=false;genBtn.textContent='Generate Code';
94
+ };