An intelligent Document Analysis API built using FastAPI, Hugging Face Transformers, and PyTorch. The system leverages a custom fine-tuned BERT model to classify text extracted from multiple document formats. It supports raw text, PDFs, Microsoft Word documents, Excel spreadsheets, CSV files, and images using Optical Character Recognition (OCR).
- 🤖 Custom fine-tuned BERT text classification model
- 🚀 High-performance FastAPI REST API
- 📝 Analyze raw text input
- 📄 PDF document processing
- 📘 Microsoft Word (.docx) support
- 📊 Excel (.xls/.xlsx) and CSV analysis
- 🖼️ OCR-based text extraction from images
- 📈 Prediction confidence scores
- 📚 Interactive API documentation (Swagger & ReDoc)
- ⚡ Automatic model loading during application startup
- 🔄 Supports multiple document formats through a single API
| File Type | Extension | Supported |
|---|---|---|
| Text | .txt |
✅ |
.pdf |
✅ | |
| Word | .docx |
✅ |
| Excel | .xlsx |
✅ |
| Excel | .xls |
✅ |
| CSV | .csv |
✅ |
| Image | .png |
✅ |
| Image | .jpg |
✅ |
| Image | .jpeg |
✅ |
FastAPI-BERT-Document-Analyzer/
│
├── compliance/
│ └── rule_engine.py
│
├── intent/
│ └── scoring.py
│
├── nlp/
│ ├── fastapi_bert.py # FastAPI application
│ └── bert_model.py # BERT model training
│
├── my_custom_model/
│ └── final/ # Saved trained model
│
├── requirements.txt
├── README.md
└── LICENSE
git clone https://github.com/yourusername/FastAPI-BERT-Document-Analyzer.git
cd FastAPI-BERT-Document-Analyzerpython -m venv venv
venv\Scripts\activatepython3 -m venv venv
source venv/bin/activatepip install -r requirements.txtRun the training script:
python nlp/bert_model.pyAfter successful training, the model will be saved inside:
my_custom_model/final/
Start the API using Uvicorn:
uvicorn nlp.fastapi_bert:app --reloadThe application will be available at:
http://127.0.0.1:8000
http://127.0.0.1:8000/docs
http://127.0.0.1:8000/redoc
POST
/analyze/text
{
"text": "This document looks suspicious."
}{
"label": "Positive",
"confidence": 98.74
}POST
/analyze/file
Upload any supported document.
Supported formats:
- TXT
- DOCX
- XLS
- XLSX
- CSV
- PNG
- JPG
- JPEG
{
"filename": "report.pdf",
"file_type": "pdf",
"analysis": {
"label": "Positive",
"confidence": 98.74
}
} User Input
│
┌──────────────┴──────────────┐
│ │
Raw Text Uploaded File
│ │
│ Detect File Type
│ │
│ ┌──────────────┬──────────────┐
│ │ │ │
│ PDF DOCX/XLSX Images
│ │ │ │
│ └──────Extract Text───────────┘
│ │
└──────────────► Preprocessing
│
▼
Fine-Tuned BERT Model
│
▼
Classification Result
│
▼
JSON Response (Label + Confidence)
- Python
- FastAPI
- Hugging Face Transformers
- PyTorch
- Pandas
- NumPy
- Scikit-learn
- Pydantic
- PyPDF2
- python-docx
- Pillow
- pytesseract
- OpenPyXL
- Uvicorn
- 🌍 Multi-language document classification
- 📂 Batch document processing
- 📄 Long document chunking
- ⚡ GPU acceleration
- 🐳 Docker support
- ☸️ Kubernetes deployment
- 🔐 JWT Authentication
- 📦 Model versioning
- 🔄 CI/CD Pipeline
- ☁️ AWS, Azure & Google Cloud deployment
- 📊 Model monitoring and analytics
Contributions are welcome!
-
Fork this repository.
-
Create a new branch.
git checkout -b feature-name- Commit your changes.
git commit -m "Add new feature"- Push to GitHub.
git push origin feature-name- Open a Pull Request.
This project is licensed under the MIT License.
Muhammad Hamid
⭐ If you found this project helpful, consider giving it a Star on GitHub!