Update src/florence/model.py
Browse files- src/florence/model.py +25 -28
src/florence/model.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 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
|
| 8 |
import torch
|
| 9 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
|
|
|
| 10 |
import spaces
|
| 11 |
import gradio as gr
|
| 12 |
|
|
@@ -15,36 +14,34 @@ from src.logger import logging
|
|
| 15 |
from src.exception import CustomExceptionHandling
|
| 16 |
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
#
|
| 20 |
-
#
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 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:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
logging.info("Model and processor loaded successfully.")
|
| 49 |
|
| 50 |
# Handle exceptions that may occur during the process
|
|
|
|
| 1 |
# Importing necessary libraries
|
|
|
|
| 2 |
import sys
|
|
|
|
|
|
|
| 3 |
from typing import Optional
|
| 4 |
+
from unittest.mock import patch
|
| 5 |
from PIL import Image
|
| 6 |
import torch
|
| 7 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 8 |
+
from transformers.dynamic_module_utils import get_imports
|
| 9 |
import spaces
|
| 10 |
import gradio as gr
|
| 11 |
|
|
|
|
| 14 |
from src.exception import CustomExceptionHandling
|
| 15 |
|
| 16 |
|
| 17 |
+
# Florence-2's custom modeling file (modeling_florence2.py) unconditionally
|
| 18 |
+
# lists "flash_attn" as a required import, even though we load the model
|
| 19 |
+
# with attn_implementation="eager" (which never touches flash_attn).
|
| 20 |
+
# Actually installing a working flash-attn build inside a Space is slow
|
| 21 |
+
# and often fails outright, since it needs a prebuilt wheel that exactly
|
| 22 |
+
# matches the image's torch/CUDA/Python ABI -- if no match exists, pip
|
| 23 |
+
# silently produces a non-functional stub package, which still leaves
|
| 24 |
+
# the model unable to load. Since flash_attn isn't actually needed here,
|
| 25 |
+
# the standard fix is to patch transformers' import-checker to drop
|
| 26 |
+
# "flash_attn" from the required-imports list, only for this one file.
|
| 27 |
+
# Reference: https://huggingface.co/microsoft/Florence-2-large-ft/discussions/4
|
| 28 |
+
def _fixed_get_imports(filename):
|
| 29 |
+
imports = get_imports(filename)
|
| 30 |
+
if str(filename).endswith("/modeling_florence2.py") and "flash_attn" in imports:
|
| 31 |
+
imports.remove("flash_attn")
|
| 32 |
+
return imports
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Load model and processor from Hugging Face
|
| 36 |
model_id = "microsoft/Florence-2-large-ft"
|
| 37 |
try:
|
| 38 |
+
with patch("transformers.dynamic_module_utils.get_imports", _fixed_get_imports):
|
| 39 |
+
model = (
|
| 40 |
+
AutoModelForCausalLM.from_pretrained(model_id, attn_implementation="eager", torch_dtype=torch.float16, trust_remote_code=True)
|
| 41 |
+
.to("cuda")
|
| 42 |
+
.eval()
|
| 43 |
+
)
|
| 44 |
+
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 45 |
logging.info("Model and processor loaded successfully.")
|
| 46 |
|
| 47 |
# Handle exceptions that may occur during the process
|