TwoQuarks commited on
Commit
0413d83
·
verified ·
1 Parent(s): 4f5b0c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -15
app.py CHANGED
@@ -16,6 +16,9 @@ STRANGE_ROOT = ROOT / "Strange"
16
  DOWN_GRAPHICS = DOWN_ROOT / "graphics"
17
  STRANGE_GRAPHICS = STRANGE_ROOT / "graphics"
18
 
 
 
 
19
 
20
  # ============================================================
21
  # Helpers
@@ -23,7 +26,6 @@ STRANGE_GRAPHICS = STRANGE_ROOT / "graphics"
23
 
24
  def _clean_pngs(folder: Path):
25
  folder.mkdir(parents=True, exist_ok=True)
26
- # Clean recursively to handle "graphics/graphics"
27
  for f in folder.rglob("*.png"):
28
  try:
29
  f.unlink()
@@ -34,10 +36,32 @@ def _clean_pngs(folder: Path):
34
  def _collect_pngs(folder: Path):
35
  if not folder.exists():
36
  return []
37
- # Collect recursively to handle nested folders
38
  return sorted(str(p) for p in folder.rglob("*.png"))
39
 
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # ============================================================
42
  # DOWN (Unified: Down + AntiDown)
43
  # ============================================================
@@ -47,24 +71,31 @@ def run_down_all(episodes: int):
47
  if not run_all.exists():
48
  raise RuntimeError(f"{run_all} not found (check folder name: must be 'Down/')")
49
 
 
 
 
50
  _clean_pngs(DOWN_GRAPHICS)
51
 
 
 
 
 
52
  cmd = [sys.executable, str(run_all), "--episodes", str(int(episodes))]
53
 
54
- subprocess.run(
55
- cmd,
56
- cwd=str(DOWN_ROOT),
57
- env=os.environ.copy(),
58
- check=True,
59
- )
60
 
61
  images = _collect_pngs(DOWN_GRAPHICS)
62
  meta = {
63
  "status": "completed",
64
  "pipeline": "DOWN + AntiDown",
65
- "episodes": int(episodes),
66
  "graphics_count": len(images),
67
  "graphics_dir": str(DOWN_GRAPHICS),
 
 
 
 
68
  }
69
  return meta, images
70
 
@@ -78,16 +109,18 @@ def run_strange_all():
78
  if not run_all.exists():
79
  raise RuntimeError(f"{run_all} not found (check folder name: must be 'Strange/')")
80
 
 
 
 
81
  _clean_pngs(STRANGE_GRAPHICS)
82
 
 
 
 
 
83
  cmd = [sys.executable, str(run_all)]
84
 
85
- subprocess.run(
86
- cmd,
87
- cwd=str(STRANGE_ROOT),
88
- env=os.environ.copy(),
89
- check=True,
90
- )
91
 
92
  images = _collect_pngs(STRANGE_GRAPHICS)
93
  meta = {
@@ -95,6 +128,7 @@ def run_strange_all():
95
  "pipeline": "STRANGE + AntiStrange",
96
  "graphics_count": len(images),
97
  "graphics_dir": str(STRANGE_GRAPHICS),
 
98
  }
99
  return meta, images
100
 
 
16
  DOWN_GRAPHICS = DOWN_ROOT / "graphics"
17
  STRANGE_GRAPHICS = STRANGE_ROOT / "graphics"
18
 
19
+ DOWN_RESULTS = DOWN_ROOT / "results"
20
+ STRANGE_RESULTS = STRANGE_ROOT / "results"
21
+
22
 
23
  # ============================================================
24
  # Helpers
 
26
 
27
  def _clean_pngs(folder: Path):
28
  folder.mkdir(parents=True, exist_ok=True)
 
29
  for f in folder.rglob("*.png"):
30
  try:
31
  f.unlink()
 
36
  def _collect_pngs(folder: Path):
37
  if not folder.exists():
38
  return []
 
