sitammeur commited on
Commit
b998f35
Β·
verified Β·
1 Parent(s): 0f586d0

Update src/florence/model.py

Browse files
Files changed (1) hide show
  1. src/florence/model.py +36 -10
src/florence/model.py CHANGED
@@ -1,11 +1,11 @@
1
  # Importing necessary libraries
2
- import spaces
3
  import sys
4
  import subprocess
5
  from typing import Optional
6
  from PIL import Image
7
  import torch
8
  from transformers import AutoProcessor, AutoModelForCausalLM
 
9
  import gradio as gr
10
 
11
  # Local imports
@@ -13,12 +13,40 @@ from src.logger import logging
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,9 +66,7 @@ except Exception as e:
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
 
 
1
  # Importing necessary libraries
 
2
  import sys
3
  import subprocess
4
  from typing import Optional
5
  from PIL import Image
6
  import torch
7
  from transformers import AutoProcessor, AutoModelForCausalLM
8
+ import spaces
9
  import gradio as gr
10
 
11
  # Local imports
 
13
  from src.exception import CustomExceptionHandling
14
 
15
 
16
+ # Install the required dependencies (flash-attn)
17
+ # Check if flash_attn is already installed before attempting installation
18
+ try:
19
+ import flash_attn
20
+ logging.info("flash_attn is already installed.")
21
+ except ImportError:
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
 
67
 
68
  @spaces.GPU
69
+ def run_example(task_prompt: str, image: Image.Image, text_input: Optional[str] = None) -> str:
 
 
70
  """
71
  Runs an example using the given task prompt and image.
72