Commit ·
7084f3d
1
Parent(s): 0524669
fix imports
Browse files
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
|
| 467 |
-
#
|
| 468 |
-
#
|
| 469 |
-
#
|
| 470 |
-
#
|
|
|
|
|
|
|
|
|
|
| 471 |
import importlib
|
| 472 |
|
| 473 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
| 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:
|