yonigozlan HF Staff commited on
Commit
7084f3d
·
1 Parent(s): 0524669

fix imports

Browse files
Files changed (1) hide show
  1. app.py +15 -7
app.py CHANGED
@@ -463,18 +463,26 @@ if not os.path.exists(repo_dir):
463
  subprocess.run(["git", "clone", "https://github.com/huggingface/transformers.git", repo_dir], check=True)
464
  subprocess.run(["pip", "install", "-e", repo_dir], check=True)
465
 
466
- # The editable install replaced the transformers package on disk, but
467
- # Python's module cache (sys.modules) still holds the version that was
468
- # imported at the top of this file. Flush stale entries and re-import
469
- # so that transformers.models.auto.modeling_auto is resolvable on this
470
- # very first run (without requiring a restart).
 
 
 
471
  import importlib
472
 
473
- importlib.invalidate_caches()
 
 
 
474
  _stale_keys = [k for k in sys.modules if k == "transformers" or k.startswith("transformers.")]
475
  for _k in _stale_keys:
476
  del sys.modules[_k]
477
- import transformers # re-import from the fresh editable install
 
 
478
 
479
  transformers_path = os.path.dirname(transformers.__file__)
480
  else:
 
463
  subprocess.run(["git", "clone", "https://github.com/huggingface/transformers.git", repo_dir], check=True)
464
  subprocess.run(["pip", "install", "-e", repo_dir], check=True)
465
 
466
+ # The editable install sets up a .pth file in site-packages so that
467
+ # transformers is importable on *future* Python startups, but the
468
+ # current interpreter has already loaded the old version into
469
+ # sys.modules and the .pth file hasn't been processed by site.py.
470
+ # Fix both problems:
471
+ # 1. Put the cloned repo's src/ at the front of sys.path so the
472
+ # import machinery can actually find the package.
473
+ # 2. Flush all stale "transformers" entries from sys.modules.
474
  import importlib
475
 
476
+ _repo_src = os.path.join(repo_dir, "src")
477
+ if _repo_src not in sys.path:
478
+ sys.path.insert(0, _repo_src)
479
+
480
  _stale_keys = [k for k in sys.modules if k == "transformers" or k.startswith("transformers.")]
481
  for _k in _stale_keys:
482
  del sys.modules[_k]
483
+
484
+ importlib.invalidate_caches()
485
+ import transformers # re-import from repo_dir/src/
486
 
487
  transformers_path = os.path.dirname(transformers.__file__)
488
  else: