AnseMin commited on
Commit
17cb3f3
·
1 Parent(s): 4968a76

Error: Error processing document with GOT-OCR (ZeroGPU): Numpy is not available

Browse files
Files changed (3) hide show
  1. requirements.txt +3 -0
  2. setup.sh +5 -0
  3. src/parsers/got_ocr_parser.py +55 -6
requirements.txt CHANGED
@@ -14,6 +14,9 @@ pydantic==2.7.1
14
  # Gemini API client
15
  google-genai>=0.1.0
16
 
 
 
 
17
  # GOT-OCR dependencies (as specified in documentation)
18
  torch==2.0.1
19
  torchvision==0.15.2
 
14
  # Gemini API client
15
  google-genai>=0.1.0
16
 
17
+ # NumPy is required by torch and other libraries
18
+ numpy>=1.24.0 # Added explicit dependency with higher version
19
+
20
  # GOT-OCR dependencies (as specified in documentation)
21
  torch==2.0.1
22
  torchvision==0.15.2
setup.sh CHANGED
@@ -18,6 +18,11 @@ else
18
  echo "If system dependencies are needed, please run this script with sudo."
19
  fi
20
 
 
 
 
 
 
21
  # Install Python dependencies
22
  echo "Installing Python dependencies..."
23
  pip install -q -U pillow opencv-python-headless
 
18
  echo "If system dependencies are needed, please run this script with sudo."
19
  fi
20
 
21
+ # Install NumPy first as it's required by many other packages
22
+ echo "Installing NumPy..."
23
+ pip install -q -U numpy>=1.24.0
24
+ echo "NumPy installed successfully"
25
+
26
  # Install Python dependencies
27
  echo "Installing Python dependencies..."
28
  pip install -q -U pillow opencv-python-headless
src/parsers/got_ocr_parser.py CHANGED
@@ -13,6 +13,15 @@ logger = logging.getLogger(__name__)
13
 
14
  # Check if required packages are installed
15
  try:
 
 
 
 
 
 
 
 
 
16
  import torch
17
  import transformers
18
  from transformers import AutoModel, AutoTokenizer
@@ -34,10 +43,11 @@ try:
34
  ZEROGPU_AVAILABLE = False
35
  logger.info("ZeroGPU not available, will use standard GPU if available")
36
 
37
- GOT_AVAILABLE = True
38
  except ImportError:
39
  GOT_AVAILABLE = False
40
  ZEROGPU_AVAILABLE = False
 
41
  logger.warning("GOT-OCR dependencies not installed. The parser will not be available.")
42
 
43
  class GotOcrParser(DocumentParser):
@@ -68,6 +78,9 @@ class GotOcrParser(DocumentParser):
68
  @classmethod
69
  def _load_model(cls):
70
  """Load the GOT-OCR model and tokenizer if not already loaded."""
 
 
 
71
  if cls._model is None or cls._tokenizer is None:
72
  try:
73
  logger.info("Loading GOT-OCR model and tokenizer...")
@@ -123,10 +136,16 @@ class GotOcrParser(DocumentParser):
123
  def parse(self, file_path: Union[str, Path], ocr_method: Optional[str] = None, **kwargs) -> str:
124
  """Parse a document using GOT-OCR 2.0."""
125
  if not GOT_AVAILABLE:
126
- raise ImportError(
127
- "GOT-OCR dependencies not installed. Please install required packages: "
128
- "torch, transformers, tiktoken, verovio, accelerate"
129
- )
 
 
 
 
 
 
130
 
131
  # Check if CUDA is available
132
  if not torch.cuda.is_available():
@@ -145,7 +164,14 @@ class GotOcrParser(DocumentParser):
145
 
146
  # Use ZeroGPU if available, otherwise use regular processing
147
  if ZEROGPU_AVAILABLE:
148
- return self._parse_with_zerogpu(file_path, ocr_type, **kwargs)
 
 
 
 
 
 
 
149
  else:
150
  return self._parse_regular(file_path, ocr_type, **kwargs)
151
 
@@ -194,6 +220,18 @@ class GotOcrParser(DocumentParser):
194
  # Define the GPU-dependent function
195
  @spaces.GPU
196
  def process_with_gpu():
 
 
 
 
 
 
 
 
 
 
 
 
197
  # Load the model
198
  self._load_model()
199
 
@@ -211,6 +249,17 @@ class GotOcrParser(DocumentParser):
211
  # Format and return the result
212
  return self._format_result(result, **kwargs)
213
 
 
 
 
 
 
 
 
 
 
 
 
214
  except Exception as e:
215
  logger.error(f"Error processing document with GOT-OCR (ZeroGPU): {str(e)}")
216
  raise RuntimeError(f"Error processing document with GOT-OCR (ZeroGPU): {str(e)}")
 
13
 
14
  # Check if required packages are installed
15
  try:
16
+ # First check for numpy as it's required by torch
17
+ try:
18
+ import numpy as np
19
+ NUMPY_AVAILABLE = True
20
+ logger.info(f"NumPy version {np.__version__} is available")
21
+ except ImportError:
22
+ NUMPY_AVAILABLE = False
23
+ logger.error("NumPy is not available. This is required for GOT-OCR.")
24
+
25
  import torch
26
  import transformers
27
  from transformers import AutoModel, AutoTokenizer
 
43
  ZEROGPU_AVAILABLE = False
44
  logger.info("ZeroGPU not available, will use standard GPU if available")
45
 
46
+ GOT_AVAILABLE = True and NUMPY_AVAILABLE
47
  except ImportError:
48
  GOT_AVAILABLE = False
49
  ZEROGPU_AVAILABLE = False
50
+ NUMPY_AVAILABLE = False
51
  logger.warning("GOT-OCR dependencies not installed. The parser will not be available.")
52
 
53
  class GotOcrParser(DocumentParser):
 
78
  @classmethod
79
  def _load_model(cls):
80
  """Load the GOT-OCR model and tokenizer if not already loaded."""
81
+ if not NUMPY_AVAILABLE:
82
+ raise ImportError("NumPy is not available. This is required for GOT-OCR.")
83
+
84
  if cls._model is None or cls._tokenizer is None:
85
  try:
86
  logger.info("Loading GOT-OCR model and tokenizer...")
 
136
  def parse(self, file_path: Union[str, Path], ocr_method: Optional[str] = None, **kwargs) -> str:
137
  """Parse a document using GOT-OCR 2.0."""
138
  if not GOT_AVAILABLE:
139
+ if not NUMPY_AVAILABLE:
140
+ raise ImportError(
141
+ "NumPy is not available. This is required for GOT-OCR. "
142
+ "Please ensure NumPy is installed in your environment."
143
+ )
144
+ else:
145
+ raise ImportError(
146
+ "GOT-OCR dependencies not installed. Please install required packages: "
147
+ "torch, transformers, tiktoken, verovio, accelerate"
148
+ )
149
 
150
  # Check if CUDA is available
151
  if not torch.cuda.is_available():
 
164
 
165
  # Use ZeroGPU if available, otherwise use regular processing
166
  if ZEROGPU_AVAILABLE:
167
+ try:
168
+ return self._parse_with_zerogpu(file_path, ocr_type, **kwargs)
169
+ except RuntimeError as e:
170
+ if "numpy" in str(e).lower():
171
+ logger.warning("NumPy issues in ZeroGPU environment, falling back to regular processing")
172
+ return self._parse_regular(file_path, ocr_type, **kwargs)
173
+ else:
174
+ raise
175
  else:
176
  return self._parse_regular(file_path, ocr_type, **kwargs)
177
 
 
220
  # Define the GPU-dependent function
221
  @spaces.GPU
222
  def process_with_gpu():
223
+ # Ensure NumPy is available
224
+ try:
225
+ import numpy
226
+ except ImportError:
227
+ # Try to install numpy if not available
228
+ import subprocess
229
+ import sys
230
+ logger.warning("NumPy not found in ZeroGPU environment, attempting to install...")
231
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "numpy>=1.24.0"])
232
+ import numpy
233
+ logger.info(f"NumPy {numpy.__version__} installed successfully in ZeroGPU environment")
234
+
235
  # Load the model
236
  self._load_model()
237
 
 
249
  # Format and return the result
250
  return self._format_result(result, **kwargs)
251
 
252
+ except ImportError as e:
253
+ if "numpy" in str(e).lower():
254
+ logger.error(f"NumPy import error in ZeroGPU environment: {str(e)}")
255
+ raise RuntimeError(
256
+ "NumPy is not available in the ZeroGPU environment. "
257
+ "This is a known issue with some HuggingFace Spaces. "
258
+ "Please try using a different parser or contact support."
259
+ )
260
+ else:
261
+ logger.error(f"Import error in ZeroGPU environment: {str(e)}")
262
+ raise RuntimeError(f"Error processing document with GOT-OCR (ZeroGPU): {str(e)}")
263
  except Exception as e:
264
  logger.error(f"Error processing document with GOT-OCR (ZeroGPU): {str(e)}")
265
  raise RuntimeError(f"Error processing document with GOT-OCR (ZeroGPU): {str(e)}")