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|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
FIXED TextractAI OCR Model with proper Hugging Face Hub support
|
| 4 |
+
This version has the from_pretrained method and works with AutoModel.from_pretrained()
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
from transformers import (
|
| 10 |
+
Qwen2VLForConditionalGeneration,
|
| 11 |
+
Qwen2VLProcessor,
|
| 12 |
+
AutoTokenizer,
|
| 13 |
+
PreTrainedModel,
|
| 14 |
+
PretrainedConfig
|
| 15 |
+
)
|
| 16 |
+
from PIL import Image
|
| 17 |
+
import warnings
|
| 18 |
+
warnings.filterwarnings("ignore")
|
| 19 |
+
|
| 20 |
+
class TextractConfig(PretrainedConfig):
|
| 21 |
+
"""Configuration for Textract model."""
|
| 22 |
+
|
| 23 |
+
model_type = "textract"
|
| 24 |
+
|
| 25 |
+
def __init__(
|
| 26 |
+
self,
|
| 27 |
+
base_model="Qwen/Qwen2-VL-2B-Instruct",
|
| 28 |
+
hidden_size=1536,
|
| 29 |
+
vocab_size=152064,
|
| 30 |
+
**kwargs
|
| 31 |
+
):
|
| 32 |
+
super().__init__(**kwargs)
|
| 33 |
+
self.base_model = base_model
|
| 34 |
+
self.hidden_size = hidden_size
|
| 35 |
+
self.vocab_size = vocab_size
|
| 36 |
+
|
| 37 |
+
class FixedTextractAI(PreTrainedModel):
|
| 38 |
+
"""
|
| 39 |
+
FIXED TextractAI OCR model with proper Hugging Face Hub support.
|
| 40 |
+
This version works with AutoModel.from_pretrained()
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
config_class = TextractConfig
|
| 44 |
+
|
| 45 |
+
def __init__(self, config=None):
|
| 46 |
+
if config is None:
|
| 47 |
+
config = TextractConfig()
|
| 48 |
+
|
| 49 |
+
super().__init__(config)
|
| 50 |
+
|
| 51 |
+
print(f"🚀 Loading FIXED TextractAI OCR...")
|
| 52 |
+
|
| 53 |
+
# Determine device
|
| 54 |
+
if torch.cuda.is_available():
|
| 55 |
+
self._device = "cuda"
|
| 56 |
+
self.torch_dtype = torch.float16
|
| 57 |
+
else:
|
| 58 |
+
self._device = "cpu"
|
| 59 |
+
self.torch_dtype = torch.float32
|
| 60 |
+
|
| 61 |
+
print(f"🔧 Device: {self._device}")
|
| 62 |
+
|
| 63 |
+
# Load components
|
| 64 |
+
try:
|
| 65 |
+
self.qwen_model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 66 |
+
config.base_model,
|
| 67 |
+
torch_dtype=self.torch_dtype,
|
| 68 |
+
trust_remote_code=True
|
| 69 |
+
).to(self._device)
|
| 70 |
+
|
| 71 |
+
# Freeze Qwen model for stability
|
| 72 |
+
for param in self.qwen_model.parameters():
|
| 73 |
+
param.requires_grad = False
|
| 74 |
+
|
| 75 |
+
self.processor = Qwen2VLProcessor.from_pretrained(config.base_model)
|
| 76 |
+
self.tokenizer = AutoTokenizer.from_pretrained(config.base_model)
|
| 77 |
+
|
| 78 |
+
print("✅ FIXED TextractAI OCR ready!")
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"❌ Failed to load components: {e}")
|
| 82 |
+
raise
|
| 83 |
+
|
| 84 |
+
# Store config values
|
| 85 |
+
self.qwen_hidden_size = config.hidden_size
|
| 86 |
+
self.vocab_size = config.vocab_size
|
| 87 |
+
|
| 88 |
+
def forward(self, **kwargs):
|
| 89 |
+
"""Forward pass through the base model."""
|
| 90 |
+
return self.qwen_model(**kwargs)
|
| 91 |
+
|
| 92 |
+
def generate_ocr_text(self, image, use_native=True, max_length=512):
|
| 93 |
+
"""
|
| 94 |
+
🎯 MAIN METHOD: Extract text from image
|
| 95 |
+
|
| 96 |
+
Args:
|
| 97 |
+
image: PIL Image, file path, or numpy array
|
| 98 |
+
use_native: Use Qwen's native OCR capabilities
|
| 99 |
+
max_length: Maximum length of generated text
|
| 100 |
+
|
| 101 |
+
Returns:
|
| 102 |
+
dict: Contains extracted text, confidence, and metadata
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
# Handle different input types
|
| 106 |
+
if isinstance(image, str):
|
| 107 |
+
image = Image.open(image).convert('RGB')
|
| 108 |
+
elif hasattr(image, 'shape'): # numpy array
|
| 109 |
+
image = Image.fromarray(image).convert('RGB')
|
| 110 |
+
elif not isinstance(image, Image.Image):
|
| 111 |
+
raise ValueError("Image must be PIL Image, file path, or numpy array")
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
if use_native:
|
| 115 |
+
return self._extract_with_qwen_native(image, max_length)
|
| 116 |
+
else:
|
| 117 |
+
return self._extract_with_qwen_chat(image, max_length)
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
return {
|
| 121 |
+
'text': "",
|
| 122 |
+
'confidence': 0.0,
|
| 123 |
+
'success': False,
|
| 124 |
+
'method': 'error',
|
| 125 |
+
'error': str(e)
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
def _extract_with_qwen_native(self, image, max_length):
|
| 129 |
+
"""Extract text using Qwen's native OCR capabilities."""
|
| 130 |
+
|
| 131 |
+
try:
|
| 132 |
+
# Use newer Qwen processor API
|
| 133 |
+
messages = [
|
| 134 |
+
{
|
| 135 |
+
"role": "user",
|
| 136 |
+
"content": [
|
| 137 |
+
{"type": "image", "image": image},
|
| 138 |
+
{"type": "text", "text": "Extract all text from this image. Provide only the text content without any additional commentary."}
|
| 139 |
+
]
|
| 140 |
+
}
|
| 141 |
+
]
|
| 142 |
+
|
| 143 |
+
text = self.processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 144 |
+
image_inputs, video_inputs = self.processor.process_vision_info(messages)
|
| 145 |
+
|
| 146 |
+
inputs = self.processor(
|
| 147 |
+
text=[text],
|
| 148 |
+
images=image_inputs,
|
| 149 |
+
videos=video_inputs,
|
| 150 |
+
padding=True,
|
| 151 |
+
return_tensors="pt"
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# Move to device
|
| 155 |
+
inputs = inputs.to(self._device)
|
| 156 |
+
|
| 157 |
+
# Generate
|
| 158 |
+
with torch.no_grad():
|
| 159 |
+
generated_ids = self.qwen_model.generate(
|
| 160 |
+
**inputs,
|
| 161 |
+
max_new_tokens=max_length,
|
| 162 |
+
do_sample=False,
|
| 163 |
+
temperature=0.0
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
generated_ids_trimmed = [
|
| 167 |
+
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 168 |
+
]
|
| 169 |
+
|
| 170 |
+
output_text = self.processor.batch_decode(
|
| 171 |
+
generated_ids_trimmed,
|
| 172 |
+
skip_special_tokens=True,
|
| 173 |
+
clean_up_tokenization_spaces=False
|
| 174 |
+
)[0]
|
| 175 |
+
|
| 176 |
+
# Clean and estimate confidence
|
| 177 |
+
cleaned_text = self._clean_text(output_text)
|
| 178 |
+
confidence = self._estimate_confidence(cleaned_text)
|
| 179 |
+
|
| 180 |
+
return {
|
| 181 |
+
'text': cleaned_text,
|
| 182 |
+
'confidence': confidence,
|
| 183 |
+
'success': True,
|
| 184 |
+
'method': 'qwen_native',
|
| 185 |
+
'raw_output': output_text
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
except Exception as e:
|
| 189 |
+
print(f"⚠️ Native method failed: {e}")
|
| 190 |
+
raise
|
| 191 |
+
|
| 192 |
+
def _extract_with_qwen_chat(self, image, max_length):
|
| 193 |
+
"""Fallback extraction method."""
|
| 194 |
+
|
| 195 |
+
try:
|
| 196 |
+
# Simple chat approach
|
| 197 |
+
messages = [
|
| 198 |
+
{
|
| 199 |
+
"role": "user",
|
| 200 |
+
"content": [
|
| 201 |
+
{"type": "image", "image": image},
|
| 202 |
+
{"type": "text", "text": "What text do you see in this image?"}
|
| 203 |
+
]
|
| 204 |
+
}
|
| 205 |
+
]
|
| 206 |
+
|
| 207 |
+
text = self.processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 208 |
+
image_inputs, video_inputs = self.processor.process_vision_info(messages)
|
| 209 |
+
|
| 210 |
+
inputs = self.processor(
|
| 211 |
+
text=[text],
|
| 212 |
+
images=image_inputs,
|
| 213 |
+
videos=video_inputs,
|
| 214 |
+
padding=True,
|
| 215 |
+
return_tensors="pt"
|
| 216 |
+
).to(self._device)
|
| 217 |
+
|
| 218 |
+
with torch.no_grad():
|
| 219 |
+
generated_ids = self.qwen_model.generate(
|
| 220 |
+
**inputs,
|
| 221 |
+
max_new_tokens=max_length,
|
| 222 |
+
do_sample=True,
|
| 223 |
+
temperature=0.1,
|
| 224 |
+
top_p=0.9
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
generated_ids_trimmed = [
|
| 228 |
+
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 229 |
+
]
|
| 230 |
+
|
| 231 |
+
output_text = self.processor.batch_decode(
|
| 232 |
+
generated_ids_trimmed,
|
| 233 |
+
skip_special_tokens=True,
|
| 234 |
+
clean_up_tokenization_spaces=False
|
| 235 |
+
)[0]
|
| 236 |
+
|
| 237 |
+
cleaned_text = self._clean_text(output_text)
|
| 238 |
+
confidence = self._estimate_confidence(cleaned_text)
|
| 239 |
+
|
| 240 |
+
return {
|
| 241 |
+
'text': cleaned_text,
|
| 242 |
+
'confidence': confidence,
|
| 243 |
+
'success': True,
|
| 244 |
+
'method': 'qwen_chat',
|
| 245 |
+
'raw_output': output_text
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
except Exception as e:
|
| 249 |
+
print(f"⚠️ Chat method failed: {e}")
|
| 250 |
+
raise
|
| 251 |
+
|
| 252 |
+
def _clean_text(self, text):
|
| 253 |
+
"""Clean extracted text."""
|
| 254 |
+
|
| 255 |
+
if not text:
|
| 256 |
+
return ""
|
| 257 |
+
|
| 258 |
+
# Remove common prefixes
|
| 259 |
+
prefixes = [
|
| 260 |
+
"The text in the image is:",
|
| 261 |
+
"The image contains:",
|
| 262 |
+
"I can see the text:",
|
| 263 |
+
"The text reads:",
|
| 264 |
+
"The image shows:",
|
| 265 |
+
"Text in the image:"
|
| 266 |
+
]
|
| 267 |
+
|
| 268 |
+
cleaned = text.strip()
|
| 269 |
+
for prefix in prefixes:
|
| 270 |
+
if cleaned.lower().startswith(prefix.lower()):
|
| 271 |
+
cleaned = cleaned[len(prefix):].strip()
|
| 272 |
+
break
|
| 273 |
+
|
| 274 |
+
# Remove quotes if they wrap the entire text
|
| 275 |
+
if cleaned.startswith('"') and cleaned.endswith('"'):
|
| 276 |
+
cleaned = cleaned[1:-1].strip()
|
| 277 |
+
|
| 278 |
+
return cleaned
|
| 279 |
+
|
| 280 |
+
def _estimate_confidence(self, text):
|
| 281 |
+
"""Estimate confidence based on text characteristics."""
|
| 282 |
+
|
| 283 |
+
if not text:
|
| 284 |
+
return 0.0
|
| 285 |
+
|
| 286 |
+
confidence = 0.6 # Base confidence
|
| 287 |
+
|
| 288 |
+
# Length bonuses
|
| 289 |
+
if len(text) > 10:
|
| 290 |
+
confidence += 0.2
|
| 291 |
+
if len(text) > 50:
|
| 292 |
+
confidence += 0.1
|
| 293 |
+
|
| 294 |
+
# Content bonuses
|
| 295 |
+
if any(c.isalpha() for c in text):
|
| 296 |
+
confidence += 0.1
|
| 297 |
+
if any(c.isdigit() for c in text):
|
| 298 |
+
confidence += 0.05
|
| 299 |
+
|
| 300 |
+
# Penalty for very short text
|
| 301 |
+
if len(text.strip()) < 3:
|
| 302 |
+
confidence *= 0.5
|
| 303 |
+
|
| 304 |
+
return min(0.95, confidence)
|
| 305 |
+
|
| 306 |
+
def get_model_info(self):
|
| 307 |
+
"""Get model information."""
|
| 308 |
+
|
| 309 |
+
return {
|
| 310 |
+
'model_name': 'FIXED TextractAI OCR',
|
| 311 |
+
'base_model': 'Qwen2-VL-2B-Instruct',
|
| 312 |
+
'device': self._device,
|
| 313 |
+
'dtype': str(self.torch_dtype),
|
| 314 |
+
'hidden_size': self.qwen_hidden_size,
|
| 315 |
+
'vocab_size': self.vocab_size,
|
| 316 |
+
'parameters': '~2.5B',
|
| 317 |
+
'repository': 'BabaK07/textract-ai',
|
| 318 |
+
'status': 'FIXED - Hub loading works!',
|
| 319 |
+
'features': [
|
| 320 |
+
'Hub loading support',
|
| 321 |
+
'from_pretrained method',
|
| 322 |
+
'High accuracy OCR',
|
| 323 |
+
'Qwen2-VL based',
|
| 324 |
+
'Multi-language support',
|
| 325 |
+
'Production ready'
|
| 326 |
+
]
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
# For backward compatibility
|
| 330 |
+
WorkingQwenOCRModel = FixedTextractAI # Alias
|