hvoss-techfak commited on
Commit
c8c1bb4
·
1 Parent(s): d932bbf
Files changed (1) hide show
  1. app.py +14 -31
app.py CHANGED
@@ -4,9 +4,9 @@ import uuid
4
  import os
5
  import logging
6
  import zipfile
7
- import importlib
8
  import wandb
9
  from contextlib import redirect_stdout, redirect_stderr
 
10
 
11
  USE_WANDB = "WANDB_API_KEY" in os.environ
12
  if USE_WANDB:
@@ -295,30 +295,23 @@ def run_autoforge_process(cmd, log_path):
295
  from joblib import parallel_backend
296
  cli_args = cmd[1:]
297
 
298
- # Import the library's high-level module which contains the main() entrypoint
299
- # that uses the internal @spaces.GPU decorators (autoforge/auto_forge.py).
300
- # Do NOT fall back to any other module; require the project module to be present.
301
- try:
302
- autoforge_main = importlib.import_module("autoforge.auto_forge")
303
- except Exception as e:
304
- # If the direct import fails, write the error to the log and return failure.
305
- with open(log_path, "a", encoding="utf-8") as log_f:
306
- log_f.write(f"\nERROR: Could not import autoforge.auto_forge: {e}\n")
307
- return -1
308
-
309
  exit_code = 0
310
- with open(log_path, "w", buffering=1, encoding="utf-8") as log_f, \
311
- redirect_stdout(log_f), redirect_stderr(log_f), parallel_backend("threading", n_jobs=-1):
 
 
 
 
312
  try:
313
- # Provide argv so the autoforge CLI parser sees the same arguments as before.
 
 
 
314
  sys.argv = ["autoforge"] + cli_args
315
- # Call the library's main() function. It may call sys.exit(); capture that.
316
- autoforge_main.main()
317
  except SystemExit as e:
318
- # Preserve the exit code behavior from the subprocess-style invocation.
319
  exit_code = e.code if isinstance(e.code, int) or e.code is None else 0
320
  except Exception as e:
321
- # Log exceptions to the log file and return a failure code.
322
  log_f.write(f"\nERROR: {e}\n")
323
  exit_code = -1
324
 
@@ -799,22 +792,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
799
  if script_dir not in sys.path:
800
  sys.path.insert(0, script_dir)
801
 
802
- # Import the local auto_forge module using a plain import (no importlib utilities)
803
  with open(self.log_path, "a", encoding="utf-8") as lf, redirect_stdout(lf), redirect_stderr(lf):
804
  try:
805
- # Use plain import; if already imported, the module's top-level code won't re-run.
806
- import auto_forge
807
-
808
  # Provide argv for the module's CLI parsing and call main()
809
  sys.argv = ["autoforge"] + (self.cmd[1:] if len(self.cmd) > 1 else [])
810
- if hasattr(auto_forge, "main"):
811
- try:
812
- auto_forge.main()
813
- self.returncode = 0
814
- except SystemExit as se:
815
- self.returncode = se.code if isinstance(se.code, int) or se.code is None else 0
816
- else:
817
- raise AttributeError("auto_forge module does not expose a main() function")
818
  except Exception as e:
819
  lf.write(f"\nERROR while importing/running auto_forge: {exc_text(e)}\n")
820
  self.exc = e
 
4
  import os
5
  import logging
6
  import zipfile
 
7
  import wandb
8
  from contextlib import redirect_stdout, redirect_stderr
9
+ import auto_forge
10
 
11
  USE_WANDB = "WANDB_API_KEY" in os.environ
12
  if USE_WANDB:
 
295
  from joblib import parallel_backend
296
  cli_args = cmd[1:]
297
 
 
 
 
 
 
 
 
 
 
 
 
298
  exit_code = 0
299
+ # Ensure local project dir is first on sys.path so `import auto_forge` imports the file in this repo
300
+ script_dir = os.path.dirname(os.path.abspath(__file__))
301
+ if script_dir not in sys.path:
302
+ sys.path.insert(0, script_dir)
303
+
304
+ with open(log_path, "w", buffering=1, encoding="utf-8") as log_f, redirect_stdout(log_f), redirect_stderr(log_f), parallel_backend("threading", n_jobs=-1):
305
  try:
306
+ # Force a fresh import of the local module by removing any cached module
307
+ if "auto_forge" in sys.modules:
308
+ del sys.modules["auto_forge"]
309
+ auto_forge = __import__("auto_forge")
310
  sys.argv = ["autoforge"] + cli_args
311
+ auto_forge.main()
 
312
  except SystemExit as e:
 
313
  exit_code = e.code if isinstance(e.code, int) or e.code is None else 0
314
  except Exception as e:
 
315
  log_f.write(f"\nERROR: {e}\n")
316
  exit_code = -1
317
 
 
792
  if script_dir not in sys.path:
793
  sys.path.insert(0, script_dir)
794
 
 
795
  with open(self.log_path, "a", encoding="utf-8") as lf, redirect_stdout(lf), redirect_stderr(lf):
796
  try:
 
 
 
797
  # Provide argv for the module's CLI parsing and call main()
798
  sys.argv = ["autoforge"] + (self.cmd[1:] if len(self.cmd) > 1 else [])
799
+ auto_forge.main()
800
+ self.returncode = 0
 
 
 
 
 
 
801
  except Exception as e:
802
  lf.write(f"\nERROR while importing/running auto_forge: {exc_text(e)}\n")
803
  self.exc = e