mgbam commited on
Commit
96267f5
·
verified ·
1 Parent(s): fe0020d

Update static/index.js

Browse files
Files changed (1) hide show
  1. static/index.js +73 -81
static/index.js CHANGED
@@ -1,118 +1,110 @@
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",
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
- // ───────────────── 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
- });
 
1
+ /* static/index.jsAnyCoder front‑end logic */
2
 
3
+ // ────── 1. MODEL LIST ──────
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
+ /* optional premium providers — keep but hide if you don't have keys */
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. LANGUAGE LIST ──────
24
  const LANGS = [
25
+ "python","c","cpp","markdown","latex","json","html","css","javascript",
26
+ "jinja2","typescript","yaml","dockerfile","shell","r","sql","sql-msSQL",
27
+ "sql-mySQL","sql-mariaDB","sql-sqlite","sql-cassandra","sql-plSQL",
28
+ "sql-hive","sql-pgSQL","sql-gql","sql-gpSQL","sql-sparkSQL","sql-esper"
 
29
  ];
30
 
31
+ // ────── 3. DOM SHORTCUTS ──────
32
+ const $ = s => document.querySelector(s);
33
  const els = {
34
+ model: $("#model"), lang : $("#lang"), prompt: $("#prompt"),
35
+ file : $("#file"), url : $("#url"), search: $("#search"),
36
+ code : $("#code"), prev : $("#preview"), hist : $("#hist"),
37
+ gen : $("#gen"), clr : $("#clear")
 
 
 
 
 
 
 
38
  };
39
 
40
+ // ────── 4. POPULATE SELECTS ──────
41
+ MODELS.forEach(m => els.model.append(new Option(m.name, m.id)));
42
+ LANGS .forEach(l => els.lang .append(new Option(l.toUpperCase(), l)));
 
 
 
43
 
44
+ // ────── 5. SIMPLE TAB SWITCHER ──────
45
+ document.querySelectorAll(".tabs[role=tablist]").forEach(tl => {
46
+ tl.addEventListener("click", ev => {
47
+ if (ev.target.role !== "tab") return;
48
+ tl.querySelectorAll("[role=tab]").forEach(t =>
49
+ t.setAttribute("aria-selected", t === ev.target));
50
+ tl.parentElement.querySelectorAll("[role=tabpanel]").forEach(p =>
51
+ p.hidden = (p.id !== ev.target.getAttribute("aria-controls")));
52
  });
53
  });
54
 
55
+ // ────── 6. CLEAR SESSION ──────
56
+ els.clr.onclick = () => {
57
+ els.prompt.value = ""; els.file.value = ""; els.url.value = "";
58
+ els.code.textContent = ""; els.prev.srcdoc = ""; els.hist.innerHTML = "";
59
+ };
60
+
61
+ // ────── 7. ADD HISTORY LINE ──────
62
+ const addHist = txt => {
63
  const li = document.createElement("li");
64
+ li.textContent = `${new Date().toLocaleTimeString()} – ${txt.slice(0,40)}…`;
65
  els.hist.prepend(li);
66
  };
67
 
68
+ // ────── 8. GENERATE (calls /run/predict) ──────
69
+ els.gen.onclick = async () => {
 
 
 
 
 
 
70
  const prompt = els.prompt.value.trim();
71
+ if (!prompt) return alert("Provide a prompt first.");
72
 
73
  els.gen.disabled = true; els.gen.textContent = "Generating…";
74
 
75
+ // optional file upload
76
+ let file_path = null;
 
 
 
 
 
 
 
77
  if (els.file.files.length) {
78
  const fd = new FormData(); fd.append("file", els.file.files[0]);
79
  const up = await fetch("/upload", { method:"POST", body: fd });
80
+ file_path = (await up.json()).file;
81
  }
82
 
83
+ const body = {
84
+ prompt,
85
+ model_id : els.model.value,
86
+ language : els.lang.value,
87
+ enable_search: els.search.checked,
88
+ website_url : els.url.value || null,
89
+ file_path
90
+ };
91
+
92
  try {
93
+ const r = await fetch("/run/predict", {
94
+ method:"POST",
95
+ headers:{ "Content-Type":"application/json" },
96
+ body: JSON.stringify(body)
97
  });
98
+ if (!r.ok) throw new Error(r.statusText);
99
+ const [code] = await r.json(); // returns [code, history]
100
  els.code.textContent = code;
101
  els.prev.srcdoc = (els.lang.value === "html")
102
  ? code
103
  : `<pre style="white-space:pre-wrap">${code.replace(/</g,"&lt;")}</pre>`;
104
  addHist(prompt);
105
+ } catch (err) {
106
+ console.error(err); alert("Generation failed (see console).");
107
  } finally {
108
  els.gen.disabled = false; els.gen.textContent = "Generate code";
109
  }
110
+ };