Spaces:
Running
Running
continues to remove cache parameter
Browse files
app.py
CHANGED
|
@@ -174,7 +174,7 @@ def initialize_model_safely():
|
|
| 174 |
"""
|
| 175 |
model_name = 'ucaslcl/GOT-OCR2_0'
|
| 176 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 177 |
-
|
| 178 |
try:
|
| 179 |
# Initialize tokenizer with proper settings
|
| 180 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
|
@@ -262,6 +262,58 @@ def direct_model_call(model, method_name, *args, **kwargs):
|
|
| 262 |
method = getattr(model, method_name)
|
| 263 |
return method(*args, **clean_kwargs)
|
| 264 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
@spaces.GPU()
|
| 266 |
def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None):
|
| 267 |
"""
|
|
@@ -293,22 +345,22 @@ def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None):
|
|
| 293 |
else:
|
| 294 |
return "Error: Unsupported image format", None, None
|
| 295 |
|
| 296 |
-
# Use
|
| 297 |
try:
|
| 298 |
if task == "Plain Text OCR":
|
| 299 |
-
res =
|
| 300 |
return res, None, unique_id
|
| 301 |
else:
|
| 302 |
if task == "Format Text OCR":
|
| 303 |
-
res =
|
| 304 |
elif task == "Fine-grained OCR (Box)":
|
| 305 |
-
res =
|
| 306 |
elif task == "Fine-grained OCR (Color)":
|
| 307 |
-
res =
|
| 308 |
elif task == "Multi-crop OCR":
|
| 309 |
-
res =
|
| 310 |
elif task == "Render Formatted OCR":
|
| 311 |
-
res =
|
| 312 |
|
| 313 |
if os.path.exists(result_path):
|
| 314 |
with open(result_path, 'r') as f:
|
|
@@ -317,7 +369,7 @@ def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None):
|
|
| 317 |
else:
|
| 318 |
return res, None, unique_id
|
| 319 |
except Exception as e:
|
| 320 |
-
# If
|
| 321 |
try:
|
| 322 |
if task == "Plain Text OCR":
|
| 323 |
res = cache_manager.safe_call('chat', tokenizer, image_path, ocr_type='ocr')
|
|
@@ -348,7 +400,7 @@ def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None):
|
|
| 348 |
finally:
|
| 349 |
if os.path.exists(image_path):
|
| 350 |
os.remove(image_path)
|
| 351 |
-
|
| 352 |
def update_image_input(task):
|
| 353 |
if task == "Fine-grained OCR (Color)":
|
| 354 |
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
|
|
|
| 174 |
"""
|
| 175 |
model_name = 'ucaslcl/GOT-OCR2_0'
|
| 176 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 177 |
+
|
| 178 |
try:
|
| 179 |
# Initialize tokenizer with proper settings
|
| 180 |
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
|
|
|
| 262 |
method = getattr(model, method_name)
|
| 263 |
return method(*args, **clean_kwargs)
|
| 264 |
|
| 265 |
+
def safe_model_call_with_dynamic_cache_fix(model, method_name, *args, **kwargs):
|
| 266 |
+
"""
|
| 267 |
+
Safe model call that handles DynamicCache errors specifically
|
| 268 |
+
"""
|
| 269 |
+
try:
|
| 270 |
+
return direct_model_call(model, method_name, *args, **kwargs)
|
| 271 |
+
except AttributeError as e:
|
| 272 |
+
if "get_max_length" in str(e) and "DynamicCache" in str(e):
|
| 273 |
+
# This is the specific DynamicCache error we need to handle
|
| 274 |
+
print("DynamicCache error detected, applying workaround...")
|
| 275 |
+
|
| 276 |
+
# Try to clear any existing cache
|
| 277 |
+
try:
|
| 278 |
+
if hasattr(model, 'clear_cache'):
|
| 279 |
+
model.clear_cache()
|
| 280 |
+
# Also try to clear transformers cache
|
| 281 |
+
import torch
|
| 282 |
+
if torch.cuda.is_available():
|
| 283 |
+
torch.cuda.empty_cache()
|
| 284 |
+
except:
|
| 285 |
+
pass
|
| 286 |
+
|
| 287 |
+
# Try the call again with minimal parameters
|
| 288 |
+
try:
|
| 289 |
+
# Create minimal kwargs with only essential parameters
|
| 290 |
+
minimal_kwargs = {}
|
| 291 |
+
essential_params = ['ocr_type', 'render', 'save_render_file', 'ocr_box', 'ocr_color']
|
| 292 |
+
for key, value in kwargs.items():
|
| 293 |
+
if key in essential_params and 'cache' not in key.lower():
|
| 294 |
+
minimal_kwargs[key] = value
|
| 295 |
+
|
| 296 |
+
method = getattr(model, method_name)
|
| 297 |
+
return method(*args, **minimal_kwargs)
|
| 298 |
+
except Exception as retry_error:
|
| 299 |
+
# If still failing, try with even more minimal approach
|
| 300 |
+
try:
|
| 301 |
+
# Try with only the most basic parameters
|
| 302 |
+
basic_kwargs = {}
|
| 303 |
+
if 'ocr_type' in kwargs:
|
| 304 |
+
basic_kwargs['ocr_type'] = kwargs['ocr_type']
|
| 305 |
+
|
| 306 |
+
method = getattr(model, method_name)
|
| 307 |
+
return method(*args, **basic_kwargs)
|
| 308 |
+
except Exception as final_error:
|
| 309 |
+
raise Exception(f"DynamicCache workaround failed: {str(final_error)}")
|
| 310 |
+
else:
|
| 311 |
+
# Re-raise if it's not the DynamicCache error
|
| 312 |
+
raise e
|
| 313 |
+
except Exception as e:
|
| 314 |
+
# Handle other errors
|
| 315 |
+
raise e
|
| 316 |
+
|
| 317 |
@spaces.GPU()
|
| 318 |
def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None):
|
| 319 |
"""
|
|
|
|
| 345 |
else:
|
| 346 |
return "Error: Unsupported image format", None, None
|
| 347 |
|
| 348 |
+
# Use safe model calls with DynamicCache error handling
|
| 349 |
try:
|
| 350 |
if task == "Plain Text OCR":
|
| 351 |
+
res = safe_model_call_with_dynamic_cache_fix(model, 'chat', tokenizer, image_path, ocr_type='ocr')
|
| 352 |
return res, None, unique_id
|
| 353 |
else:
|
| 354 |
if task == "Format Text OCR":
|
| 355 |
+
res = safe_model_call_with_dynamic_cache_fix(model, 'chat', tokenizer, image_path, ocr_type='format', render=True, save_render_file=result_path)
|
| 356 |
elif task == "Fine-grained OCR (Box)":
|
| 357 |
+
res = safe_model_call_with_dynamic_cache_fix(model, 'chat', tokenizer, image_path, ocr_type=ocr_type, ocr_box=ocr_box, render=True, save_render_file=result_path)
|
| 358 |
elif task == "Fine-grained OCR (Color)":
|
| 359 |
+
res = safe_model_call_with_dynamic_cache_fix(model, 'chat', tokenizer, image_path, ocr_type=ocr_type, ocr_color=ocr_color, render=True, save_render_file=result_path)
|
| 360 |
elif task == "Multi-crop OCR":
|
| 361 |
+
res = safe_model_call_with_dynamic_cache_fix(model, 'chat_crop', tokenizer, image_path, ocr_type='format', render=True, save_render_file=result_path)
|
| 362 |
elif task == "Render Formatted OCR":
|
| 363 |
+
res = safe_model_call_with_dynamic_cache_fix(model, 'chat', tokenizer, image_path, ocr_type='format', render=True, save_render_file=result_path)
|
| 364 |
|
| 365 |
if os.path.exists(result_path):
|
| 366 |
with open(result_path, 'r') as f:
|
|
|
|
| 369 |
else:
|
| 370 |
return res, None, unique_id
|
| 371 |
except Exception as e:
|
| 372 |
+
# If safe call fails, try with cache manager as fallback
|
| 373 |
try:
|
| 374 |
if task == "Plain Text OCR":
|
| 375 |
res = cache_manager.safe_call('chat', tokenizer, image_path, ocr_type='ocr')
|
|
|
|
| 400 |
finally:
|
| 401 |
if os.path.exists(image_path):
|
| 402 |
os.remove(image_path)
|
| 403 |
+
|
| 404 |
def update_image_input(task):
|
| 405 |
if task == "Fine-grained OCR (Color)":
|
| 406 |
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|