AnseMin commited on
Commit
e045d86
·
1 Parent(s): dbc3034

Error: Error processing document with GOT-OCR: GotOcrParser._load_model..patched_chat() takes 3 positional arguments but 4 were given

Browse files
Files changed (1) hide show
  1. src/parsers/got_ocr_parser.py +12 -19
src/parsers/got_ocr_parser.py CHANGED
@@ -140,32 +140,25 @@ class GotOcrParser(DocumentParser):
140
  logger.warning(f"Could not inspect original chat method: {e}")
141
 
142
  # Define a completely new patched chat method that avoids parameter conflicts
143
- def patched_chat(self, tokenizer, image_path, **kwargs):
144
  """A patched version of chat method that forces float16 precision"""
145
- ocr_type_val = kwargs.get('ocr_type', 'ocr') # Default to 'ocr' if not specified
146
-
147
- # Remove ocr_type from kwargs to avoid duplication
148
- kwargs_copy = kwargs.copy()
149
- if 'ocr_type' in kwargs_copy:
150
- del kwargs_copy['ocr_type']
151
-
152
- logger.info(f"Using patched chat method with float16, ocr_type={ocr_type_val}")
153
 
154
  # Set explicit autocast dtype
155
  if hasattr(torch.amp, 'autocast'):
156
  with torch.amp.autocast(device_type='cuda', dtype=torch.float16):
157
  try:
158
- # Always use ocr_type as third positional argument per original signature
159
- return original_chat(self, tokenizer, image_path, ocr_type_val, **kwargs_copy)
160
  except TypeError as e:
161
  logger.warning(f"First call approach failed: {e}, trying alternative approach")
162
  try:
163
  # Try passing image_path as string in case that's the issue
164
- return original_chat(self, tokenizer, str(image_path), ocr_type_val, **kwargs_copy)
165
  except Exception as e2:
166
  logger.warning(f"Second call approach also failed: {e2}")
167
- # Fall back to just passing the original arguments unchanged
168
- return original_chat(self, tokenizer, image_path, **kwargs)
169
  except RuntimeError as e:
170
  if "bfloat16" in str(e):
171
  logger.error(f"BFloat16 error encountered despite patching: {e}")
@@ -175,16 +168,16 @@ class GotOcrParser(DocumentParser):
175
  else:
176
  # Same approach without autocast
177
  try:
178
- # Always use ocr_type as third positional argument
179
- return original_chat(self, tokenizer, image_path, ocr_type_val, **kwargs_copy)
180
  except TypeError as e:
181
  logger.warning(f"Call without autocast failed: {e}, trying alternative approach")
182
  try:
183
  # Try passing image_path as string in case that's the issue
184
- return original_chat(self, tokenizer, str(image_path), ocr_type_val, **kwargs_copy)
185
  except:
186
- # Fall back to just passing the original arguments unchanged
187
- return original_chat(self, tokenizer, image_path, **kwargs)
188
 
189
  # Apply the patch
190
  import types
 
140
  logger.warning(f"Could not inspect original chat method: {e}")
141
 
142
  # Define a completely new patched chat method that avoids parameter conflicts
143
+ def patched_chat(self, tokenizer, image_path, ocr_type, **kwargs):
144
  """A patched version of chat method that forces float16 precision"""
145
+ logger.info(f"Using patched chat method with float16, ocr_type={ocr_type}")
 
 
 
 
 
 
 
146
 
147
  # Set explicit autocast dtype
148
  if hasattr(torch.amp, 'autocast'):
149
  with torch.amp.autocast(device_type='cuda', dtype=torch.float16):
150
  try:
151
+ # Pass all arguments directly to maintain signature
152
+ return original_chat(self, tokenizer, image_path, ocr_type, **kwargs)
153
  except TypeError as e:
154
  logger.warning(f"First call approach failed: {e}, trying alternative approach")
155
  try:
156
  # Try passing image_path as string in case that's the issue
157
+ return original_chat(self, tokenizer, str(image_path), ocr_type, **kwargs)
158
  except Exception as e2:
159
  logger.warning(f"Second call approach also failed: {e2}")
160
+ # Fall back to original method
161
+ return original_chat(self, tokenizer, image_path, ocr_type, **kwargs)
162
  except RuntimeError as e:
163
  if "bfloat16" in str(e):
164
  logger.error(f"BFloat16 error encountered despite patching: {e}")
 
168
  else:
169
  # Same approach without autocast
170
  try:
171
+ # Direct call with all positional args
172
+ return original_chat(self, tokenizer, image_path, ocr_type, **kwargs)
173
  except TypeError as e:
174
  logger.warning(f"Call without autocast failed: {e}, trying alternative approach")
175
  try:
176
  # Try passing image_path as string in case that's the issue
177
+ return original_chat(self, tokenizer, str(image_path), ocr_type, **kwargs)
178
  except:
179
+ # Fall back to original method
180
+ return original_chat(self, tokenizer, image_path, ocr_type, **kwargs)
181
 
182
  # Apply the patch
183
  import types