mgbam commited on
Commit
ca950c4
Β·
verified Β·
1 Parent(s): d87506b

Update static/index.js

Browse files
Files changed (1) hide show
  1. static/index.js +72 -71
static/index.js CHANGED
@@ -1,20 +1,25 @@
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",
@@ -23,95 +28,91 @@ const LANGS = [
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
  });
 
1
+ /* static/index.js – AnyCoderΒ front‑end */
2
 
3
+ // ───────────────── 1. MODEL REGISTRY ──────────────────
4
  const MODELS = [
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: "Qwen3‑235B‑A22B‑Instruct‑2507", id: "Qwen/Qwen3-235B-A22B-Instruct-2507" },
12
+ { name: "Qwen3‑235B‑A22B‑Thinking", id: "Qwen/Qwen3-235B-A22B-Thinking" },
13
+ { name: "Qwen3‑Coder‑480B‑A35B", id: "Qwen/Qwen3-Coder-480B-A35B-Instruct" },
14
+ { name: "Qwen3‑32B", id: "Qwen/Qwen3-32B" },
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
  ];
21
 
22
+ // ───────────────── 2. LANGUAGES ───────────────────────
23
  const LANGS = [
24
  "python","c","cpp","markdown","latex","json","html","css",
25
  "javascript","jinja2","typescript","yaml","dockerfile","shell",
 
28
  "sql-gpSQL","sql-sparkSQL","sql-esper"
29
  ];
30
 
31
+ // ───────────────── 3. DOM HOOKS ───────────────────────
32
+ const $ = sel => document.querySelector(sel);
33
+ const els = {
34
+ model : $("#model"),
35
+ lang : $("#lang"),
36
+ prompt : $("#prompt"),
37
+ file : $("#file"),
38
+ url : $("#url"),
39
+ search : $("#search"),
40
+ code : $("#code"),
41
+ prev : $("#preview"),
42
+ hist : $("#hist"),
43
+ gen : $("#gen"),
44
+ clear : $("#clear")
45
+ };
46
 
47
+ // ───────────────── 4. INITIAL FILL ────────────────────
48
  MODELS.forEach(m => {
49
+ const opt = new Option(m.name, m.id); opt.dataset.provider = m.provider || "";
50
+ els.model.append(opt);
 
 
 
 
51
  });
52
+ LANGS.forEach(l => els.lang.append(new Option(l.toUpperCase(), l)));
53
 
54
+ // ───────────────── 5. TABS HANDLER ────────────────────
55
  document.querySelectorAll(".tabs[role=tablist]").forEach(tablist => {
56
  tablist.addEventListener("click", e => {
57
+ if (e.target.role !== "tab") return;
58
  const tabs = tablist.querySelectorAll("[role=tab]");
59
  const panels = tablist.parentElement.querySelectorAll("[role=tabpanel]");
60
  tabs.forEach(t => t.setAttribute("aria-selected", t === e.target));
61
+ panels.forEach(p => p.hidden = (p.id !== e.target.getAttribute("aria-controls")));
62
  });
63
  });
64
 
65
+ // ───────────────── 6. HELPERS ─────────────────────────
66
+ const addHist = text => {
67
  const li = document.createElement("li");
68
  li.textContent = `${new Date().toLocaleTimeString()} – ${text.slice(0,40)}…`;
69
+ els.hist.prepend(li);
70
  };
71
 
72
+ // ───────────────── 7. CLEAR BUTTON ───────────────────
73
+ els.clear.addEventListener("click", () => {
74
+ els.prompt.value = ""; els.file.value = ""; els.url.value = "";
75
+ els.code.textContent = ""; els.prev.srcdoc = ""; els.hist.innerHTML = "";
76
  });
77
 
78
+ // ───────────────── 8. GENERATE CODE ──────────────────
79
+ els.gen.addEventListener("click", async () => {
80
+ const prompt = els.prompt.value.trim();
81
+ if (!prompt) { alert("Provide a prompt."); return; }
82
 
83
+ els.gen.disabled = true; els.gen.textContent = "Generating…";
84
 
85
+ const payload = {
86
  prompt,
87
+ model_id : els.model.value,
88
+ language : els.lang.value,
89
+ enable_search : els.search.checked,
90
+ website_url : els.url.value || null,
91
+ file_path : null // will be set after upload
92
  };
93
 
94
+ if (els.file.files.length) {
95
+ const fd = new FormData(); fd.append("file", els.file.files[0]);
 
 
96
  const up = await fetch("/upload", { method:"POST", body: fd });
97
+ payload.file_path = (await up.json()).file;
98
  }
99
 
100
  try {
101
+ const res = await fetch("/run/predict", {
102
+ method : "POST",
103
+ headers : { "Content-Type":"application/json" },
104
+ body : JSON.stringify(payload)
105
  });
106
  if (!res.ok) throw new Error(`HTTPΒ ${res.status}`);
107
+ const [code] = await res.json(); // Gradio returns [code, history]
108
+ els.code.textContent = code;
109
+ els.prev.srcdoc = (els.lang.value === "html")
110
  ? code
111
  : `<pre style="white-space:pre-wrap">${code.replace(/</g,"&lt;")}</pre>`;
112
+ addHist(prompt);
113
+ } catch (e) {
114
+ console.error(e); alert("Generation failed – see console.");
 
115
  } finally {
116
+ els.gen.disabled = false; els.gen.textContent = "GenerateΒ code";
117
  }
118
  });