Pepguy commited on
Commit
bf8b129
·
verified ·
1 Parent(s): 304ff12

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +18 -11
app.js CHANGED
@@ -1882,12 +1882,13 @@ app.get("/status", (_, res) => res.json({
1882
  app.get("/domains", (_, res) => res.json(tracker.stats()));
1883
 
1884
  app.post("/generate", async (req, res) => {
 
1885
  const n = parseInt(req.query.n ?? req.body.n ?? "50");
1886
  const rps = Math.min(parseFloat(req.query.rps ?? req.body.rps ?? DEFAULT_RPS.toString()), MAX_RPS);
1887
- const synthFraction = parseFloat(req.query.synthetic_fraction ?? req.body.synthetic_fraction ?? "0.4");
1888
  const maxVars = parseInt(req.query.max_vars ?? req.body.max_vars ?? "20");
1889
 
1890
- log(`Generation request: n=${n} rps=${rps} synthetic=${(synthFraction * 100).toFixed(0)}%`, "HEAD");
1891
 
1892
  let batchErrors = 0;
1893
  let newSamplesCount = 0;
@@ -1898,7 +1899,9 @@ app.post("/generate", async (req, res) => {
1898
  const gens = Object.values(SYNTHETIC_GENERATORS);
1899
  const perGen = Math.max(1, Math.ceil(nSynthetic / gens.length));
1900
  let tempSynth = [];
1901
- for (const genFn of gens) { tempSynth.push(...genFn(perGen)); }
 
 
1902
  tempSynth = tempSynth.slice(0, nSynthetic);
1903
 
1904
  for (const raw of tempSynth) {
@@ -1909,19 +1912,24 @@ app.post("/generate", async (req, res) => {
1909
  newSamplesCount++;
1910
  } else { batchErrors++; }
1911
  }
1912
- saveDataset(); // Commit synthetics to disk immediately
1913
  log(`Synthetic batch committed to disk: ${newSamplesCount} samples`, "OK");
1914
  }
1915
 
1916
- // ── 2. AI BATCH (Incremental Save per Call) ──
1917
  const nAI = n - nSynthetic;
1918
  const callsNeeded = Math.ceil(nAI / SAMPLES_PER_CALL);
1919
  const minInterval = 1000 / rps;
1920
 
1921
- // Send initial response so the connection doesn't timeout,
1922
- // but the background loop keeps writing to disk.
1923
- res.json({ message: "Generation started. Use /dataset/download to track progress.", total_requested: n });
 
 
 
 
1924
 
 
1925
  (async () => {
1926
  for (let ci = 0; ci < callsNeeded; ci++) {
1927
  const t0 = Date.now();
@@ -1943,10 +1951,9 @@ app.post("/generate", async (req, res) => {
1943
  } else { batchErrors++; }
1944
  }
1945
 
1946
- // THE FIX: Save to disk after every successful Bedrock call
1947
  if (callValid > 0) {
1948
  saveDataset();
1949
- log(`Incremental Save: Call ${ci + 1}/${callsNeeded} committed (${callValid} samples). Total dataset: ${dataset.length}`, "OK");
1950
  }
1951
  } catch (e) {
1952
  log(`Bedrock call ${ci + 1} failed: ${e.message}`, "ERROR");
@@ -1956,7 +1963,7 @@ app.post("/generate", async (req, res) => {
1956
  const elapsed = Date.now() - t0;
1957
  if (elapsed < minInterval) await new Promise(r => setTimeout(r, minInterval - elapsed));
1958
  }
1959
- log(`Batch Process Finished. New: ${newSamplesCount} | Errors: ${batchErrors}`, "HEAD");
1960
  })();
1961
  });
1962
 
 
1882
  app.get("/domains", (_, res) => res.json(tracker.stats()));
1883
 
1884
  app.post("/generate", async (req, res) => {
1885
+ // Correcting the variable names to match the logic below
1886
  const n = parseInt(req.query.n ?? req.body.n ?? "50");
1887
  const rps = Math.min(parseFloat(req.query.rps ?? req.body.rps ?? DEFAULT_RPS.toString()), MAX_RPS);
1888
+ const syntheticFraction = parseFloat(req.query.synthetic_fraction ?? req.body.synthetic_fraction ?? "0.4");
1889
  const maxVars = parseInt(req.query.max_vars ?? req.body.max_vars ?? "20");
1890
 
1891
+ log(`Generation request: n=${n} rps=${rps} synthetic=${(syntheticFraction * 100).toFixed(0)}%`, "HEAD");
1892
 
1893
  let batchErrors = 0;
1894
  let newSamplesCount = 0;
 
1899
  const gens = Object.values(SYNTHETIC_GENERATORS);
1900
  const perGen = Math.max(1, Math.ceil(nSynthetic / gens.length));
1901
  let tempSynth = [];
1902
+ for (const genFn of gens) {
1903
+ try { tempSynth.push(...genFn(perGen)); } catch(e) { log(`Gen error: ${e.message}`, "ERROR"); }
1904
+ }
1905
  tempSynth = tempSynth.slice(0, nSynthetic);
1906
 
1907
  for (const raw of tempSynth) {
 
1912
  newSamplesCount++;
1913
  } else { batchErrors++; }
1914
  }
1915
+ saveDataset();
1916
  log(`Synthetic batch committed to disk: ${newSamplesCount} samples`, "OK");
1917
  }
1918
 
1919
+ // ── 2. AI BATCH (Incremental Save) ──
1920
  const nAI = n - nSynthetic;
1921
  const callsNeeded = Math.ceil(nAI / SAMPLES_PER_CALL);
1922
  const minInterval = 1000 / rps;
1923
 
1924
+ // Respond immediately to prevent client-side timeouts
1925
+ res.json({
1926
+ message: "Generation started in background.",
1927
+ total_requested: n,
1928
+ synthetic_count: nSynthetic,
1929
+ ai_tasks: callsNeeded
1930
+ });
1931
 
1932
+ // Run AI loop in background
1933
  (async () => {
1934
  for (let ci = 0; ci < callsNeeded; ci++) {
1935
  const t0 = Date.now();
 
1951
  } else { batchErrors++; }
1952
  }
1953
 
 
1954
  if (callValid > 0) {
1955
  saveDataset();
1956
+ log(`Saved Call ${ci + 1}/${callsNeeded} (${callValid} samples). Total: ${dataset.length}`, "OK");
1957
  }
1958
  } catch (e) {
1959
  log(`Bedrock call ${ci + 1} failed: ${e.message}`, "ERROR");
 
1963
  const elapsed = Date.now() - t0;
1964
  if (elapsed < minInterval) await new Promise(r => setTimeout(r, minInterval - elapsed));
1965
  }
1966
+ log(`BACKGROUND PROCESS COMPLETE. New samples: ${newSamplesCount} | Errors: ${batchErrors}`, "HEAD");
1967
  })();
1968
  });
1969