mgbam commited on
Commit
25bf9bb
·
verified ·
1 Parent(s): b07d55c

Update static/index.js

Browse files
Files changed (1) hide show
  1. static/index.js +100 -77
static/index.js CHANGED
@@ -1,94 +1,117 @@
1
- /* static/index.js FULL FILE */
 
2
  const MODELS = [
3
- {name:"Qwen/Qwen3‑32B",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:"Qwen3‑235B‑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
- };
 
 
 
 
 
 
 
 
 
 
1
+ /* static/index.js — AnyCoder AI front‑end */
2
+
3
  const MODELS = [
4
+ { name: "Qwen/Qwen3‑32B", id: "Qwen/Qwen3-32B" },
5
+ { name: "Moonshot Kimi‑K2", id: "moonshotai/Kimi-K2-Instruct", provider: "groq" },
6
+ { name: "DeepSeek V3", id: "deepseek-ai/DeepSeek-V3-0324" },
7
+ { name: "DeepSeek R1", id: "deepseek-ai/DeepSeek-R1-0528" },
8
+ { name: "ERNIE‑4.5‑VL", id: "baidu/ERNIE-4.5-VL-424B-A47B-Base-PT" },
9
+ { name: "MiniMax M1", id: "MiniMaxAI/MiniMax-M1-80k" },
10
+ { name: "Qwen3‑235B‑A22B", id: "Qwen/Qwen3-235B-A22B" },
11
+ { name: "SmolLM3‑3B", id: "HuggingFaceTB/SmolLM3-3B" },
12
+ { name: "GLM‑4.1V‑9B‑Thinking", id: "THUDM/GLM-4.1V-9B-Thinking" },
13
+ { name: "OpenAI GPT‑4", id: "openai/gpt-4", provider: "openai" },
14
+ { name: "Gemini Pro", id: "gemini/pro", provider: "gemini" },
15
+ { name: "Fireworks V1", id: "fireworks-ai/fireworks-v1", provider: "fireworks" }
16
  ];
17
+
18
  const LANGS = [
19
+ "python","c","cpp","markdown","latex","json","html","css",
20
+ "javascript","jinja2","typescript","yaml","dockerfile","shell",
21
+ "r","sql","sql-msSQL","sql-mySQL","sql-mariaDB","sql-sqlite",
22
+ "sql-cassandra","sql-plSQL","sql-hive","sql-pgSQL","sql-gql",
23
+ "sql-gpSQL","sql-sparkSQL","sql-esper"
24
  ];
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ /* ---------- DOM ---------- */
27
+ const $ = q => document.querySelector(q);
28
+ const modelSel = $("#model");
29
+ const langSel = $("#language");
30
+ const promptIn = $("#prompt");
31
+ const fileIn = $("#reference‑file");
32
+ const websiteIn = $("#website‑url");
33
+ const searchChk = $("#web‑search");
34
+ const codeOut = $("#code‑out");
35
+ const previewFrame = $("#preview");
36
+ const histList = $("#history‑list");
37
+ const genBtn = $("#generate");
38
+ const clearBtn = $("#clear");
39
+
40
+ /* ---------- populate dropdowns ---------- */
41
+ MODELS.forEach(m => {
42
+ const opt = document.createElement("option");
43
+ opt.value = m.id; opt.textContent = m.name; modelSel.appendChild(opt);
44
  });
45
+ LANGS.forEach(l => {
46
+ const opt = document.createElement("option");
47
+ opt.value = l; opt.textContent = l.toUpperCase(); langSel.appendChild(opt);
48
  });
49
 
50
+ /* ---------- tab logic ---------- */
51
+ document.querySelectorAll(".tabs[role=tablist]").forEach(tablist => {
52
+ tablist.addEventListener("click", e => {
53
+ if (e.target.getAttribute("role") !== "tab") return;
54
+ const tabs = tablist.querySelectorAll("[role=tab]");
55
+ const panels = tablist.parentElement.querySelectorAll("[role=tabpanel]");
56
+ tabs.forEach(t => t.setAttribute("aria-selected", t === e.target));
57
+ panels.forEach(p => p.hidden = p.id !== e.target.getAttribute("aria-controls"));
58
+ });
59
+ });
 
 
 
 
 
60
 
61
+ /* ---------- utilities ---------- */
62
+ const addHistory = text => {
63
+ const li = document.createElement("li");
64
+ li.textContent = `${new Date().toLocaleTimeString()} – ${text.slice(0,40)}…`;
65
  histList.prepend(li);
66
  };
67
 
68
+ /* ---------- clear ---------- */
69
+ clearBtn.addEventListener("click", () => {
70
+ promptIn.value = ""; fileIn.value = ""; websiteIn.value = "";
71
+ codeOut.textContent = ""; previewFrame.srcdoc = ""; histList.innerHTML = "";
72
+ });
73
+
74
  /* ---------- generate ---------- */
75
+ genBtn.addEventListener("click", async () => {
76
+ const prompt = promptIn.value.trim();
77
+ if (!prompt) { alert("Please provide a prompt."); return; }
 
78
 
79
+ genBtn.disabled = true; genBtn.textContent = "Generating…";
80
+
81
+ const body = {
82
+ prompt,
83
+ model_id : modelSel.value,
84
+ language : langSel.value,
85
+ enable_search : searchChk.checked,
86
+ website_url : websiteIn.value || null,
87
+ file_path : null // will be set below if file selected
88
  };
89
+
90
+ if (fileIn.files.length) {
91
+ // upload file via FormData so Gradio saves it and passes path
92
+ const fd = new FormData();
93
+ fd.append("file", fileIn.files[0]);
94
+ const up = await fetch("/upload", { method:"POST", body: fd });
95
+ body.file_path = (await up.json()).file;
96
  }
97
 
98
+ try {
99
+ const res = await fetch("/run/predict", {
100
+ method : "POST",
101
+ headers: { "Content-Type": "application/json" },
102
+ body : JSON.stringify(body)
103
+ });
104
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
105
+ const [code] = await res.json(); // ← Gradio returns [code, history]
106
+ codeOut.textContent = code;
107
+ previewFrame.srcdoc = langSel.value === "html"
108
+ ? code
109
+ : `<pre style="white-space:pre-wrap">${code.replace(/</g,"&lt;")}</pre>`;
110
+ addHistory(prompt);
111
+ } catch (err) {
112
+ console.error(err);
113
+ alert("Generation failed – check console.");
114
+ } finally {
115
+ genBtn.disabled = false; genBtn.textContent = "Generate code";
116
+ }
117
+ });