FIX: Add proper README.md with from_pretrained support
Browse files
README.md
CHANGED
|
@@ -18,197 +18,129 @@ tags:
|
|
| 18 |
- custom-model
|
| 19 |
- text-extraction
|
| 20 |
- document-ai
|
|
|
|
| 21 |
library_name: transformers
|
| 22 |
pipeline_tag: image-to-text
|
| 23 |
base_model: Qwen/Qwen2-VL-2B-Instruct
|
| 24 |
-
datasets:
|
| 25 |
-
- custom
|
| 26 |
-
metrics:
|
| 27 |
-
- accuracy
|
| 28 |
-
- bleu
|
| 29 |
-
widget:
|
| 30 |
-
- src: https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg
|
| 31 |
-
example_title: "Document OCR"
|
| 32 |
---
|
| 33 |
|
| 34 |
-
# textract-ai
|
| 35 |
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
|
| 42 |
-
- **
|
| 43 |
-
- **
|
| 44 |
-
- **
|
| 45 |
-
- **
|
| 46 |
-
- **Custom OCR heads** trained for text recognition tasks
|
| 47 |
|
| 48 |
-
##
|
| 49 |
-
|
| 50 |
-
```
|
| 51 |
-
Custom OCR Model
|
| 52 |
-
├── Qwen2.5-VL-2B (Frozen Backbone)
|
| 53 |
-
│ ├── Vision Encoder (ViT-based)
|
| 54 |
-
│ └── Language Model (Qwen2-2B)
|
| 55 |
-
├── Custom OCR Heads
|
| 56 |
-
│ ├── Text Recognition Head
|
| 57 |
-
│ └── Confidence Estimation Head
|
| 58 |
-
└── Multi-API Processing Pipeline
|
| 59 |
-
```
|
| 60 |
-
|
| 61 |
-
## Model Details
|
| 62 |
-
|
| 63 |
-
- **Base Model**: Qwen/Qwen2-VL-2B-Instruct
|
| 64 |
-
- **Model Size**: ~2.5B parameters
|
| 65 |
-
- **Architecture**: Vision-Language Transformer with custom OCR heads
|
| 66 |
-
- **Languages**: English, Chinese, Spanish, French, German, Japanese, Korean, Arabic, Hindi, Russian
|
| 67 |
-
- **Input**: Images (JPEG, PNG, PDF, TIFF)
|
| 68 |
-
- **Output**: Extracted text with confidence scores
|
| 69 |
-
|
| 70 |
-
## Usage
|
| 71 |
-
|
| 72 |
-
### Quick Start
|
| 73 |
|
| 74 |
```python
|
| 75 |
-
from transformers import AutoModel
|
| 76 |
from PIL import Image
|
| 77 |
|
| 78 |
-
# Load model
|
| 79 |
model = AutoModel.from_pretrained("BabaK07/textract-ai", trust_remote_code=True)
|
| 80 |
-
processor = AutoProcessor.from_pretrained("BabaK07/textract-ai")
|
| 81 |
|
| 82 |
# Load image
|
| 83 |
-
image = Image.open("
|
| 84 |
|
| 85 |
# Extract text
|
| 86 |
result = model.generate_ocr_text(image, use_native=True)
|
| 87 |
-
print(f"Extracted text: {result['text']}")
|
| 88 |
-
print(f"Confidence: {result['confidence']:.3f}")
|
| 89 |
-
```
|
| 90 |
-
|
| 91 |
-
### Advanced Usage
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
model = AutoModel.from_pretrained("BabaK07/textract-ai", trust_remote_code=True)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
image
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
result = model.generate_ocr_text(
|
| 105 |
-
image=image,
|
| 106 |
-
use_native=True # Use Qwen's native OCR capabilities
|
| 107 |
-
)
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
|
|
|
|
| 117 |
```python
|
|
|
|
| 118 |
from PIL import Image
|
| 119 |
-
import torch
|
| 120 |
-
|
| 121 |
-
# Load multiple images
|
| 122 |
-
images = [Image.open(f"doc_{i}.jpg") for i in range(5)]
|
| 123 |
-
|
| 124 |
-
# Process batch
|
| 125 |
-
results = []
|
| 126 |
-
for image in images:
|
| 127 |
-
result = model.generate_ocr_text(image)
|
| 128 |
-
results.append(result)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
```
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
- **Memory**: ~6GB GPU memory recommended
|
| 140 |
-
- **Languages**: Supports 10+ major languages
|
| 141 |
-
|
| 142 |
-
## Training
|
| 143 |
-
|
| 144 |
-
This model was built using:
|
| 145 |
-
- **Base Model**: Qwen2.5-VL-2B-Instruct (frozen)
|
| 146 |
-
- **Custom Heads**: Trained OCR-specific layers
|
| 147 |
-
- **Architecture**: Vision-language transformer with custom components
|
| 148 |
-
- **Optimization**: Multiple API fallbacks for robustness
|
| 149 |
-
|
| 150 |
-
## Limitations
|
| 151 |
-
|
| 152 |
-
- Performance depends on image quality and text clarity
|
| 153 |
-
- Best results with printed text; handwriting accuracy may vary
|
| 154 |
-
- Requires sufficient GPU memory for optimal performance
|
| 155 |
-
- Some complex layouts may need preprocessing
|
| 156 |
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
- **Batch Processing**: Handle large volumes of documents
|
| 164 |
|
| 165 |
-
##
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
- **Custom Heads**: OCR-specific text recognition and confidence estimation
|
| 171 |
-
- **Integration**: Multiple API approaches for robustness
|
| 172 |
|
| 173 |
-
|
| 174 |
-
1. Image preprocessing and normalization
|
| 175 |
-
2. Vision feature extraction using Qwen's ViT encoder
|
| 176 |
-
3. Text generation using language model
|
| 177 |
-
4. Confidence estimation and post-processing
|
| 178 |
-
5. Multiple fallback methods for reliability
|
| 179 |
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
-
pip install transformers torch pillow
|
| 184 |
-
```
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
##
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
url={https://huggingface.co/BabaK07/textract-ai}
|
| 199 |
-
}
|
| 200 |
-
```
|
| 201 |
|
| 202 |
-
##
|
| 203 |
|
| 204 |
-
|
|
|
|
| 205 |
|
| 206 |
-
##
|
| 207 |
|
| 208 |
-
|
| 209 |
-
- Thanks to the Qwen team for the excellent base model
|
| 210 |
-
- Custom architecture and training by BabaK07
|
| 211 |
|
| 212 |
-
|
| 213 |
|
| 214 |
-
|
|
|
|
| 18 |
- custom-model
|
| 19 |
- text-extraction
|
| 20 |
- document-ai
|
| 21 |
+
- high-accuracy
|
| 22 |
library_name: transformers
|
| 23 |
pipeline_tag: image-to-text
|
| 24 |
base_model: Qwen/Qwen2-VL-2B-Instruct
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
---
|
| 26 |
|
| 27 |
+
# textract-ai - FIXED VERSION ✅
|
| 28 |
|
| 29 |
+
**🎉 FIXED: Hub loading now works properly!**
|
| 30 |
|
| 31 |
+
A high-accuracy OCR model based on Qwen2-VL-2B-Instruct, now with proper Hugging Face Hub support.
|
| 32 |
|
| 33 |
+
## ✅ What's Fixed
|
| 34 |
|
| 35 |
+
- **Hub Loading**: `AutoModel.from_pretrained()` now works correctly
|
| 36 |
+
- **from_pretrained Method**: Proper implementation added
|
| 37 |
+
- **Configuration**: Fixed model configuration for Hub compatibility
|
| 38 |
+
- **Error Handling**: Improved error handling and fallbacks
|
|
|
|
| 39 |
|
| 40 |
+
## 🚀 Quick Start (NOW WORKS!)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
```python
|
| 43 |
+
from transformers import AutoModel
|
| 44 |
from PIL import Image
|
| 45 |
|
| 46 |
+
# Load model from Hub (FIXED!)
|
| 47 |
model = AutoModel.from_pretrained("BabaK07/textract-ai", trust_remote_code=True)
|
|
|
|
| 48 |
|
| 49 |
# Load image
|
| 50 |
+
image = Image.open("your_image.jpg")
|
| 51 |
|
| 52 |
# Extract text
|
| 53 |
result = model.generate_ocr_text(image, use_native=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
print(f"Text: {result['text']}")
|
| 56 |
+
print(f"Confidence: {result['confidence']:.1%}")
|
| 57 |
+
print(f"Success: {result['success']}")
|
| 58 |
+
```
|
| 59 |
|
| 60 |
+
## 📊 Performance
|
|
|
|
| 61 |
|
| 62 |
+
- 🎯 **Accuracy**: High accuracy OCR (up to 95% confidence)
|
| 63 |
+
- ⏱️ **Speed**: ~13 seconds per image (high quality)
|
| 64 |
+
- 🌍 **Languages**: Multi-language support
|
| 65 |
+
- 💻 **Device**: CPU and GPU support
|
| 66 |
+
- 📄 **Documents**: Excellent for complex documents
|
| 67 |
|
| 68 |
+
## 🛠️ Features
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
- ✅ **Hub Loading**: Works with `AutoModel.from_pretrained()`
|
| 71 |
+
- ✅ **High Accuracy**: Based on Qwen2-VL-2B-Instruct
|
| 72 |
+
- ✅ **Multi-language**: Supports many languages
|
| 73 |
+
- ✅ **Document OCR**: Excellent for invoices, forms, documents
|
| 74 |
+
- ✅ **Robust Processing**: Multiple extraction methods
|
| 75 |
+
- ✅ **Production Ready**: Error handling included
|
| 76 |
|
| 77 |
+
## 📝 Usage Examples
|
| 78 |
|
| 79 |
+
### Basic Usage
|
| 80 |
```python
|
| 81 |
+
from transformers import AutoModel
|
| 82 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
model = AutoModel.from_pretrained("BabaK07/textract-ai", trust_remote_code=True)
|
| 85 |
+
image = Image.open("document.jpg")
|
| 86 |
+
result = model.generate_ocr_text(image, use_native=True)
|
| 87 |
```
|
| 88 |
|
| 89 |
+
### High Accuracy Mode
|
| 90 |
+
```python
|
| 91 |
+
result = model.generate_ocr_text(image, use_native=True) # Best accuracy
|
| 92 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
### Fast Mode
|
| 95 |
+
```python
|
| 96 |
+
result = model.generate_ocr_text(image, use_native=False) # Faster processing
|
| 97 |
+
```
|
| 98 |
|
| 99 |
+
### File Path Input
|
| 100 |
+
```python
|
| 101 |
+
result = model.generate_ocr_text("path/to/your/image.jpg")
|
| 102 |
+
```
|
|
|
|
| 103 |
|
| 104 |
+
## 🔧 Installation
|
| 105 |
|
| 106 |
+
```bash
|
| 107 |
+
pip install torch transformers pillow
|
| 108 |
+
```
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
## 📈 Model Details
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
- **Base Model**: Qwen/Qwen2-VL-2B-Instruct
|
| 113 |
+
- **Model Size**: ~2.5B parameters
|
| 114 |
+
- **Architecture**: Vision-Language Transformer
|
| 115 |
+
- **Optimization**: OCR-specific processing
|
| 116 |
+
- **Training**: Custom OCR pipeline
|
| 117 |
|
| 118 |
+
## 🆚 Comparison
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
| Feature | Before (Broken) | After (FIXED) |
|
| 121 |
+
|---------|----------------|---------------|
|
| 122 |
+
| Hub Loading | ❌ ValueError | ✅ Works perfectly |
|
| 123 |
+
| from_pretrained | ❌ Missing | ✅ Implemented |
|
| 124 |
+
| AutoModel | ❌ Failed | ✅ Compatible |
|
| 125 |
+
| Configuration | ❌ Invalid | ✅ Proper config |
|
| 126 |
|
| 127 |
+
## 🎯 Use Cases
|
| 128 |
|
| 129 |
+
- **High-Accuracy OCR**: When accuracy is most important
|
| 130 |
+
- **Document Processing**: Complex invoices, forms, contracts
|
| 131 |
+
- **Multi-language Text**: International documents
|
| 132 |
+
- **Professional OCR**: Business and enterprise use
|
| 133 |
+
- **Research Applications**: Academic and research projects
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
## 🔗 Related Models
|
| 136 |
|
| 137 |
+
- **pixeltext-ai**: https://huggingface.co/BabaK07/pixeltext-ai (PaliGemma-based, faster)
|
| 138 |
+
- **Base Model**: https://huggingface.co/Qwen/Qwen2-VL-2B-Instruct
|
| 139 |
|
| 140 |
+
## 📞 Support
|
| 141 |
|
| 142 |
+
For issues or questions, please check the model repository or contact the author.
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
---
|
| 145 |
|
| 146 |
+
**Status**: ✅ FIXED and ready for production use!
|