wuhp commited on
Commit
db5e783
·
verified ·
1 Parent(s): c1aab1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -12
app.py CHANGED
@@ -144,7 +144,7 @@ def parse_roboflow_url(s: str):
144
  version = None
145
  if len(p) >= 3:
146
  v = p[2]
147
- if v.lower().startswith('v') and v[1:].isdigit():
148
  version = int(v[1:])
149
  elif v.isdigit():
150
  version = int(v)
@@ -459,14 +459,16 @@ def _install_supervisely_logger_shim():
459
  return str(root)
460
 
461
  # ---- NEW: kwargs-aware sitecustomize shim (safe, non-invasive) ---------------
462
- def _install_workspace_shim_v3(cwd_for_train: str, module_default: str = "rtdetrv2_pytorch.src"):
463
  """
464
  Writes a sitecustomize.py that monkeypatches
465
  rtdetrv2_pytorch.src.core.workspace.create so it works with the real signature:
466
  def create(name, **kwargs):
467
  The shim ensures kwargs['cfg'] is a dict, then guarantees cfg['_pymodule'] is a *module object*.
 
468
  """
469
- sc_path = os.path.join(cwd_for_train, "sitecustomize.py")
 
470
 
471
  # NOTE: Not an f-string. Escape literal braces with {{ }} and only format {module_default}.
472
  code = textwrap.dedent("""\
@@ -505,7 +507,7 @@ def _install_workspace_shim_v3(cwd_for_train: str, module_default: str = "rtdetr
505
  def create(name, **kwargs):
506
  cfg = kwargs.get("cfg")
507
  if not isinstance(cfg, dict):
508
- cfg = {{}} if cfg is None else dict(cfg)
509
  kwargs["cfg"] = cfg
510
  _ensure_pymodule_object(cfg)
511
  return _orig_create(name, **kwargs)
@@ -910,20 +912,33 @@ def training_handler(dataset_path, model_key, run_name, epochs, batch, imgsz, lr
910
  try:
911
  train_cwd = os.path.dirname(train_script)
912
 
913
- # Install kwargs-aware sitecustomize shim (safe, non-invasive)
914
- _install_workspace_shim_v3(train_cwd, module_default="rtdetrv2_pytorch.src")
 
915
 
916
  env = os.environ.copy()
917
- # Make sure repo code can be imported
 
 
 
 
 
 
 
 
 
918
  env["PYTHONPATH"] = os.pathsep.join(filter(None, [
919
- PY_IMPL_DIR, REPO_DIR, env.get("PYTHONPATH", "")
 
 
 
 
 
920
  ]))
921
- # Put our shim first so supervisely import never breaks
922
- shim_root = _install_supervisely_logger_shim()
923
- env["PYTHONPATH"] = os.pathsep.join([shim_root, env["PYTHONPATH"]])
924
  env.setdefault("WANDB_DISABLED", "true")
925
- # Provide a secondary hint for some config loaders
926
  env.setdefault("RTDETR_PYMODULE", "rtdetrv2_pytorch.src")
 
927
 
928
  proc = subprocess.Popen(cmd, cwd=train_cwd,
929
  stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
 
144
  version = None
145
  if len(p) >= 3:
146
  v = p[2]
147
+ if v.lower().startsWith('v') and v[1:].isdigit():
148
  version = int(v[1:])
149
  elif v.isdigit():
150
  version = int(v)
 
459
  return str(root)
460
 
461
  # ---- NEW: kwargs-aware sitecustomize shim (safe, non-invasive) ---------------
462
+ def _install_workspace_shim_v3(dest_dir: str, module_default: str = "rtdetrv2_pytorch.src"):
463
  """
464
  Writes a sitecustomize.py that monkeypatches
465
  rtdetrv2_pytorch.src.core.workspace.create so it works with the real signature:
466
  def create(name, **kwargs):
467
  The shim ensures kwargs['cfg'] is a dict, then guarantees cfg['_pymodule'] is a *module object*.
468
+ `dest_dir` MUST be on sys.path at interpreter startup (we'll prepend it to PYTHONPATH).
469
  """
470
+ os.makedirs(dest_dir, exist_ok=True)
471
+ sc_path = os.path.join(dest_dir, "sitecustomize.py")
472
 
473
  # NOTE: Not an f-string. Escape literal braces with {{ }} and only format {module_default}.
474
  code = textwrap.dedent("""\
 
507
  def create(name, **kwargs):
508
  cfg = kwargs.get("cfg")
509
  if not isinstance(cfg, dict):
510
+ cfg = {} if cfg is None else dict(cfg)
511
  kwargs["cfg"] = cfg
512
  _ensure_pymodule_object(cfg)
513
  return _orig_create(name, **kwargs)
 
912
  try:
913
  train_cwd = os.path.dirname(train_script)
914
 
915
+ # --- NEW: create a temp dir for sitecustomize and put it FIRST on PYTHONPATH
916
+ shim_dir = tempfile.mkdtemp(prefix="rtdetr_site_")
917
+ _install_workspace_shim_v3(shim_dir, module_default="rtdetrv2_pytorch.src")
918
 
919
  env = os.environ.copy()
920
+
921
+ # Supervisely logger shim (can be later in path)
922
+ sly_shim_root = _install_supervisely_logger_shim()
923
+
924
+ # Build PYTHONPATH — order matters!
925
+ # 1) shim_dir (so sitecustomize auto-imports)
926
+ # 2) train_cwd (belt & suspenders; makes local imports easy)
927
+ # 3) PY_IMPL_DIR + REPO_DIR (RT-DETRv2 code)
928
+ # 4) sly_shim_root (optional)
929
+ # 5) existing PYTHONPATH
930
  env["PYTHONPATH"] = os.pathsep.join(filter(None, [
931
+ shim_dir,
932
+ train_cwd,
933
+ PY_IMPL_DIR,
934
+ REPO_DIR,
935
+ sly_shim_root,
936
+ env.get("PYTHONPATH", "")
937
  ]))
938
+
 
 
939
  env.setdefault("WANDB_DISABLED", "true")
 
940
  env.setdefault("RTDETR_PYMODULE", "rtdetrv2_pytorch.src")
941
+ env.setdefault("PYTHONUNBUFFERED", "1") # nicer real-time logs
942
 
943
  proc = subprocess.Popen(cmd, cwd=train_cwd,
944
  stdout=subprocess.PIPE, stderr=subprocess.STDOUT,