Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ from model.tokenizer import SanskritSourceTokenizer, SanskritTargetTokenizer
|
|
| 17 |
|
| 18 |
|
| 19 |
RESULTS_DIR = "generated_results"
|
| 20 |
-
DEFAULT_ANALYSIS_OUT = "
|
| 21 |
os.makedirs(RESULTS_DIR, exist_ok=True)
|
| 22 |
|
| 23 |
HF_DEFAULT_MODEL_REPO = os.environ.get("HF_DEFAULT_MODEL_REPO", "bhsinghgrid/DevaFlow")
|
|
@@ -296,11 +296,26 @@ def generate_from_ui(
|
|
| 296 |
return output_text, status, record
|
| 297 |
|
| 298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
def _run_analysis_cmd(task, ckpt_path, output_dir, input_text="dharmo rakṣati rakṣitaḥ", phase="analyze"):
|
| 300 |
os.makedirs(output_dir, exist_ok=True)
|
| 301 |
-
script =
|
| 302 |
-
if
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
| 304 |
# Space-safe Task4 fallback: if ablation models don't exist, bootstrap them
|
| 305 |
# from currently selected checkpoint so Task4 can still execute end-to-end.
|
| 306 |
if str(task) == "4" and phase == "analyze":
|
|
@@ -337,7 +352,7 @@ def _run_analysis_cmd(task, ckpt_path, output_dir, input_text="dharmo rakṣati
|
|
| 337 |
|
| 338 |
proc = subprocess.run(cmd, capture_output=True, text=True, env=env)
|
| 339 |
log = f"$ {' '.join(cmd)}\n\n{proc.stdout}\n{proc.stderr}"
|
| 340 |
-
return proc.returncode, log
|
| 341 |
|
| 342 |
|
| 343 |
def _bundle_task_outputs(model_bundle, output_dir):
|
|
@@ -375,13 +390,17 @@ def _live_input_summary(model_bundle, input_text: str) -> str:
|
|
| 375 |
def run_single_task(model_bundle, task, output_dir, input_text, task4_phase):
|
| 376 |
if not model_bundle:
|
| 377 |
raise gr.Error("Load a model first.")
|
| 378 |
-
code, log = _run_analysis_cmd(task, model_bundle["ckpt_path"], output_dir, input_text, task4_phase)
|
| 379 |
if code != 0:
|
| 380 |
_bundle_task_outputs(model_bundle, output_dir)
|
| 381 |
log = f"{log}\n\n--- Live input summary ---\n{_live_input_summary(model_bundle, input_text)}"
|
| 382 |
status = f"Task {task} fallback mode: bundled reports + live input analysis."
|
| 383 |
else:
|
| 384 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
return status, log
|
| 386 |
|
| 387 |
|
|
@@ -390,15 +409,25 @@ def run_all_tasks(model_bundle, output_dir, input_text, task4_phase):
|
|
| 390 |
raise gr.Error("Load a model first.")
|
| 391 |
logs = []
|
| 392 |
failures = 0
|
|
|
|
| 393 |
for task in ["1", "2", "3", "4", "5"]:
|
| 394 |
-
code, log = _run_analysis_cmd(
|
|
|
|
|
|
|
| 395 |
logs.append(f"\n\n{'='*22} TASK {task} {'='*22}\n{log}")
|
|
|
|
| 396 |
if code != 0:
|
| 397 |
failures += 1
|
| 398 |
-
if failures:
|
| 399 |
_bundle_task_outputs(model_bundle, output_dir)
|
|
|
|
| 400 |
logs.append(f"\n\n--- Live input summary ---\n{_live_input_summary(model_bundle, input_text)}")
|
| 401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
return status, "".join(logs)
|
| 403 |
|
| 404 |
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
RESULTS_DIR = "generated_results"
|
| 20 |
+
DEFAULT_ANALYSIS_OUT = "analysis_outputs/T4"
|
| 21 |
os.makedirs(RESULTS_DIR, exist_ok=True)
|
| 22 |
|
| 23 |
HF_DEFAULT_MODEL_REPO = os.environ.get("HF_DEFAULT_MODEL_REPO", "bhsinghgrid/DevaFlow")
|
|
|
|
| 296 |
return output_text, status, record
|
| 297 |
|
| 298 |
|
| 299 |
+
def _resolve_analysis_script() -> Path | None:
|
| 300 |
+
candidates = [
|
| 301 |
+
Path("analysis") / "run_analysis.py",
|
| 302 |
+
Path("final_folder") / "analysis" / "run_analysis.py",
|
| 303 |
+
Path("deploy_ready") / "space_repo" / "analysis" / "run_analysis.py",
|
| 304 |
+
]
|
| 305 |
+
for p in candidates:
|
| 306 |
+
if p.exists():
|
| 307 |
+
return p
|
| 308 |
+
return None
|
| 309 |
+
|
| 310 |
+
|
| 311 |
def _run_analysis_cmd(task, ckpt_path, output_dir, input_text="dharmo rakṣati rakṣitaḥ", phase="analyze"):
|
| 312 |
os.makedirs(output_dir, exist_ok=True)
|
| 313 |
+
script = _resolve_analysis_script()
|
| 314 |
+
if script is None:
|
| 315 |
+
bundled = Path("analysis_outputs")
|
| 316 |
+
if bundled.exists():
|
| 317 |
+
return 0, "Analysis runner not bundled; using packaged analysis_outputs.", True
|
| 318 |
+
return 2, "Analysis runner missing and no bundled analysis_outputs found.", False
|
| 319 |
# Space-safe Task4 fallback: if ablation models don't exist, bootstrap them
|
| 320 |
# from currently selected checkpoint so Task4 can still execute end-to-end.
|
| 321 |
if str(task) == "4" and phase == "analyze":
|
|
|
|
| 352 |
|
| 353 |
proc = subprocess.run(cmd, capture_output=True, text=True, env=env)
|
| 354 |
log = f"$ {' '.join(cmd)}\n\n{proc.stdout}\n{proc.stderr}"
|
| 355 |
+
return proc.returncode, log, False
|
| 356 |
|
| 357 |
|
| 358 |
def _bundle_task_outputs(model_bundle, output_dir):
|
|
|
|
| 390 |
def run_single_task(model_bundle, task, output_dir, input_text, task4_phase):
|
| 391 |
if not model_bundle:
|
| 392 |
raise gr.Error("Load a model first.")
|
| 393 |
+
code, log, used_bundled = _run_analysis_cmd(task, model_bundle["ckpt_path"], output_dir, input_text, task4_phase)
|
| 394 |
if code != 0:
|
| 395 |
_bundle_task_outputs(model_bundle, output_dir)
|
| 396 |
log = f"{log}\n\n--- Live input summary ---\n{_live_input_summary(model_bundle, input_text)}"
|
| 397 |
status = f"Task {task} fallback mode: bundled reports + live input analysis."
|
| 398 |
else:
|
| 399 |
+
if used_bundled:
|
| 400 |
+
_bundle_task_outputs(model_bundle, output_dir)
|
| 401 |
+
status = f"Task {task} loaded from bundled analysis outputs."
|
| 402 |
+
else:
|
| 403 |
+
status = f"Task {task} completed (exit={code})."
|
| 404 |
return status, log
|
| 405 |
|
| 406 |
|
|
|
|
| 409 |
raise gr.Error("Load a model first.")
|
| 410 |
logs = []
|
| 411 |
failures = 0
|
| 412 |
+
used_bundled_any = False
|
| 413 |
for task in ["1", "2", "3", "4", "5"]:
|
| 414 |
+
code, log, used_bundled = _run_analysis_cmd(
|
| 415 |
+
task, model_bundle["ckpt_path"], output_dir, input_text, task4_phase
|
| 416 |
+
)
|
| 417 |
logs.append(f"\n\n{'='*22} TASK {task} {'='*22}\n{log}")
|
| 418 |
+
used_bundled_any = used_bundled_any or used_bundled
|
| 419 |
if code != 0:
|
| 420 |
failures += 1
|
| 421 |
+
if failures or used_bundled_any:
|
| 422 |
_bundle_task_outputs(model_bundle, output_dir)
|
| 423 |
+
if failures:
|
| 424 |
logs.append(f"\n\n--- Live input summary ---\n{_live_input_summary(model_bundle, input_text)}")
|
| 425 |
+
if failures:
|
| 426 |
+
status = f"Run-all finished with {failures} fallback task(s)."
|
| 427 |
+
elif used_bundled_any:
|
| 428 |
+
status = "Run-all loaded from bundled analysis outputs."
|
| 429 |
+
else:
|
| 430 |
+
status = "All 5 tasks completed."
|
| 431 |
return status, "".join(logs)
|
| 432 |
|
| 433 |
|