39
  return sorted(str(p) for p in folder.rglob("*.png"))
40
 
41
 
42
+ def _run_capture(cmd, cwd: Path, env: dict):
43
+ """
44
+ Run a subprocess and capture stdout/stderr.
45
+ If it fails, raise a RuntimeError containing the logs so Gradio shows it.
46
+ """
47
+ result = subprocess.run(
48
+ cmd,
49
+ cwd=str(cwd),
50
+ env=env,
51
+ capture_output=True,
52
+ text=True,
53
+ )
54
+ if result.returncode != 0:
55
+ raise RuntimeError(
56
+ "Subprocess failed\n"
57
+ f"CMD: {' '.join(map(str, cmd))}\n"
58
+ f"CWD: {cwd}\n\n"
59
+ f"STDOUT:\n{result.stdout}\n"
60
+ f"STDERR:\n{result.stderr}"
61
+ )
62
+ return result.stdout, result.stderr
63
+
64
+
65
  # ============================================================
66
  # DOWN (Unified: Down + AntiDown)
67
  # ============================================================
 
71
  if not run_all.exists():
72
  raise RuntimeError(f"{run_all} not found (check folder name: must be 'Down/')")
73
 
74
+ DOWN_GRAPHICS.mkdir(parents=True, exist_ok=True)
75
+ DOWN_RESULTS.mkdir(parents=True, exist_ok=True)
76
+
77
  _clean_pngs(DOWN_GRAPHICS)
78
 
79
+ env = os.environ.copy()
80
+ env["TWOQUARKS_RESULTS_DIR"] = str(DOWN_RESULTS)
81
+ env["TWOQUARKS_GRAPHICS_DIR"] = str(DOWN_GRAPHICS)
82
+
83
  cmd = [sys.executable, str(run_all), "--episodes", str(int(episodes))]
84
 
85
+ # capture logs so the UI shows the real failure reason if any
86
+ _run_capture(cmd, cwd=DOWN_ROOT, env=env)
 
 
 
 
87
 
88
  images = _collect_pngs(DOWN_GRAPHICS)
89
  meta = {
90
  "status": "completed",
91
  "pipeline": "DOWN + AntiDown",
92
+ "episodes_per_phase": int(episodes),
93
  "graphics_count": len(images),
94
  "graphics_dir": str(DOWN_GRAPHICS),
95
+ "results_dir": str(DOWN_RESULTS),
96
+ # canonical CSV names the dual plot expects
97
+ "csv_down": str(DOWN_RESULTS / "down_paradox_tabular_results.csv"),
98
+ "csv_antidown": str(DOWN_RESULTS / "antidown_corrupted_valley_tabular.csv"),
99
  }
100
  return meta, images
101
 
 
109
  if not run_all.exists():
110
  raise RuntimeError(f"{run_all} not found (check folder name: must be 'Strange/')")
111
 
112
+ STRANGE_GRAPHICS.mkdir(parents=True, exist_ok=True)
113
+ STRANGE_RESULTS.mkdir(parents=True, exist_ok=True)
114
+
115
  _clean_pngs(STRANGE_GRAPHICS)
116
 
117
+ env = os.environ.copy()
118
+ env["TWOQUARKS_RESULTS_DIR"] = str(STRANGE_RESULTS)
119
+ env["TWOQUARKS_GRAPHICS_DIR"] = str(STRANGE_GRAPHICS)
120
+
121
  cmd = [sys.executable, str(run_all)]
122
 
123
+ _run_capture(cmd, cwd=STRANGE_ROOT, env=env)
 
 
 
 
 
124
 
125
  images = _collect_pngs(STRANGE_GRAPHICS)
126
  meta = {
 
128
  "pipeline": "STRANGE + AntiStrange",
129
  "graphics_count": len(images),
130
  "graphics_dir": str(STRANGE_GRAPHICS),
131
+ "results_dir": str(STRANGE_RESULTS),
132
  }
133
  return meta, images
134