Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

FastAPI with BERT Document Analyzer

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).


✨ Features

  • 🤖 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

📂 Supported File Formats

File Type Extension Supported
Text .txt
PDF .pdf
Word .docx
Excel .xlsx
Excel .xls
CSV .csv
Image .png
Image .jpg
Image .jpeg

📁 Project Structure

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

⚙️ Installation

1. Clone the Repository

git clone https://github.com/yourusername/FastAPI-BERT-Document-Analyzer.git

cd FastAPI-BERT-Document-Analyzer

2. Create a Virtual Environment

Windows

python -m venv venv

venv\Scripts\activate

Linux / macOS

python3 -m venv venv

source venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

🏋️ Train the BERT Model

Run the training script:

python nlp/bert_model.py

After successful training, the model will be saved inside:

my_custom_model/final/

🚀 Run the FastAPI Server

Start the API using Uvicorn:

uvicorn nlp.fastapi_bert:app --reload

The application will be available at:

http://127.0.0.1:8000

📚 API Documentation

Swagger UI

http://127.0.0.1:8000/docs

ReDoc

http://127.0.0.1:8000/redoc

📌 API Endpoints

1️⃣ Analyze Raw Text

POST

/analyze/text

Request

{
    "text": "This document looks suspicious."
}

Response

{
  "label": "Positive",
  "confidence": 98.74
}

2️⃣ Analyze Document

POST

/analyze/file

Upload any supported document.

Supported formats:

  • TXT
  • PDF
  • DOCX
  • XLS
  • XLSX
  • CSV
  • PNG
  • JPG
  • JPEG

Example Response

{
    "filename": "report.pdf",
    "file_type": "pdf",
    "analysis": {
        "label": "Positive",
        "confidence": 98.74
    }
}

🧠 Processing Workflow

                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)

🛠️ Technology Stack

  • Python
  • FastAPI
  • Hugging Face Transformers
  • PyTorch
  • Pandas
  • NumPy
  • Scikit-learn
  • Pydantic
  • PyPDF2
  • python-docx
  • Pillow
  • pytesseract
  • OpenPyXL
  • Uvicorn

📈 Future Enhancements

  • 🌍 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

🤝 Contributing

Contributions are welcome!

  1. Fork this repository.

  2. Create a new branch.

git checkout -b feature-name
  1. Commit your changes.
git commit -m "Add new feature"
  1. Push to GitHub.
git push origin feature-name
  1. Open a Pull Request.

📄 License

This project is licensed under the MIT License.


👨‍💻 Author

Muhammad Hamid


⭐ If you found this project helpful, consider giving it a Star on GitHub!

About

An AI-powered document analysis system built with FastAPI and a fine-tuned BERT model. Supports text, PDF, Word, Excel, CSV, and image classification with OCR-based text extraction.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages