Spaces:
Runtime error
Runtime error
Error: Error processing document with GOT-OCR (ZeroGPU): Numpy is not available
Browse files- src/parsers/got_ocr_parser.py +88 -12
src/parsers/got_ocr_parser.py
CHANGED
|
@@ -4,6 +4,8 @@ import json
|
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
import logging
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from src.parsers.parser_interface import DocumentParser
|
| 9 |
from src.parsers.parser_registry import ParserRegistry
|
|
@@ -11,17 +13,22 @@ from src.parsers.parser_registry import ParserRegistry
|
|
| 11 |
# Configure logging
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
import torch
|
| 26 |
import transformers
|
| 27 |
from transformers import AutoModel, AutoTokenizer
|
|
@@ -37,7 +44,6 @@ try:
|
|
| 37 |
GOT_AVAILABLE = True and NUMPY_AVAILABLE
|
| 38 |
except ImportError:
|
| 39 |
GOT_AVAILABLE = False
|
| 40 |
-
NUMPY_AVAILABLE = False
|
| 41 |
logger.warning("GOT-OCR dependencies not installed. The parser will not be available.")
|
| 42 |
|
| 43 |
class GotOcrParser(DocumentParser):
|
|
@@ -72,6 +78,8 @@ class GotOcrParser(DocumentParser):
|
|
| 72 |
@classmethod
|
| 73 |
def _load_model(cls):
|
| 74 |
"""Load the GOT-OCR model and tokenizer if not already loaded."""
|
|
|
|
|
|
|
| 75 |
if not NUMPY_AVAILABLE:
|
| 76 |
raise ImportError("NumPy is not available. This is required for GOT-OCR.")
|
| 77 |
|
|
@@ -127,15 +135,73 @@ class GotOcrParser(DocumentParser):
|
|
| 127 |
|
| 128 |
logger.info("GOT-OCR model released from memory")
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
def parse(self, file_path: Union[str, Path], ocr_method: Optional[str] = None, **kwargs) -> str:
|
| 131 |
"""Parse a document using GOT-OCR 2.0."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
if not GOT_AVAILABLE:
|
| 133 |
if not NUMPY_AVAILABLE:
|
|
|
|
| 134 |
raise ImportError(
|
| 135 |
"NumPy is not available. This is required for GOT-OCR. "
|
| 136 |
-
"Please ensure NumPy is installed in your environment."
|
|
|
|
| 137 |
)
|
| 138 |
else:
|
|
|
|
| 139 |
raise ImportError(
|
| 140 |
"GOT-OCR dependencies not installed. Please install required packages: "
|
| 141 |
"torch, transformers, tiktoken, verovio, accelerate"
|
|
@@ -157,6 +223,16 @@ class GotOcrParser(DocumentParser):
|
|
| 157 |
ocr_type = "format" if ocr_method == "format" else "ocr"
|
| 158 |
|
| 159 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
# Load the model
|
| 161 |
self._load_model()
|
| 162 |
|
|
|
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
import logging
|
| 7 |
+
import sys
|
| 8 |
+
import importlib
|
| 9 |
|
| 10 |
from src.parsers.parser_interface import DocumentParser
|
| 11 |
from src.parsers.parser_registry import ParserRegistry
|
|
|
|
| 13 |
# Configure logging
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
# Global flag for NumPy availability
|
| 17 |
+
NUMPY_AVAILABLE = False
|
| 18 |
+
NUMPY_VERSION = None
|
| 19 |
+
|
| 20 |
+
# Try to import NumPy
|
| 21 |
try:
|
| 22 |
+
import numpy as np
|
| 23 |
+
NUMPY_AVAILABLE = True
|
| 24 |
+
NUMPY_VERSION = np.__version__
|
| 25 |
+
logger.info(f"NumPy version {NUMPY_VERSION} is available")
|
| 26 |
+
except ImportError:
|
| 27 |
+
NUMPY_AVAILABLE = False
|
| 28 |
+
logger.error("NumPy is not available. This is required for GOT-OCR.")
|
| 29 |
+
|
| 30 |
+
# Check if required packages are installed
|
| 31 |
+
try:
|
| 32 |
import torch
|
| 33 |
import transformers
|
| 34 |
from transformers import AutoModel, AutoTokenizer
|
|
|
|
| 44 |
GOT_AVAILABLE = True and NUMPY_AVAILABLE
|
| 45 |
except ImportError:
|
| 46 |
GOT_AVAILABLE = False
|
|
|
|
| 47 |
logger.warning("GOT-OCR dependencies not installed. The parser will not be available.")
|
| 48 |
|
| 49 |
class GotOcrParser(DocumentParser):
|
|
|
|
| 78 |
@classmethod
|
| 79 |
def _load_model(cls):
|
| 80 |
"""Load the GOT-OCR model and tokenizer if not already loaded."""
|
| 81 |
+
global NUMPY_AVAILABLE
|
| 82 |
+
|
| 83 |
if not NUMPY_AVAILABLE:
|
| 84 |
raise ImportError("NumPy is not available. This is required for GOT-OCR.")
|
| 85 |
|
|
|
|
| 135 |
|
| 136 |
logger.info("GOT-OCR model released from memory")
|
| 137 |
|
| 138 |
+
def _try_install_numpy(self):
|
| 139 |
+
"""Attempt to install NumPy using pip."""
|
| 140 |
+
global NUMPY_AVAILABLE, NUMPY_VERSION
|
| 141 |
+
|
| 142 |
+
logger.warning("Attempting to install NumPy...")
|
| 143 |
+
try:
|
| 144 |
+
import subprocess
|
| 145 |
+
# Try to install numpy without specifying a version
|
| 146 |
+
result = subprocess.run(
|
| 147 |
+
[sys.executable, "-m", "pip", "install", "-q", "numpy", "--no-cache-dir"],
|
| 148 |
+
capture_output=True,
|
| 149 |
+
text=True,
|
| 150 |
+
check=True
|
| 151 |
+
)
|
| 152 |
+
logger.info(f"NumPy installation result: {result.stdout}")
|
| 153 |
+
|
| 154 |
+
# Try to import numpy again
|
| 155 |
+
importlib.invalidate_caches()
|
| 156 |
+
import numpy as np
|
| 157 |
+
importlib.reload(np)
|
| 158 |
+
|
| 159 |
+
NUMPY_AVAILABLE = True
|
| 160 |
+
NUMPY_VERSION = np.__version__
|
| 161 |
+
logger.info(f"NumPy installed successfully: version {NUMPY_VERSION}")
|
| 162 |
+
return True
|
| 163 |
+
except Exception as e:
|
| 164 |
+
logger.error(f"Failed to install NumPy: {str(e)}")
|
| 165 |
+
if hasattr(e, 'stderr'):
|
| 166 |
+
logger.error(f"Installation error output: {e.stderr}")
|
| 167 |
+
return False
|
| 168 |
+
|
| 169 |
def parse(self, file_path: Union[str, Path], ocr_method: Optional[str] = None, **kwargs) -> str:
|
| 170 |
"""Parse a document using GOT-OCR 2.0."""
|
| 171 |
+
global NUMPY_AVAILABLE, GOT_AVAILABLE
|
| 172 |
+
|
| 173 |
+
# Check NumPy availability and try to install if not available
|
| 174 |
+
if not NUMPY_AVAILABLE:
|
| 175 |
+
logger.warning("NumPy not available, attempting to install it...")
|
| 176 |
+
if self._try_install_numpy():
|
| 177 |
+
# Update GOT availability flag if NumPy is now available
|
| 178 |
+
try:
|
| 179 |
+
import torch
|
| 180 |
+
import transformers
|
| 181 |
+
GOT_AVAILABLE = True
|
| 182 |
+
logger.info("Updated GOT availability after installing NumPy: Available")
|
| 183 |
+
except ImportError:
|
| 184 |
+
GOT_AVAILABLE = False
|
| 185 |
+
logger.error("Updated GOT availability after installing NumPy: Not Available (missing other dependencies)")
|
| 186 |
+
else:
|
| 187 |
+
logger.error("Failed to install NumPy. Cannot proceed with GOT-OCR.")
|
| 188 |
+
raise ImportError(
|
| 189 |
+
"NumPy is not available and could not be installed automatically. "
|
| 190 |
+
"Please ensure NumPy is installed in your environment. "
|
| 191 |
+
"Add the following to your logs for debugging: NUMPY_INSTALLATION_FAILED"
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
# Check overall GOT availability
|
| 195 |
if not GOT_AVAILABLE:
|
| 196 |
if not NUMPY_AVAILABLE:
|
| 197 |
+
logger.error("NumPy is still not available after installation attempt.")
|
| 198 |
raise ImportError(
|
| 199 |
"NumPy is not available. This is required for GOT-OCR. "
|
| 200 |
+
"Please ensure NumPy is installed in your environment. "
|
| 201 |
+
"Environment details: Python " + sys.version
|
| 202 |
)
|
| 203 |
else:
|
| 204 |
+
logger.error("Other GOT-OCR dependencies missing even though NumPy is available.")
|
| 205 |
raise ImportError(
|
| 206 |
"GOT-OCR dependencies not installed. Please install required packages: "
|
| 207 |
"torch, transformers, tiktoken, verovio, accelerate"
|
|
|
|
| 223 |
ocr_type = "format" if ocr_method == "format" else "ocr"
|
| 224 |
|
| 225 |
try:
|
| 226 |
+
# Check if numpy needs to be reloaded
|
| 227 |
+
if 'numpy' in sys.modules:
|
| 228 |
+
logger.info("NumPy module found in sys.modules, attempting to reload...")
|
| 229 |
+
try:
|
| 230 |
+
importlib.reload(sys.modules['numpy'])
|
| 231 |
+
import numpy as np
|
| 232 |
+
logger.info(f"NumPy reloaded successfully: version {np.__version__}")
|
| 233 |
+
except Exception as e:
|
| 234 |
+
logger.error(f"Error reloading NumPy: {str(e)}")
|
| 235 |
+
|
| 236 |
# Load the model
|
| 237 |
self._load_model()
|
| 238 |
|