Update src/florence/model.py
Browse files- src/florence/model.py +9 -35
src/florence/model.py
CHANGED
|
@@ -13,40 +13,12 @@ from src.logger import logging
|
|
| 13 |
from src.exception import CustomExceptionHandling
|
| 14 |
|
| 15 |
|
| 16 |
-
# Install the required dependencies
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
logging.info("flash_attn not found. Attempting to install...")
|
| 23 |
-
try:
|
| 24 |
-
# Set environment variable to skip CUDA build if needed
|
| 25 |
-
import os
|
| 26 |
-
env = os.environ.copy()
|
| 27 |
-
env["FLASH_ATTENTION_SKIP_CUDA_BUILD"] = "TRUE"
|
| 28 |
-
|
| 29 |
-
result = subprocess.run(
|
| 30 |
-
["pip", "install", "flash-attn", "--no-build-isolation"],
|
| 31 |
-
env=env,
|
| 32 |
-
capture_output=True,
|
| 33 |
-
text=True,
|
| 34 |
-
check=False
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
if result.returncode == 0:
|
| 38 |
-
logging.info("flash_attn installed successfully.")
|
| 39 |
-
else:
|
| 40 |
-
logging.warning(f"flash_attn installation may have failed: {result.stderr}")
|
| 41 |
-
# Try importing again to verify
|
| 42 |
-
try:
|
| 43 |
-
import flash_attn
|
| 44 |
-
logging.info("flash_attn import successful after installation.")
|
| 45 |
-
except ImportError:
|
| 46 |
-
logging.error("flash_attn installation failed. The model may not work correctly.")
|
| 47 |
-
except Exception as e:
|
| 48 |
-
logging.warning(f"Error during flash_attn installation: {e}")
|
| 49 |
-
logging.warning("Continuing without flash_attn. The model may require it.")
|
| 50 |
|
| 51 |
# Load model and processor from Hugging Face
|
| 52 |
model_id = "microsoft/Florence-2-large-ft"
|
|
@@ -66,7 +38,9 @@ except Exception as e:
|
|
| 66 |
|
| 67 |
|
| 68 |
@spaces.GPU
|
| 69 |
-
def run_example(
|
|
|
|
|
|
|
| 70 |
"""
|
| 71 |
Runs an example using the given task prompt and image.
|
| 72 |
|
|
|
|
| 13 |
from src.exception import CustomExceptionHandling
|
| 14 |
|
| 15 |
|
| 16 |
+
# Install the required dependencies
|
| 17 |
+
subprocess.run(
|
| 18 |
+
"pip install flash-attn --no-build-isolation",
|
| 19 |
+
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
|
| 20 |
+
shell=True,
|
| 21 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Load model and processor from Hugging Face
|
| 24 |
model_id = "microsoft/Florence-2-large-ft"
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
@spaces.GPU
|
| 41 |
+
def run_example(
|
| 42 |
+
task_prompt: str, image: Image.Image, text_input: Optional[str] = None
|
| 43 |
+
) -> str:
|
| 44 |
"""
|
| 45 |
Runs an example using the given task prompt and image.
|
| 46 |
|