Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
|
@@ -102,6 +102,8 @@ Handles:
|
|
| 102 |
|
| 103 |
---
|
| 104 |
|
|
|
|
|
|
|
| 105 |
## βοΈ Configuration Instructions
|
| 106 |
|
| 107 |
### 1. **Environment Variables**
|
|
@@ -161,6 +163,163 @@ Simulated full Make β API β Gmail β Airtable pipeline with live data.
|
|
| 161 |
|
| 162 |
---
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
## π§Ύ Summary
|
| 165 |
|
| 166 |
This system automates layout validation, integrates seamlessly with existing publishing workflows, and provides real-time notifications to authors and staff.
|
|
|
|
| 102 |
|
| 103 |
---
|
| 104 |
|
| 105 |
+
|
| 106 |
+
|
| 107 |
## βοΈ Configuration Instructions
|
| 108 |
|
| 109 |
### 1. **Environment Variables**
|
|
|
|
| 163 |
|
| 164 |
---
|
| 165 |
|
| 166 |
+
# π» Code Structure and Description
|
| 167 |
+
|
| 168 |
+
The repository is organized for clarity, modularity, and maintainability.
|
| 169 |
+
Each module has a defined responsibility in the validation pipeline.
|
| 170 |
+
|
| 171 |
+
```
|
| 172 |
+
project_root/
|
| 173 |
+
β
|
| 174 |
+
βββ main.py # FastAPI entrypoint and route definitions
|
| 175 |
+
βββ validator.py # Core image and OCR analysis logic
|
| 176 |
+
βββ notify.py # Airtable + webhook (Make) integration
|
| 177 |
+
βββ requirements.txt # Python dependencies
|
| 178 |
+
βββ Dockerfile # Deployment configuration for Hugging Face
|
| 179 |
+
βββ .env.example # Example environment variable file
|
| 180 |
+
βββ test_images/ # Sample covers for QA and benchmarking
|
| 181 |
+
```
|
| 182 |
+
|
| 183 |
+
---
|
| 184 |
+
|
| 185 |
+
## π§© Module Breakdown
|
| 186 |
+
|
| 187 |
+
### **main.py**
|
| 188 |
+
Handles all HTTP requests via FastAPI.
|
| 189 |
+
|
| 190 |
+
**Key functions**
|
| 191 |
+
- `@app.post("/analyze")`: receives file uploads, saves to temp storage.
|
| 192 |
+
- Calls `process_image()` from `validator.py`.
|
| 193 |
+
- Computes `status`, `confidence`, and `issues`.
|
| 194 |
+
- Sends final results to Airtable and triggers Make webhook for emails.
|
| 195 |
+
|
| 196 |
+
**Error handling**
|
| 197 |
+
- All errors return structured `HTTPException` with `500` status and message trace.
|
| 198 |
+
|
| 199 |
+
---
|
| 200 |
+
|
| 201 |
+
### **validator.py**
|
| 202 |
+
Implements all computer vision and text-detection logic.
|
| 203 |
+
|
| 204 |
+
**Core components**
|
| 205 |
+
- **OCR Detection**: uses `easyocr.Reader` for text box extraction.
|
| 206 |
+
- **Overlap Confidence**: intersection ratio between text and badge zone.
|
| 207 |
+
- **Safe Zone Validation**: 3 mm margins and 9 mm bottom reserved space.
|
| 208 |
+
- **Image Quality**: checks blur variance and resolution.
|
| 209 |
+
- **OCR Confidence**: mean OCR confidence across detected lines.
|
| 210 |
+
|
| 211 |
+
**Outputs**
|
| 212 |
+
Returns a dictionary:
|
| 213 |
+
```python
|
| 214 |
+
{
|
| 215 |
+
"cover_valid": bool,
|
| 216 |
+
"confidence_score": float,
|
| 217 |
+
"unauthorized_text_in_award_zone": [...],
|
| 218 |
+
"text_in_safe_margin": [...],
|
| 219 |
+
"validation_message": str,
|
| 220 |
+
"overlay_path": str
|
| 221 |
+
}
|
| 222 |
+
```
|
| 223 |
+
|
| 224 |
+
---
|
| 225 |
+
|
| 226 |
+
### **notify.py**
|
| 227 |
+
Handles post-processing integrations.
|
| 228 |
+
|
| 229 |
+
**Functions**
|
| 230 |
+
- `update_airtable(...)`
|
| 231 |
+
- Connects to Airtable using **PyAirtable**.
|
| 232 |
+
- Updates or creates record entries for validated covers.
|
| 233 |
+
- `send_email(...)`
|
| 234 |
+
- Sends formatted HTML emails to authors through Make webhook API.
|
| 235 |
+
|
| 236 |
+
---
|
| 237 |
+
|
| 238 |
+
### **requirements.txt**
|
| 239 |
+
Lists dependencies for deployment.
|
| 240 |
+
|
| 241 |
+
Key libraries:
|
| 242 |
+
- `fastapi`, `uvicorn` β API server
|
| 243 |
+
- `opencv-python`, `easyocr`, `numpy`, `pillow` β image analysis
|
| 244 |
+
- `pyairtable`, `requests`, `python-dotenv` β integrations and config
|
| 245 |
+
- `gunicorn` β production server
|
| 246 |
+
|
| 247 |
+
---
|
| 248 |
+
|
| 249 |
+
### **Dockerfile**
|
| 250 |
+
Defines build environment for Hugging Face deployment.
|
| 251 |
+
|
| 252 |
+
**Highlights**
|
| 253 |
+
- Based on `python:3.11-slim`
|
| 254 |
+
- Installs system packages (`libgl1`, `poppler-utils`, etc.)
|
| 255 |
+
- Copies code and installs dependencies
|
| 256 |
+
- Launches FastAPI on port `7860`
|
| 257 |
+
|
| 258 |
+
---
|
| 259 |
+
|
| 260 |
+
### **.env.example**
|
| 261 |
+
Template for environment configuration:
|
| 262 |
+
```
|
| 263 |
+
AIRTABLE_BASE=appXXXX
|
| 264 |
+
AIRTABLE_TABLE=Book cover revision
|
| 265 |
+
AIRTABLE_KEY=keyXXXX
|
| 266 |
+
MAKE_WEBHOOK=https://hook.eu1.make.com/abcd1234efgh5678
|
| 267 |
+
FROM_EMAIL=team@bookleafpublishing.com
|
| 268 |
+
```
|
| 269 |
+
|
| 270 |
+
---
|
| 271 |
+
|
| 272 |
+
### **test_images/**
|
| 273 |
+
Contains controlled test samples for benchmarking:
|
| 274 |
+
- `pass_sample.png` β valid layout
|
| 275 |
+
- `overlap_badge.png` β author text inside award zone
|
| 276 |
+
- `margin_violation.png` β text in unsafe margin
|
| 277 |
+
- `lowres_cover.png` β image quality test
|
| 278 |
+
|
| 279 |
+
---
|
| 280 |
+
|
| 281 |
+
## π§ Code Highlights
|
| 282 |
+
|
| 283 |
+
- **Reusable design:** each validation function operates independently.
|
| 284 |
+
- **Single model load:** EasyOCR initialized once at startup β faster inference.
|
| 285 |
+
- **Modular I/O:** output dictionary used by both API and external automations.
|
| 286 |
+
- **Extensible:** can plug new validation rules (e.g., typography checks) without changing API schema.
|
| 287 |
+
|
| 288 |
+
---
|
| 289 |
+
|
| 290 |
+
## π§Ύ Example Data Flow (Code-Level)
|
| 291 |
+
|
| 292 |
+
```
|
| 293 |
+
main.py (FastAPI)
|
| 294 |
+
β
|
| 295 |
+
βββΊ validator.py β process_image()
|
| 296 |
+
β β
|
| 297 |
+
β βββΊ detect_text() β EasyOCR
|
| 298 |
+
β βββΊ check_safe_zones() β OpenCV geometry
|
| 299 |
+
β βββΊ check_image_quality() β blur/resolution
|
| 300 |
+
β βββΊ compute_confidence()
|
| 301 |
+
β
|
| 302 |
+
βββΊ notify.py
|
| 303 |
+
βββΊ update_airtable()
|
| 304 |
+
βββΊ send_email() β Make webhook β Gmail
|
| 305 |
+
```
|
| 306 |
+
|
| 307 |
+
---
|
| 308 |
+
|
| 309 |
+
## π Key Code Metrics
|
| 310 |
+
|
| 311 |
+
| Component | Avg Runtime | Accuracy | Notes |
|
| 312 |
+
|------------|--------------|-----------|--------|
|
| 313 |
+
| OCR + Layout detection | ~2.9 s | 93% | Model cached after load |
|
| 314 |
+
| Image quality check | <0.4 s | 100% | Laplacian variance method |
|
| 315 |
+
| Overlap confidence | <0.3 s | 99% | Ratio-based intersection |
|
| 316 |
+
| Full API cycle | ~5 s | β | Includes file I/O |
|
| 317 |
+
|
| 318 |
+
---
|
| 319 |
+
|
| 320 |
+
**Result:**
|
| 321 |
+
A modular, production-ready codebase that integrates machine vision, workflow automation, and data tracking in a single lightweight API.
|
| 322 |
+
|
| 323 |
## π§Ύ Summary
|
| 324 |
|
| 325 |
This system automates layout validation, integrates seamlessly with existing publishing workflows, and provides real-time notifications to authors and staff.
|