Spaces:
Running
Running
Commit
Β·
67e2508
1
Parent(s):
796f321
π§ Fix Gradio API name conflicts and upgrade version
Browse filesβ
Fixes:
- Add unique api_name to avoid duplicate function warnings
- Upgrade Gradio from 4.0.0 to 4.44.0+ for latest features
- Separate API endpoints for upload vs click events
π Deployment:
- Eliminates warning: 'api_name extract_text_from_image already exists'
- Uses latest Gradio version with bug fixes and improvements
- Maintains both auto-upload and manual extract functionality
- README.md +363 -176
- requirements.txt +1 -1
- ui/interface.py +4 -2
README.md
CHANGED
@@ -12,200 +12,215 @@ license: mit
|
|
12 |
|
13 |
# π TextLens - AI-Powered OCR
|
14 |
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
## ποΈ Architecture
|
30 |
|
31 |
```
|
32 |
textlens-ocr/
|
33 |
-
βββ
|
34 |
-
βββ
|
35 |
-
βββ
|
36 |
-
|
37 |
-
|
38 |
-
β βββ ocr_processor.py #
|
39 |
-
βββ
|
40 |
-
β
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
47 |
```
|
48 |
|
49 |
## π Quick Start
|
50 |
|
51 |
-
###
|
52 |
|
53 |
-
|
|
|
54 |
|
55 |
-
|
56 |
-
git clone https://github.com/KumarAmrit30/textlens-ocr.git
|
57 |
-
cd textlens-ocr
|
58 |
-
```
|
59 |
|
60 |
-
|
61 |
|
62 |
```bash
|
63 |
-
|
64 |
-
|
65 |
```
|
66 |
|
67 |
-
|
68 |
|
69 |
```bash
|
|
|
|
|
70 |
pip install -r requirements.txt
|
71 |
```
|
72 |
|
73 |
-
|
74 |
-
|
75 |
```bash
|
76 |
python app.py
|
77 |
```
|
|
|
78 |
|
79 |
-
|
80 |
-
Navigate to `http://localhost:7860`
|
81 |
-
|
82 |
-
### Quick Test
|
83 |
-
|
84 |
-
Run the test suite to verify everything works:
|
85 |
|
86 |
```bash
|
87 |
-
|
|
|
88 |
```
|
89 |
|
90 |
-
##
|
91 |
-
|
92 |
-
### OCR Processing Pipeline
|
93 |
|
94 |
-
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
|
97 |
-
- Supports both basic OCR and region-based extraction
|
98 |
-
- GPU accelerated inference
|
99 |
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
-
|
103 |
-
- Works when Florence-2 fails to load
|
104 |
-
- Multi-language support
|
105 |
|
106 |
-
|
107 |
-
- Demonstration functionality
|
108 |
-
- Shows interface working correctly
|
109 |
-
- Used when other methods are unavailable
|
110 |
|
111 |
-
|
|
|
112 |
|
113 |
-
|
|
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
# Try Florence-2 with specific revision
|
118 |
-
model = AutoModelForCausalLM.from_pretrained(
|
119 |
-
"microsoft/Florence-2-base",
|
120 |
-
revision='refs/pr/6',
|
121 |
-
trust_remote_code=True
|
122 |
-
)
|
123 |
-
except:
|
124 |
-
# Fall back to default Florence-2
|
125 |
-
model = AutoModelForCausalLM.from_pretrained(
|
126 |
-
"microsoft/Florence-2-base",
|
127 |
-
trust_remote_code=True
|
128 |
-
)
|
129 |
```
|
130 |
|
131 |
-
###
|
132 |
|
133 |
-
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
138 |
|
139 |
-
|
|
|
|
|
140 |
|
141 |
-
|
142 |
-
| ---------------- | ------ | ------ | --------- | --------------------- |
|
143 |
-
| Florence-2-base | 230M | Fast | High | General OCR |
|
144 |
-
| Florence-2-large | 770M | Medium | Very High | High accuracy needs |
|
145 |
-
| EasyOCR | ~100MB | Medium | Good | Fallback/Multilingual |
|
146 |
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
-
|
150 |
-
- **PNG** (.png)
|
151 |
-
- **WebP** (.webp)
|
152 |
-
- **BMP** (.bmp)
|
153 |
-
- **TIFF** (.tiff, .tif)
|
154 |
-
- **GIF** (.gif)
|
155 |
|
156 |
-
|
157 |
|
158 |
-
|
159 |
-
- **πͺ Receipt Processing**: Extract data from receipts and invoices
|
160 |
-
- **π± Screenshot Text Extraction**: Get text from app screenshots
|
161 |
-
- **π License Plate Reading**: Extract text from vehicle plates
|
162 |
-
- **π Book/Article Scanning**: Digitize printed materials
|
163 |
-
- **π Multilingual Text**: Process text in various languages
|
164 |
|
165 |
-
|
|
|
|
|
|
|
166 |
|
167 |
-
|
168 |
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
-
|
172 |
-
# For faster inference
|
173 |
-
ocr = OCRProcessor(model_name="microsoft/Florence-2-base")
|
174 |
|
175 |
-
|
176 |
-
ocr = OCRProcessor(model_name="microsoft/Florence-2-large")
|
177 |
-
```
|
178 |
-
|
179 |
-
### UI Customization
|
180 |
|
181 |
-
|
182 |
|
183 |
-
-
|
184 |
-
-
|
185 |
-
-
|
|
|
186 |
|
187 |
-
|
188 |
|
189 |
-
|
|
|
190 |
|
191 |
-
|
192 |
-
# Run all tests
|
193 |
-
python test_ocr.py
|
194 |
|
195 |
-
|
196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
```
|
198 |
|
199 |
-
|
200 |
-
|
201 |
-
### HuggingFace Spaces
|
202 |
-
|
203 |
-
1. Fork this repository
|
204 |
-
2. Create a new Space on HuggingFace
|
205 |
-
3. Connect your repository
|
206 |
-
4. The app will automatically deploy
|
207 |
-
|
208 |
-
### Docker Deployment
|
209 |
|
210 |
```dockerfile
|
211 |
FROM python:3.9-slim
|
@@ -220,92 +235,264 @@ EXPOSE 7860
|
|
220 |
CMD ["python", "app.py"]
|
221 |
```
|
222 |
|
223 |
-
|
224 |
|
225 |
```bash
|
226 |
-
|
227 |
-
|
228 |
-
gunicorn -w 4 -b 0.0.0.0:7860 app:create_interface().app
|
229 |
```
|
230 |
|
231 |
-
|
232 |
|
233 |
-
|
|
234 |
-
| ---------------------- |
|
235 |
-
|
|
236 |
-
|
|
237 |
-
|
|
|
|
238 |
|
239 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
|
|
|
|
|
|
|
|
246 |
|
247 |
-
##
|
248 |
|
249 |
### OCRProcessor Class
|
250 |
|
251 |
```python
|
252 |
from models.ocr_processor import OCRProcessor
|
253 |
|
254 |
-
# Initialize
|
255 |
-
ocr = OCRProcessor(
|
|
|
|
|
|
|
|
|
256 |
|
257 |
-
# Extract text
|
258 |
text = ocr.extract_text(image)
|
|
|
259 |
|
260 |
-
# Extract with
|
261 |
result = ocr.extract_text_with_regions(image)
|
|
|
262 |
|
263 |
-
# Get model
|
264 |
info = ocr.get_model_info()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
```
|
266 |
|
267 |
-
##
|
268 |
|
269 |
### Common Issues
|
270 |
|
271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
|
273 |
```bash
|
274 |
-
|
275 |
-
|
276 |
```
|
277 |
|
278 |
-
2. **
|
279 |
|
280 |
-
```
|
281 |
-
|
282 |
-
ocr = OCRProcessor()
|
283 |
-
ocr.device = "cpu"
|
284 |
```
|
285 |
|
286 |
-
3. **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
287 |
```bash
|
288 |
-
|
289 |
-
|
290 |
```
|
291 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
## π License
|
293 |
|
294 |
-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
|
|
|
295 |
|
296 |
-
|
|
|
|
|
|
|
297 |
|
298 |
-
|
299 |
-
- **HuggingFace** for the transformers library
|
300 |
-
- **Gradio** for the web interface framework
|
301 |
-
- **EasyOCR** for fallback OCR capabilities
|
302 |
|
303 |
-
|
304 |
|
305 |
-
-
|
306 |
-
-
|
307 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
308 |
|
309 |
---
|
310 |
|
|
|
|
|
311 |
**Made with β€οΈ for the AI community**
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# π TextLens - AI-Powered OCR
|
14 |
|
15 |
+
[](https://huggingface.co/spaces/GoConqurer/textlens-ocr)
|
16 |
+
[](https://github.com/KumarAmrit30/textlens-ocr)
|
17 |
+
[](LICENSE)
|
18 |
+
[](https://www.python.org/downloads/)
|
19 |
|
20 |
+
A state-of-the-art Vision-Language Model (VLM) based OCR application that extracts text from images using Microsoft Florence-2 with intelligent fallback systems and enterprise-grade zero downtime deployment.
|
21 |
|
22 |
+
## π Live Demo
|
23 |
+
|
24 |
+
**π Try it now:** [https://huggingface.co/spaces/GoConqurer/textlens-ocr](https://huggingface.co/spaces/GoConqurer/textlens-ocr)
|
25 |
+
|
26 |
+

|
27 |
+
|
28 |
+
## β¨ Key Features
|
29 |
+
|
30 |
+
### π€ Advanced AI-Powered OCR
|
31 |
+
|
32 |
+
- **Microsoft Florence-2 VLM**: State-of-the-art vision-language model for text extraction
|
33 |
+
- **Intelligent Fallback System**: Automatic fallback to EasyOCR if primary model fails
|
34 |
+
- **Multi-Model Support**: Florence-2-base and Florence-2-large variants
|
35 |
+
- **Real-time Processing**: Instant text extraction on image upload
|
36 |
+
|
37 |
+
### π¨ Modern User Experience
|
38 |
+
|
39 |
+
- **Clean UI**: Professional Gradio interface with intuitive design
|
40 |
+
- **Multiple Input Methods**: Upload files, use webcam, or paste from clipboard
|
41 |
+
- **Copy-to-Clipboard**: One-click text copying functionality
|
42 |
+
- **Responsive Design**: Works seamlessly on desktop and mobile devices
|
43 |
+
- **Dark/Light Theme**: Automatic theme adaptation
|
44 |
+
|
45 |
+
### β‘ Performance & Reliability
|
46 |
+
|
47 |
+
- **GPU Acceleration**: Supports CUDA, MPS (Apple Silicon), and CPU inference
|
48 |
+
- **Smart Device Detection**: Automatically uses best available hardware
|
49 |
+
- **Error Resilience**: Robust error handling with graceful degradation
|
50 |
+
- **Memory Optimization**: Efficient model loading and cleanup
|
51 |
+
|
52 |
+
### π‘οΈ Enterprise Features
|
53 |
+
|
54 |
+
- **Zero Downtime Deployment**: Blue-green deployment with health checks
|
55 |
+
- **Health Monitoring**: Built-in `/health` and `/ready` endpoints
|
56 |
+
- **Graceful Shutdown**: Signal handling for clean application restarts
|
57 |
+
- **Production Ready**: Scalable architecture with automated deployment
|
58 |
|
59 |
## ποΈ Architecture
|
60 |
|
61 |
```
|
62 |
textlens-ocr/
|
63 |
+
βββ π± Frontend (Gradio UI)
|
64 |
+
β βββ ui/interface.py # Main interface components
|
65 |
+
β βββ ui/handlers.py # Event handlers & logic
|
66 |
+
β βββ ui/styles.py # CSS styling & themes
|
67 |
+
βββ π§ AI Models
|
68 |
+
β βββ models/ocr_processor.py # OCR engine with fallbacks
|
69 |
+
βββ π§ Utilities
|
70 |
+
β βββ utils/image_utils.py # Image preprocessing
|
71 |
+
βββ π Deployment
|
72 |
+
β βββ .github/workflows/ # CI/CD pipelines
|
73 |
+
β βββ scripts/deploy.py # Manual deployment tools
|
74 |
+
β βββ deployment.config.yml # Deployment configuration
|
75 |
+
βββ π Documentation
|
76 |
+
β βββ README.md # Main documentation
|
77 |
+
β βββ DEPLOYMENT.md # Deployment guide
|
78 |
+
βββ βοΈ Configuration
|
79 |
+
βββ app.py # Main application entry
|
80 |
+
βββ requirements.txt # Dependencies
|
81 |
```
|
82 |
|
83 |
## π Quick Start
|
84 |
|
85 |
+
### π Online (Recommended)
|
86 |
|
87 |
+
**Instant access** - No installation required:
|
88 |
+
π [**Launch TextLens**](https://huggingface.co/spaces/GoConqurer/textlens-ocr)
|
89 |
|
90 |
+
### π» Local Development
|
|
|
|
|
|
|
91 |
|
92 |
+
1. **Clone Repository**
|
93 |
|
94 |
```bash
|
95 |
+
git clone https://github.com/KumarAmrit30/textlens-ocr.git
|
96 |
+
cd textlens-ocr
|
97 |
```
|
98 |
|
99 |
+
2. **Setup Environment**
|
100 |
|
101 |
```bash
|
102 |
+
python -m venv textlens_env
|
103 |
+
source textlens_env/bin/activate # Windows: textlens_env\Scripts\activate
|
104 |
pip install -r requirements.txt
|
105 |
```
|
106 |
|
107 |
+
3. **Launch Application**
|
|
|
108 |
```bash
|
109 |
python app.py
|
110 |
```
|
111 |
+
π Open: `http://localhost:7860`
|
112 |
|
113 |
+
### π§ͺ Quick Test
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
```bash
|
116 |
+
# Verify installation
|
117 |
+
python -c "from models.ocr_processor import OCRProcessor; print('β
TextLens ready!')"
|
118 |
```
|
119 |
|
120 |
+
## π Model Performance
|
|
|
|
|
121 |
|
122 |
+
| Model | Size | Speed | Accuracy | Best For |
|
123 |
+
| -------------------- | ----- | --------- | ------------ | ---------------------- |
|
124 |
+
| **Florence-2-base** | 270M | β‘ Fast | π High | General OCR, Real-time |
|
125 |
+
| **Florence-2-large** | 770M | π Medium | π Very High | High accuracy needs |
|
126 |
+
| **EasyOCR** | ~100M | π Medium | π Good | Fallback, Multilingual |
|
127 |
|
128 |
+
## π― Supported Use Cases
|
|
|
|
|
129 |
|
130 |
+
| Category | Examples | Performance |
|
131 |
+
| ------------------- | ------------------------------- | ----------- |
|
132 |
+
| π **Documents** | PDFs, Scanned papers, Forms | βββββ |
|
133 |
+
| π§Ύ **Receipts** | Shopping receipts, Invoices | ββββ |
|
134 |
+
| π± **Screenshots** | App interfaces, Error messages | βββββ |
|
135 |
+
| π **Vehicle** | License plates, VIN numbers | ββββ |
|
136 |
+
| π **Books** | Printed text, Handwritten notes | ββββ |
|
137 |
+
| π **Multilingual** | Multiple languages | βββ |
|
138 |
|
139 |
+
## π§ Configuration
|
|
|
|
|
140 |
|
141 |
+
### ποΈ Model Selection
|
|
|
|
|
|
|
142 |
|
143 |
+
```python
|
144 |
+
from models.ocr_processor import OCRProcessor
|
145 |
|
146 |
+
# Fast inference (recommended)
|
147 |
+
ocr = OCRProcessor(model_name="microsoft/Florence-2-base")
|
148 |
|
149 |
+
# Maximum accuracy
|
150 |
+
ocr = OCRProcessor(model_name="microsoft/Florence-2-large")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
```
|
152 |
|
153 |
+
### π¨ UI Customization
|
154 |
|
155 |
+
Modify `ui/styles.py` to customize appearance:
|
156 |
|
157 |
+
```python
|
158 |
+
# Change color scheme
|
159 |
+
PRIMARY_COLOR = "#1f77b4"
|
160 |
+
SECONDARY_COLOR = "#ff7f0e"
|
161 |
|
162 |
+
# Update layout
|
163 |
+
INTERFACE_WIDTH = "100%"
|
164 |
+
```
|
165 |
|
166 |
+
### βοΈ Environment Variables
|
|
|
|
|
|
|
|
|
167 |
|
168 |
+
| Variable | Description | Default |
|
169 |
+
| ---------------------- | -------------------- | ---------------------- |
|
170 |
+
| `SPACE_ID` | HuggingFace Space ID | Auto-detected |
|
171 |
+
| `DEPLOYMENT_STAGE` | deployment stage | `production` |
|
172 |
+
| `TRANSFORMERS_CACHE` | Model cache path | `~/.cache/huggingface` |
|
173 |
+
| `CUDA_VISIBLE_DEVICES` | GPU selection | All available |
|
174 |
|
175 |
+
## π Deployment
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
+
### π€ HuggingFace Spaces (Recommended)
|
178 |
|
179 |
+
**Automatic Deployment:**
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
+
1. Fork this repository
|
182 |
+
2. Push to `main`/`master` branch
|
183 |
+
3. GitHub Actions automatically deploys to HuggingFace Spaces
|
184 |
+
4. Access your deployed app at: `https://huggingface.co/spaces/USERNAME/textlens-ocr`
|
185 |
|
186 |
+
**Manual Deployment:**
|
187 |
|
188 |
+
1. Go to [GitHub Actions](https://github.com/KumarAmrit30/textlens-ocr/actions)
|
189 |
+
2. Select "Deploy to HuggingFace Spaces"
|
190 |
+
3. Click "Run workflow"
|
191 |
+
4. Choose deployment type:
|
192 |
+
- **Direct**: Quick deployment to production
|
193 |
+
- **Blue-Green**: Zero downtime with staging validation
|
194 |
|
195 |
+
### π Zero Downtime Deployment
|
|
|
|
|
196 |
|
197 |
+
Our enterprise-grade deployment system ensures **zero downtime** for users:
|
|
|
|
|
|
|
|
|
198 |
|
199 |
+
**Features:**
|
200 |
|
201 |
+
- π΅ **Blue-Green Deployment**: Test in staging before production
|
202 |
+
- π₯ **Health Monitoring**: Automatic health checks with retry logic
|
203 |
+
- π **Graceful Shutdown**: Clean application restarts
|
204 |
+
- π **Real-time Monitoring**: Deployment status tracking
|
205 |
|
206 |
+
**Health Endpoints:**
|
207 |
|
208 |
+
- `GET /health` - Application health status
|
209 |
+
- `GET /ready` - Application readiness check
|
210 |
|
211 |
+
**Deployment Flow:**
|
|
|
|
|
212 |
|
213 |
+
```mermaid
|
214 |
+
graph LR
|
215 |
+
A[Code Push] --> B[Validate]
|
216 |
+
B --> C[Deploy Staging]
|
217 |
+
C --> D[Health Check]
|
218 |
+
D --> E[Deploy Production]
|
219 |
+
E --> F[Verify]
|
220 |
+
F --> G[Complete β
]
|
221 |
```
|
222 |
|
223 |
+
### π³ Docker Deployment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
|
225 |
```dockerfile
|
226 |
FROM python:3.9-slim
|
|
|
235 |
CMD ["python", "app.py"]
|
236 |
```
|
237 |
|
238 |
+
Build and run:
|
239 |
|
240 |
```bash
|
241 |
+
docker build -t textlens-ocr .
|
242 |
+
docker run -p 7860:7860 textlens-ocr
|
|
|
243 |
```
|
244 |
|
245 |
+
### βοΈ Cloud Platforms
|
246 |
|
247 |
+
| Platform | Status | Guide |
|
248 |
+
| ---------------------- | ------------- | ------------------------------------------------------------------- |
|
249 |
+
| **HuggingFace Spaces** | β
Ready | [Deploy Now](https://huggingface.co/spaces/GoConqurer/textlens-ocr) |
|
250 |
+
| **Google Colab** | β
Compatible | Open in Colab |
|
251 |
+
| **AWS/GCP/Azure** | π§ Docker | Use Docker deployment |
|
252 |
+
| **Heroku** | β οΈ Limited | GPU not available |
|
253 |
|
254 |
+
## π§ͺ Testing & Development
|
255 |
+
|
256 |
+
### π Running Tests
|
257 |
+
|
258 |
+
```bash
|
259 |
+
# Basic functionality test
|
260 |
+
python -c "
|
261 |
+
from models.ocr_processor import OCRProcessor
|
262 |
+
ocr = OCRProcessor()
|
263 |
+
print(f'β
Model loaded: {ocr.get_model_info()}')
|
264 |
+
"
|
265 |
+
|
266 |
+
# Test with sample image
|
267 |
+
python -c "
|
268 |
+
from PIL import Image
|
269 |
+
from models.ocr_processor import OCRProcessor
|
270 |
+
import requests
|
271 |
+
|
272 |
+
# Download test image
|
273 |
+
img_url = 'https://via.placeholder.com/300x100/000000/FFFFFF?text=Hello+World'
|
274 |
+
image = Image.open(requests.get(img_url, stream=True).raw)
|
275 |
+
|
276 |
+
# Test OCR
|
277 |
+
ocr = OCRProcessor()
|
278 |
+
result = ocr.extract_text(image)
|
279 |
+
print(f'β
OCR Result: {result}')
|
280 |
+
"
|
281 |
+
```
|
282 |
+
|
283 |
+
### π οΈ Development Tools
|
284 |
+
|
285 |
+
```bash
|
286 |
+
# Install development dependencies
|
287 |
+
pip install -r requirements.txt
|
288 |
|
289 |
+
# Format code
|
290 |
+
black . --line-length 88
|
291 |
+
|
292 |
+
# Type checking
|
293 |
+
mypy models/ utils/ ui/
|
294 |
+
|
295 |
+
# Lint code
|
296 |
+
flake8 --max-line-length 88
|
297 |
+
```
|
298 |
|
299 |
+
## π API Reference
|
300 |
|
301 |
### OCRProcessor Class
|
302 |
|
303 |
```python
|
304 |
from models.ocr_processor import OCRProcessor
|
305 |
|
306 |
+
# Initialize processor
|
307 |
+
ocr = OCRProcessor(
|
308 |
+
model_name="microsoft/Florence-2-base", # Model selection
|
309 |
+
device=None, # Auto-detect device
|
310 |
+
torch_dtype=None # Auto-select dtype
|
311 |
+
)
|
312 |
|
313 |
+
# Extract text from image
|
314 |
text = ocr.extract_text(image)
|
315 |
+
# Returns: str
|
316 |
|
317 |
+
# Extract text with bounding boxes
|
318 |
result = ocr.extract_text_with_regions(image)
|
319 |
+
# Returns: dict with text and regions
|
320 |
|
321 |
+
# Get model information
|
322 |
info = ocr.get_model_info()
|
323 |
+
# Returns: dict with model details
|
324 |
+
|
325 |
+
# Cleanup resources
|
326 |
+
ocr.cleanup()
|
327 |
+
```
|
328 |
+
|
329 |
+
### Health Check API
|
330 |
+
|
331 |
+
```bash
|
332 |
+
# Check application health
|
333 |
+
curl https://huggingface.co/spaces/GoConqurer/textlens-ocr/health
|
334 |
+
|
335 |
+
# Response:
|
336 |
+
{
|
337 |
+
"status": "healthy",
|
338 |
+
"timestamp": 1640995200,
|
339 |
+
"version": "1.0.0",
|
340 |
+
"environment": "production"
|
341 |
+
}
|
342 |
+
|
343 |
+
# Check readiness
|
344 |
+
curl https://huggingface.co/spaces/GoConqurer/textlens-ocr/ready
|
345 |
+
|
346 |
+
# Response:
|
347 |
+
{
|
348 |
+
"status": "ready",
|
349 |
+
"timestamp": 1640995200
|
350 |
+
}
|
351 |
```
|
352 |
|
353 |
+
## π¨ Troubleshooting
|
354 |
|
355 |
### Common Issues
|
356 |
|
357 |
+
| Issue | Symptoms | Solution |
|
358 |
+
| ----------------------- | ------------------------ | --------------------------------------- |
|
359 |
+
| **Model Loading Error** | ImportError, CUDA errors | Check GPU drivers, install CUDA toolkit |
|
360 |
+
| **Memory Error** | Out of memory | Reduce batch size, use CPU inference |
|
361 |
+
| **SSL Certificate** | SSL errors on macOS | Run certificate update command |
|
362 |
+
| **Permission Error** | File access denied | Check file permissions, run as admin |
|
363 |
+
|
364 |
+
### Debug Commands
|
365 |
+
|
366 |
+
```bash
|
367 |
+
# Check CUDA availability
|
368 |
+
python -c "import torch; print(f'CUDA: {torch.cuda.is_available()}')"
|
369 |
+
|
370 |
+
# Check transformers version
|
371 |
+
python -c "import transformers; print(f'Transformers: {transformers.__version__}')"
|
372 |
+
|
373 |
+
# Test health endpoint locally
|
374 |
+
curl http://localhost:7860/health
|
375 |
+
|
376 |
+
# View application logs
|
377 |
+
tail -f textlens.log
|
378 |
+
```
|
379 |
+
|
380 |
+
### Getting Help
|
381 |
+
|
382 |
+
1. π **Check existing issues**: [GitHub Issues](https://github.com/KumarAmrit30/textlens-ocr/issues)
|
383 |
+
2. π **Create new issue**: Provide error details and environment info
|
384 |
+
3. π¬ **Join discussion**: [GitHub Discussions](https://github.com/KumarAmrit30/textlens-ocr/discussions)
|
385 |
+
4. π§ **Contact**: Create an issue for direct support
|
386 |
+
|
387 |
+
## π€ Contributing
|
388 |
+
|
389 |
+
We welcome contributions! Here's how to get started:
|
390 |
+
|
391 |
+
### π§ Development Setup
|
392 |
+
|
393 |
+
1. **Fork & Clone**
|
394 |
|
395 |
```bash
|
396 |
+
git clone https://github.com/YOUR_USERNAME/textlens-ocr.git
|
397 |
+
cd textlens-ocr
|
398 |
```
|
399 |
|
400 |
+
2. **Create Branch**
|
401 |
|
402 |
+
```bash
|
403 |
+
git checkout -b feature/your-feature-name
|
|
|
|
|
404 |
```
|
405 |
|
406 |
+
3. **Make Changes**
|
407 |
+
|
408 |
+
- Add new features or fix bugs
|
409 |
+
- Update tests and documentation
|
410 |
+
- Follow code style guidelines
|
411 |
+
|
412 |
+
4. **Test Changes**
|
413 |
+
|
414 |
```bash
|
415 |
+
python -m pytest tests/
|
416 |
+
python -c "from models.ocr_processor import OCRProcessor; OCRProcessor()"
|
417 |
```
|
418 |
|
419 |
+
5. **Submit PR**
|
420 |
+
```bash
|
421 |
+
git add .
|
422 |
+
git commit -m "feat: add your feature description"
|
423 |
+
git push origin feature/your-feature-name
|
424 |
+
```
|
425 |
+
|
426 |
+
### π Contribution Guidelines
|
427 |
+
|
428 |
+
- **Code Style**: Follow PEP 8, use Black formatter
|
429 |
+
- **Documentation**: Update README and docstrings
|
430 |
+
- **Tests**: Add tests for new functionality
|
431 |
+
- **Commits**: Use conventional commit messages
|
432 |
+
- **Issues**: Link PRs to relevant issues
|
433 |
+
|
434 |
## π License
|
435 |
|
436 |
+
This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details.
|
437 |
+
|
438 |
+
### π Third-Party Licenses
|
439 |
|
440 |
+
- **Microsoft Florence-2**: [MIT License](https://github.com/microsoft/Florence)
|
441 |
+
- **HuggingFace Transformers**: [Apache License 2.0](https://github.com/huggingface/transformers)
|
442 |
+
- **Gradio**: [Apache License 2.0](https://github.com/gradio-app/gradio)
|
443 |
+
- **EasyOCR**: [Apache License 2.0](https://github.com/JaidedAI/EasyOCR)
|
444 |
|
445 |
+
## π Acknowledgments
|
|
|
|
|
|
|
446 |
|
447 |
+
Special thanks to:
|
448 |
|
449 |
+
- **Microsoft Research** for the incredible Florence-2 vision-language model
|
450 |
+
- **HuggingFace** for the transformers library and Spaces platform
|
451 |
+
- **Gradio Team** for the amazing web interface framework
|
452 |
+
- **JaidedAI** for EasyOCR fallback capabilities
|
453 |
+
- **Open Source Community** for continuous support and contributions
|
454 |
+
|
455 |
+
## π Project Status
|
456 |
+
|
457 |
+
| Component | Status | Version |
|
458 |
+
| ----------------- | ------------- | ------- |
|
459 |
+
| **Core OCR** | β
Stable | v1.0.0 |
|
460 |
+
| **Web UI** | β
Stable | v1.0.0 |
|
461 |
+
| **Deployment** | β
Production | v1.0.0 |
|
462 |
+
| **API** | β
Stable | v1.0.0 |
|
463 |
+
| **Documentation** | β
Complete | v1.0.0 |
|
464 |
+
|
465 |
+
### π― Roadmap
|
466 |
+
|
467 |
+
- [ ] **Multi-language UI** support
|
468 |
+
- [ ] **Batch processing** for multiple images
|
469 |
+
- [ ] **API rate limiting** and authentication
|
470 |
+
- [ ] **Custom model** fine-tuning support
|
471 |
+
- [ ] **Mobile app** development
|
472 |
+
- [ ] **Cloud storage** integration
|
473 |
+
|
474 |
+
## π Support & Community
|
475 |
+
|
476 |
+
### π Links
|
477 |
+
|
478 |
+
- **π Homepage**: [GitHub Repository](https://github.com/KumarAmrit30/textlens-ocr)
|
479 |
+
- **π Live Demo**: [HuggingFace Spaces](https://huggingface.co/spaces/GoConqurer/textlens-ocr)
|
480 |
+
- **π Issues**: [Report Bugs](https://github.com/KumarAmrit30/textlens-ocr/issues)
|
481 |
+
- **π¬ Discussions**: [GitHub Discussions](https://github.com/KumarAmrit30/textlens-ocr/discussions)
|
482 |
+
- **π Documentation**: [Deployment Guide](DEPLOYMENT.md)
|
483 |
+
|
484 |
+
### π Stats
|
485 |
+
|
486 |
+

|
487 |
+

|
488 |
+

|
489 |
|
490 |
---
|
491 |
|
492 |
+
<div align="center">
|
493 |
+
|
494 |
**Made with β€οΈ for the AI community**
|
495 |
+
|
496 |
+
[β Star this repo](https://github.com/KumarAmrit30/textlens-ocr) β’ [π Try the demo](https://huggingface.co/spaces/GoConqurer/textlens-ocr) β’ [π Read docs](DEPLOYMENT.md)
|
497 |
+
|
498 |
+
</div>
|
requirements.txt
CHANGED
@@ -6,7 +6,7 @@ sentencepiece>=0.1.97
|
|
6 |
protobuf>=3.20.0
|
7 |
|
8 |
# UI and web interface
|
9 |
-
gradio>=4.
|
10 |
|
11 |
# Image processing
|
12 |
pillow>=9.0.0
|
|
|
6 |
protobuf>=3.20.0
|
7 |
|
8 |
# UI and web interface
|
9 |
+
gradio>=4.44.0
|
10 |
|
11 |
# Image processing
|
12 |
pillow>=9.0.0
|
ui/interface.py
CHANGED
@@ -103,13 +103,15 @@ def create_interface():
|
|
103 |
image_input.upload(
|
104 |
fn=extract_text_from_image,
|
105 |
inputs=image_input,
|
106 |
-
outputs=text_output
|
|
|
107 |
)
|
108 |
|
109 |
extract_btn.click(
|
110 |
fn=extract_text_from_image,
|
111 |
inputs=image_input,
|
112 |
-
outputs=text_output
|
|
|
113 |
)
|
114 |
|
115 |
refresh_status_btn.click(
|
|
|
103 |
image_input.upload(
|
104 |
fn=extract_text_from_image,
|
105 |
inputs=image_input,
|
106 |
+
outputs=text_output,
|
107 |
+
api_name="extract_on_upload"
|
108 |
)
|
109 |
|
110 |
extract_btn.click(
|
111 |
fn=extract_text_from_image,
|
112 |
inputs=image_input,
|
113 |
+
outputs=text_output,
|
114 |
+
api_name="extract_on_click"
|
115 |
)
|
116 |
|
117 |
refresh_status_btn.click(
|