Spaces:
Sleeping
Sleeping
chenyicheng commited on
Commit ·
97c823c
1
Parent(s): b7d4b6a
update app
Browse files
app.py
CHANGED
|
@@ -38,6 +38,7 @@ CODE_MAX_TOKENS = 8192
|
|
| 38 |
DATASET_PREVIEW_TIMEOUT = 60 # seconds
|
| 39 |
RUN_CODE_URL = os.getenv("DATACHEF_RUN_URL", "http://127.0.0.1:7001/run_code")
|
| 40 |
RUN_CODE_TIMEOUT = int(os.getenv("DATACHEF_RUN_TIMEOUT", "300"))
|
|
|
|
| 41 |
RUN_CODE_KEY = os.getenv("DATACHEF_RUN_KEY") # optional shared secret sent as x-forward-key
|
| 42 |
_DATASET_PREVIEW_CACHE: Dict[str, Dict] = {}
|
| 43 |
# Preset library
|
|
@@ -987,6 +988,107 @@ def _get_dataset_examples(dataset_id: str, timeout: Optional[int] = None) -> Dic
|
|
| 987 |
raise RuntimeError("; ".join(errors))
|
| 988 |
|
| 989 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 990 |
def _fetch_json(url: str, timeout: int) -> Dict:
|
| 991 |
try:
|
| 992 |
with urllib.request.urlopen(url, timeout=timeout) as resp:
|
|
@@ -1171,6 +1273,12 @@ def _build_input_sample(
|
|
| 1171 |
dataset_infos.append(_get_dataset_examples(dataset_id, DATASET_PREVIEW_TIMEOUT))
|
| 1172 |
except Exception as e:
|
| 1173 |
errors[dataset_id] = str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1174 |
if errors and not dataset_infos:
|
| 1175 |
raise ValueError(f"Dataset build error: {errors}")
|
| 1176 |
return {
|
|
@@ -1199,6 +1307,11 @@ def _preview_dataset(dataset_ids_table: List[List[str]]) -> str:
|
|
| 1199 |
dataset_infos.append(_get_dataset_examples(dataset_id, DATASET_PREVIEW_TIMEOUT))
|
| 1200 |
except Exception as e:
|
| 1201 |
errors[dataset_id] = str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1202 |
|
| 1203 |
return _render_preview_table_html(dataset_infos, errors if errors else None)
|
| 1204 |
except Exception as e:
|
|
|
|
| 38 |
DATASET_PREVIEW_TIMEOUT = 60 # seconds
|
| 39 |
RUN_CODE_URL = os.getenv("DATACHEF_RUN_URL", "http://127.0.0.1:7001/run_code")
|
| 40 |
RUN_CODE_TIMEOUT = int(os.getenv("DATACHEF_RUN_TIMEOUT", "300"))
|
| 41 |
+
DATASET_LOAD_PROBE_TIMEOUT = int(os.getenv("DATASET_LOAD_PROBE_TIMEOUT", "60"))
|
| 42 |
RUN_CODE_KEY = os.getenv("DATACHEF_RUN_KEY") # optional shared secret sent as x-forward-key
|
| 43 |
_DATASET_PREVIEW_CACHE: Dict[str, Dict] = {}
|
| 44 |
# Preset library
|
|
|
|
| 988 |
raise RuntimeError("; ".join(errors))
|
| 989 |
|
| 990 |
|
| 991 |
+
def _run_code_probe(code_text: str, timeout: int) -> Tuple[bool, str]:
|
| 992 |
+
"""
|
| 993 |
+
Run lightweight code on the proxy runner to test dataset loading.
|
| 994 |
+
Returns (success, error_message).
|
| 995 |
+
"""
|
| 996 |
+
if not RUN_CODE_URL:
|
| 997 |
+
return False, "RUN_CODE_URL not configured"
|
| 998 |
+
|
| 999 |
+
headers = {}
|
| 1000 |
+
if RUN_CODE_KEY:
|
| 1001 |
+
headers["x-forward-key"] = RUN_CODE_KEY
|
| 1002 |
+
|
| 1003 |
+
try:
|
| 1004 |
+
resp = requests.post(
|
| 1005 |
+
RUN_CODE_URL,
|
| 1006 |
+
json={"code": code_text, "timeout": timeout},
|
| 1007 |
+
headers=headers,
|
| 1008 |
+
timeout=min(timeout, 30) + 10,
|
| 1009 |
+
)
|
| 1010 |
+
resp.raise_for_status()
|
| 1011 |
+
data = resp.json() if resp.content else {}
|
| 1012 |
+
except Exception as e: # noqa: BLE001
|
| 1013 |
+
return False, f"probe request failed: {e}"
|
| 1014 |
+
|
| 1015 |
+
job_id = data.get("job_id")
|
| 1016 |
+
if not job_id:
|
| 1017 |
+
return False, "probe runner did not return job_id"
|
| 1018 |
+
|
| 1019 |
+
status_base = RUN_CODE_URL.rsplit("/", 1)[0].rstrip("/")
|
| 1020 |
+
status_url = f"{status_base}/status/{job_id}"
|
| 1021 |
+
deadline = time.time() + timeout
|
| 1022 |
+
poll_interval = 1.5
|
| 1023 |
+
result_data = None
|
| 1024 |
+
while time.time() < deadline:
|
| 1025 |
+
try:
|
| 1026 |
+
r = requests.get(status_url, headers=headers, timeout=min(timeout, 30))
|
| 1027 |
+
r.raise_for_status()
|
| 1028 |
+
job = r.json() if r.content else {}
|
| 1029 |
+
except Exception:
|
| 1030 |
+
time.sleep(poll_interval)
|
| 1031 |
+
continue
|
| 1032 |
+
status = job.get("status")
|
| 1033 |
+
if status in {"success", "failed", "timeout", "error"}:
|
| 1034 |
+
result_data = job
|
| 1035 |
+
break
|
| 1036 |
+
time.sleep(poll_interval)
|
| 1037 |
+
|
| 1038 |
+
if result_data is None:
|
| 1039 |
+
return False, f"probe job {job_id} timed out after {timeout}s"
|
| 1040 |
+
|
| 1041 |
+
status = result_data.get("status")
|
| 1042 |
+
retcode = result_data.get("returncode")
|
| 1043 |
+
stderr = result_data.get("stderr") or ""
|
| 1044 |
+
summary = result_data.get("summary") or ""
|
| 1045 |
+
if status == "success" and retcode == 0:
|
| 1046 |
+
return True, ""
|
| 1047 |
+
return False, summary or stderr or f"probe failed (status={status}, returncode={retcode})"
|
| 1048 |
+
|
| 1049 |
+
|
| 1050 |
+
def _probe_dataset_load(dataset_id: str, name: Optional[str], split: str) -> Tuple[bool, str]:
|
| 1051 |
+
"""
|
| 1052 |
+
Trigger a small load via proxy runner to weed out slow/invalid datasets.
|
| 1053 |
+
Loads up to 20 samples; returns (success, error_message).
|
| 1054 |
+
"""
|
| 1055 |
+
name_literal = "None" if not name or name == "default" else repr(name)
|
| 1056 |
+
split_literal = repr(split or "train")
|
| 1057 |
+
code = (
|
| 1058 |
+
"from aidp import load_remote_dataset\n"
|
| 1059 |
+
"from datasets import disable_caching\n"
|
| 1060 |
+
"disable_caching()\n"
|
| 1061 |
+
f"ds = load_remote_dataset({repr(dataset_id)}, name={name_literal}, split={split_literal}, num_samples=20, shuffle=False)\n"
|
| 1062 |
+
"print('rows', len(ds))\n"
|
| 1063 |
+
"print('columns', list(ds.features.keys()))\n"
|
| 1064 |
+
)
|
| 1065 |
+
return _run_code_probe(code, timeout=DATASET_LOAD_PROBE_TIMEOUT)
|
| 1066 |
+
|
| 1067 |
+
|
| 1068 |
+
def _filter_dataset_infos_by_load(dataset_infos: List[Dict]) -> Tuple[List[Dict], Dict[str, str]]:
|
| 1069 |
+
"""
|
| 1070 |
+
Run load probes for each dataset. Datasets that fail or timeout are dropped.
|
| 1071 |
+
Returns (kept_dataset_infos, load_errors).
|
| 1072 |
+
"""
|
| 1073 |
+
kept: List[Dict] = []
|
| 1074 |
+
errors: Dict[str, str] = {}
|
| 1075 |
+
for info in dataset_infos:
|
| 1076 |
+
dataset_id = info.get("dataset_id", "")
|
| 1077 |
+
examples = info.get("examples") or []
|
| 1078 |
+
if not examples:
|
| 1079 |
+
errors[dataset_id] = "no examples to probe"
|
| 1080 |
+
continue
|
| 1081 |
+
first = examples[0]
|
| 1082 |
+
name = first.get("name") or None
|
| 1083 |
+
split = first.get("split") or "train"
|
| 1084 |
+
ok, err = _probe_dataset_load(dataset_id, name, split)
|
| 1085 |
+
if ok:
|
| 1086 |
+
kept.append(info)
|
| 1087 |
+
else:
|
| 1088 |
+
errors[dataset_id] = err
|
| 1089 |
+
return kept, errors
|
| 1090 |
+
|
| 1091 |
+
|
| 1092 |
def _fetch_json(url: str, timeout: int) -> Dict:
|
| 1093 |
try:
|
| 1094 |
with urllib.request.urlopen(url, timeout=timeout) as resp:
|
|
|
|
| 1273 |
dataset_infos.append(_get_dataset_examples(dataset_id, DATASET_PREVIEW_TIMEOUT))
|
| 1274 |
except Exception as e:
|
| 1275 |
errors[dataset_id] = str(e)
|
| 1276 |
+
|
| 1277 |
+
# Probe actual dataset loading via proxy runner to drop slow/invalid ones
|
| 1278 |
+
if dataset_infos:
|
| 1279 |
+
dataset_infos, load_errors = _filter_dataset_infos_by_load(dataset_infos)
|
| 1280 |
+
errors.update(load_errors)
|
| 1281 |
+
|
| 1282 |
if errors and not dataset_infos:
|
| 1283 |
raise ValueError(f"Dataset build error: {errors}")
|
| 1284 |
return {
|
|
|
|
| 1307 |
dataset_infos.append(_get_dataset_examples(dataset_id, DATASET_PREVIEW_TIMEOUT))
|
| 1308 |
except Exception as e:
|
| 1309 |
errors[dataset_id] = str(e)
|
| 1310 |
+
|
| 1311 |
+
# Probe loadability and drop slow/failed datasets before rendering preview
|
| 1312 |
+
if dataset_infos:
|
| 1313 |
+
dataset_infos, load_errors = _filter_dataset_infos_by_load(dataset_infos)
|
| 1314 |
+
errors.update(load_errors)
|
| 1315 |
|
| 1316 |
return _render_preview_table_html(dataset_infos, errors if errors else None)
|
| 1317 |
except Exception as e:
|