Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -645,7 +645,24 @@ def _live_task_analysis(model_bundle, task: str, input_text: str, task5_cfg: dic
|
|
| 645 |
return _live_input_summary(model_bundle, text)
|
| 646 |
|
| 647 |
|
| 648 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
tasks = ["1", "2", "3", "4", "5"]
|
| 650 |
failures = 0
|
| 651 |
logs = []
|
|
@@ -661,14 +678,17 @@ def _bg_worker(job_id: str, model_bundle, output_dir: str, input_text: str, task
|
|
| 661 |
}
|
| 662 |
)
|
| 663 |
try:
|
| 664 |
-
|
| 665 |
-
task,
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
|
|
|
|
|
|
|
|
|
| 672 |
logs.append(f"\n\n{'='*22} TASK {task} {'='*22}\n{log}")
|
| 673 |
if code != 0:
|
| 674 |
failures += 1
|
|
@@ -740,7 +760,7 @@ def _bg_worker(job_id: str, model_bundle, output_dir: str, input_text: str, task
|
|
| 740 |
)
|
| 741 |
|
| 742 |
|
| 743 |
-
def start_run_all_background(model_bundle, output_dir, input_text, task4_phase, task5_cfg):
|
| 744 |
if not model_bundle:
|
| 745 |
raise gr.Error("Load a model first.")
|
| 746 |
os.makedirs(output_dir, exist_ok=True)
|
|
@@ -758,12 +778,19 @@ def start_run_all_background(model_bundle, output_dir, input_text, task4_phase,
|
|
| 758 |
}
|
| 759 |
th = threading.Thread(
|
| 760 |
target=_bg_worker,
|
| 761 |
-
args=(job_id, model_bundle, output_dir, input_text, task4_phase, task5_cfg),
|
| 762 |
daemon=True,
|
| 763 |
)
|
| 764 |
th.start()
|
| 765 |
flow = _build_flow_markdown(model_loaded=True, inference_ready=True, task_states=_BG_JOBS[job_id]["task_states"])
|
| 766 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 767 |
|
| 768 |
|
| 769 |
def poll_run_all_background(job_id, output_dir):
|
|
@@ -782,19 +809,24 @@ def poll_run_all_background(job_id, output_dir):
|
|
| 782 |
return status, j.get("log", ""), j.get("task_states", {}), flow, *outputs
|
| 783 |
|
| 784 |
|
| 785 |
-
def run_single_task_and_refresh(model_bundle, task, output_dir, input_text, task4_phase, task5_cfg):
|
| 786 |
-
status, log, task_states, flow = run_single_task(
|
|
|
|
|
|
|
| 787 |
out = refresh_task_outputs(output_dir)
|
| 788 |
return status, log, task_states, flow, *out
|
| 789 |
|
| 790 |
|
| 791 |
-
def run_single_task(model_bundle, task, output_dir, input_text, task4_phase, task5_cfg):
|
| 792 |
if not model_bundle:
|
| 793 |
raise gr.Error("Load a model first.")
|
| 794 |
t0 = time.perf_counter()
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
|
|
|
|
|
|
|
|
|
| 798 |
task_states = {k: "pending" for k in ["1", "2", "3", "4", "5"]}
|
| 799 |
task_states[str(task)] = "running"
|
| 800 |
elapsed = (time.perf_counter() - t0) * 1000.0
|
|
@@ -836,16 +868,19 @@ def run_single_task(model_bundle, task, output_dir, input_text, task4_phase, tas
|
|
| 836 |
return status, log, task_states, flow
|
| 837 |
|
| 838 |
|
| 839 |
-
def run_all_tasks(model_bundle, output_dir, input_text, task4_phase, task5_cfg):
|
| 840 |
if not model_bundle:
|
| 841 |
raise gr.Error("Load a model first.")
|
| 842 |
logs = []
|
| 843 |
failures = 0
|
| 844 |
used_bundled_any = False
|
| 845 |
for task in ["1", "2", "3", "4", "5"]:
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
|
|
|
|
|
|
|
|
|
| 849 |
logs.append(f"\n\n{'='*22} TASK {task} {'='*22}\n{log}")
|
| 850 |
used_bundled_any = used_bundled_any or used_bundled
|
| 851 |
if code != 0:
|
|
@@ -916,11 +951,14 @@ def _safe_refresh_task_outputs(output_dir):
|
|
| 916 |
|
| 917 |
|
| 918 |
def _safe_start_run_all_background(
|
| 919 |
-
model_bundle, output_dir, input_text, task4_phase, current_job_id,
|
|
|
|
| 920 |
):
|
| 921 |
try:
|
| 922 |
cfg = _task5_cfg(lambda_min, lambda_max, lambda_step, task5_samples)
|
| 923 |
-
status, log, job_id, task_states, flow = start_run_all_background(
|
|
|
|
|
|
|
| 924 |
return status, log, job_id, task_states, flow
|
| 925 |
except Exception as e:
|
| 926 |
err = f"Background start failed: {e}"
|
|
@@ -937,11 +975,12 @@ def _safe_poll_run_all_background(job_id, output_dir):
|
|
| 937 |
|
| 938 |
|
| 939 |
def _safe_run_single_task_and_refresh(
|
| 940 |
-
model_bundle, task, output_dir, input_text, task4_phase,
|
|
|
|
| 941 |
):
|
| 942 |
try:
|
| 943 |
cfg = _task5_cfg(lambda_min, lambda_max, lambda_step, task5_samples)
|
| 944 |
-
return run_single_task_and_refresh(model_bundle, task, output_dir, input_text, task4_phase, cfg)
|
| 945 |
except Exception as e:
|
| 946 |
err = f"Task {task} failed: {e}"
|
| 947 |
out = _safe_refresh_task_outputs(output_dir)
|
|
@@ -1050,6 +1089,11 @@ with gr.Blocks(title="Sanskrit Diffusion Client Demo", css=CUSTOM_CSS) as demo:
|
|
| 1050 |
value="analyze",
|
| 1051 |
label="Task 4 Phase",
|
| 1052 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1053 |
gr.Markdown("**Task 5 Controls**")
|
| 1054 |
task5_lambda_min = gr.Slider(0.0, 3.0, value=0.0, step=0.1, label="Task5 λ min")
|
| 1055 |
task5_lambda_max = gr.Slider(0.0, 3.0, value=3.0, step=0.1, label="Task5 λ max")
|
|
@@ -1176,7 +1220,7 @@ with gr.Blocks(title="Sanskrit Diffusion Client Demo", css=CUSTOM_CSS) as demo:
|
|
| 1176 |
fn=_safe_run_single_task_and_refresh,
|
| 1177 |
inputs=[
|
| 1178 |
model_state, task_choice, analysis_output_dir, analysis_input, task4_phase,
|
| 1179 |
-
task5_lambda_min, task5_lambda_max, task5_lambda_step, task5_samples
|
| 1180 |
],
|
| 1181 |
outputs=[
|
| 1182 |
task_run_status,
|
|
@@ -1199,7 +1243,7 @@ with gr.Blocks(title="Sanskrit Diffusion Client Demo", css=CUSTOM_CSS) as demo:
|
|
| 1199 |
fn=_safe_start_run_all_background,
|
| 1200 |
inputs=[
|
| 1201 |
model_state, analysis_output_dir, analysis_input, task4_phase, bg_job_state,
|
| 1202 |
-
task5_lambda_min, task5_lambda_max, task5_lambda_step, task5_samples
|
| 1203 |
],
|
| 1204 |
outputs=[task_run_status, task_run_log, bg_job_state, task_states_view, flow_box],
|
| 1205 |
)
|
|
|
|
| 645 |
return _live_input_summary(model_bundle, text)
|
| 646 |
|
| 647 |
|
| 648 |
+
def _run_quick_task(task, model_bundle, input_text, task5_cfg):
|
| 649 |
+
log = (
|
| 650 |
+
f"[Quick Mode] Task {task}\n"
|
| 651 |
+
f"Heavy analysis runner skipped for speed.\n\n"
|
| 652 |
+
f"{_live_task_analysis(model_bundle, task, input_text, task5_cfg)}"
|
| 653 |
+
)
|
| 654 |
+
return 0, log, False
|
| 655 |
+
|
| 656 |
+
|
| 657 |
+
def _bg_worker(
|
| 658 |
+
job_id: str,
|
| 659 |
+
model_bundle,
|
| 660 |
+
output_dir: str,
|
| 661 |
+
input_text: str,
|
| 662 |
+
task4_phase: str,
|
| 663 |
+
task5_cfg: dict,
|
| 664 |
+
quick_mode: bool,
|
| 665 |
+
):
|
| 666 |
tasks = ["1", "2", "3", "4", "5"]
|
| 667 |
failures = 0
|
| 668 |
logs = []
|
|
|
|
| 678 |
}
|
| 679 |
)
|
| 680 |
try:
|
| 681 |
+
if quick_mode:
|
| 682 |
+
code, log, used_bundled = _run_quick_task(task, model_bundle, input_text, task5_cfg)
|
| 683 |
+
else:
|
| 684 |
+
code, log, used_bundled = _run_analysis_cmd(
|
| 685 |
+
task,
|
| 686 |
+
model_bundle["ckpt_path"],
|
| 687 |
+
output_dir,
|
| 688 |
+
input_text,
|
| 689 |
+
task4_phase,
|
| 690 |
+
task5_cfg.get("samples", 50),
|
| 691 |
+
)
|
| 692 |
logs.append(f"\n\n{'='*22} TASK {task} {'='*22}\n{log}")
|
| 693 |
if code != 0:
|
| 694 |
failures += 1
|
|
|
|
| 760 |
)
|
| 761 |
|
| 762 |
|
| 763 |
+
def start_run_all_background(model_bundle, output_dir, input_text, task4_phase, task5_cfg, quick_mode):
|
| 764 |
if not model_bundle:
|
| 765 |
raise gr.Error("Load a model first.")
|
| 766 |
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
| 778 |
}
|
| 779 |
th = threading.Thread(
|
| 780 |
target=_bg_worker,
|
| 781 |
+
args=(job_id, model_bundle, output_dir, input_text, task4_phase, task5_cfg, bool(quick_mode)),
|
| 782 |
daemon=True,
|
| 783 |
)
|
| 784 |
th.start()
|
| 785 |
flow = _build_flow_markdown(model_loaded=True, inference_ready=True, task_states=_BG_JOBS[job_id]["task_states"])
|
| 786 |
+
mode = "Quick" if quick_mode else "Full"
|
| 787 |
+
return (
|
| 788 |
+
f"Background run started ({mode} Mode). Job ID: {job_id}",
|
| 789 |
+
f"Job {job_id} queued...",
|
| 790 |
+
job_id,
|
| 791 |
+
_BG_JOBS[job_id]["task_states"],
|
| 792 |
+
flow,
|
| 793 |
+
)
|
| 794 |
|
| 795 |
|
| 796 |
def poll_run_all_background(job_id, output_dir):
|
|
|
|
| 809 |
return status, j.get("log", ""), j.get("task_states", {}), flow, *outputs
|
| 810 |
|
| 811 |
|
| 812 |
+
def run_single_task_and_refresh(model_bundle, task, output_dir, input_text, task4_phase, task5_cfg, quick_mode):
|
| 813 |
+
status, log, task_states, flow = run_single_task(
|
| 814 |
+
model_bundle, task, output_dir, input_text, task4_phase, task5_cfg, quick_mode
|
| 815 |
+
)
|
| 816 |
out = refresh_task_outputs(output_dir)
|
| 817 |
return status, log, task_states, flow, *out
|
| 818 |
|
| 819 |
|
| 820 |
+
def run_single_task(model_bundle, task, output_dir, input_text, task4_phase, task5_cfg, quick_mode):
|
| 821 |
if not model_bundle:
|
| 822 |
raise gr.Error("Load a model first.")
|
| 823 |
t0 = time.perf_counter()
|
| 824 |
+
if quick_mode:
|
| 825 |
+
code, log, used_bundled = _run_quick_task(task, model_bundle, input_text, task5_cfg)
|
| 826 |
+
else:
|
| 827 |
+
code, log, used_bundled = _run_analysis_cmd(
|
| 828 |
+
task, model_bundle["ckpt_path"], output_dir, input_text, task4_phase, task5_cfg.get("samples", 50)
|
| 829 |
+
)
|
| 830 |
task_states = {k: "pending" for k in ["1", "2", "3", "4", "5"]}
|
| 831 |
task_states[str(task)] = "running"
|
| 832 |
elapsed = (time.perf_counter() - t0) * 1000.0
|
|
|
|
| 868 |
return status, log, task_states, flow
|
| 869 |
|
| 870 |
|
| 871 |
+
def run_all_tasks(model_bundle, output_dir, input_text, task4_phase, task5_cfg, quick_mode):
|
| 872 |
if not model_bundle:
|
| 873 |
raise gr.Error("Load a model first.")
|
| 874 |
logs = []
|
| 875 |
failures = 0
|
| 876 |
used_bundled_any = False
|
| 877 |
for task in ["1", "2", "3", "4", "5"]:
|
| 878 |
+
if quick_mode:
|
| 879 |
+
code, log, used_bundled = _run_quick_task(task, model_bundle, input_text, task5_cfg)
|
| 880 |
+
else:
|
| 881 |
+
code, log, used_bundled = _run_analysis_cmd(
|
| 882 |
+
task, model_bundle["ckpt_path"], output_dir, input_text, task4_phase, task5_cfg.get("samples", 50)
|
| 883 |
+
)
|
| 884 |
logs.append(f"\n\n{'='*22} TASK {task} {'='*22}\n{log}")
|
| 885 |
used_bundled_any = used_bundled_any or used_bundled
|
| 886 |
if code != 0:
|
|
|
|
| 951 |
|
| 952 |
|
| 953 |
def _safe_start_run_all_background(
|
| 954 |
+
model_bundle, output_dir, input_text, task4_phase, current_job_id,
|
| 955 |
+
lambda_min, lambda_max, lambda_step, task5_samples, quick_mode
|
| 956 |
):
|
| 957 |
try:
|
| 958 |
cfg = _task5_cfg(lambda_min, lambda_max, lambda_step, task5_samples)
|
| 959 |
+
status, log, job_id, task_states, flow = start_run_all_background(
|
| 960 |
+
model_bundle, output_dir, input_text, task4_phase, cfg, quick_mode
|
| 961 |
+
)
|
| 962 |
return status, log, job_id, task_states, flow
|
| 963 |
except Exception as e:
|
| 964 |
err = f"Background start failed: {e}"
|
|
|
|
| 975 |
|
| 976 |
|
| 977 |
def _safe_run_single_task_and_refresh(
|
| 978 |
+
model_bundle, task, output_dir, input_text, task4_phase,
|
| 979 |
+
lambda_min, lambda_max, lambda_step, task5_samples, quick_mode
|
| 980 |
):
|
| 981 |
try:
|
| 982 |
cfg = _task5_cfg(lambda_min, lambda_max, lambda_step, task5_samples)
|
| 983 |
+
return run_single_task_and_refresh(model_bundle, task, output_dir, input_text, task4_phase, cfg, quick_mode)
|
| 984 |
except Exception as e:
|
| 985 |
err = f"Task {task} failed: {e}"
|
| 986 |
out = _safe_refresh_task_outputs(output_dir)
|
|
|
|
| 1089 |
value="analyze",
|
| 1090 |
label="Task 4 Phase",
|
| 1091 |
)
|
| 1092 |
+
quick_mode = gr.Checkbox(
|
| 1093 |
+
value=True,
|
| 1094 |
+
label="Quick Mode (fast live analysis)",
|
| 1095 |
+
info="Runs lightweight live task analysis instead of heavy runner.",
|
| 1096 |
+
)
|
| 1097 |
gr.Markdown("**Task 5 Controls**")
|
| 1098 |
task5_lambda_min = gr.Slider(0.0, 3.0, value=0.0, step=0.1, label="Task5 λ min")
|
| 1099 |
task5_lambda_max = gr.Slider(0.0, 3.0, value=3.0, step=0.1, label="Task5 λ max")
|
|
|
|
| 1220 |
fn=_safe_run_single_task_and_refresh,
|
| 1221 |
inputs=[
|
| 1222 |
model_state, task_choice, analysis_output_dir, analysis_input, task4_phase,
|
| 1223 |
+
task5_lambda_min, task5_lambda_max, task5_lambda_step, task5_samples, quick_mode
|
| 1224 |
],
|
| 1225 |
outputs=[
|
| 1226 |
task_run_status,
|
|
|
|
| 1243 |
fn=_safe_start_run_all_background,
|
| 1244 |
inputs=[
|
| 1245 |
model_state, analysis_output_dir, analysis_input, task4_phase, bg_job_state,
|
| 1246 |
+
task5_lambda_min, task5_lambda_max, task5_lambda_step, task5_samples, quick_mode
|
| 1247 |
],
|
| 1248 |
outputs=[task_run_status, task_run_log, bg_job_state, task_states_view, flow_box],
|
| 1249 |
)
|