sitammeur commited on
Commit
38637d5
·
verified ·
1 Parent(s): dc384e7

Update src/florence/model.py

Browse files
Files changed (1) hide show
  1. src/florence/model.py +12 -3
src/florence/model.py CHANGED
@@ -1,6 +1,7 @@
1
  # Importing necessary libraries
2
  import os
3
  import sys
 
4
  import subprocess
5
  from typing import Optional
6
  from PIL import Image
@@ -16,17 +17,25 @@ from src.exception import CustomExceptionHandling
16
 
17
  # Install the required dependencies
18
  # NOTE: passing env={...} to subprocess.run REPLACES the entire environment
19
- # instead of extending it, which strips PATH/HOME/etc. and can break the
20
- # pip install (and therefore the whole Space) at import time. Copy the
21
- # current environment first and only add/override the flag we need.
22
  env = os.environ.copy()
23
  env["FLASH_ATTENTION_SKIP_CUDA_BUILD"] = "TRUE"
24
  subprocess.run(
25
  "pip install flash-attn --no-build-isolation",
26
  env=env,
27
  shell=True,
 
28
  )
29
 
 
 
 
 
 
 
 
 
30
  # Load model and processor from Hugging Face
31
  model_id = "microsoft/Florence-2-large-ft"
32
  try:
 
1
  # Importing necessary libraries
2
  import os
3
  import sys
4
+ import importlib
5
  import subprocess
6
  from typing import Optional
7
  from PIL import Image
 
17
 
18
  # Install the required dependencies
19
  # NOTE: passing env={...} to subprocess.run REPLACES the entire environment
20
+ # instead of extending it, which strips PATH/HOME/etc. Copy the current
21
+ # environment first and only add/override the flag we need.
 
22
  env = os.environ.copy()
23
  env["FLASH_ATTENTION_SKIP_CUDA_BUILD"] = "TRUE"
24
  subprocess.run(
25
  "pip install flash-attn --no-build-isolation",
26
  env=env,
27
  shell=True,
28
+ check=True,
29
  )
30
 
31
+ # The pip install above happens in a subprocess AFTER this Python process
32
+ # has already started, so the interpreter's import machinery has a stale
33
+ # view of what's on disk (sys.path_importer_cache). Without invalidating
34
+ # it, transformers' internal check_imports()/find_spec() lookup for
35
+ # flash_attn reports "not found" even though it just installed
36
+ # successfully -- this is what was causing the ImportError at model load.
37
+ importlib.invalidate_caches()
38
+
39
  # Load model and processor from Hugging Face
40
  model_id = "microsoft/Florence-2-large-ft"
41
  try: