gurki309/contract-analysis-ai
0
Contract Analysis AI — Backend
Fine-grained contract clause classification powered by DistilBERT + FastAPI
Architecture
contract-ai/
├── app/
│ ├── api/
│ │ ├── routes.py # GET /health · POST /predict · POST /train
│ │ └── schema.py # Pydantic request/response models
│ ├── core/
│ │ ├── config.py # Pydantic Settings (env vars / .env)
│ │ └── model_loader.py # Singleton model registry (loaded once)
│ ├── services/
│ │ ├── inference.py # predict_clause(text) → label + confidence
│ │ ├── preprocessing.py # Dataset load, clean, tokenize
│ │ └── training.py # HuggingFace Trainer pipeline
│ └── main.py # FastAPI app + lifespan startup
├── models/
│ ├── contract_classifier/ # Fine-tuned model weights (save here)
│ └── label_map.json # { "0": "Termination", "1": "Indemnification", … }
├── data/ # Raw / cached dataset files
├── notebooks/
│ └── colab_training.ipynb # 🔥 Google Colab training notebook (GPU)
├── pyproject.toml
└── .env.exampleQuick Start
Prerequisites
- Python 3.10+
- `uv` — install with
pip install uv
1 — Clone & Install
git clone <your-repo-url>
cd legal-contract/contract-ai
cp .env.example .env
uv sync # installs all dependencies from pyproject.toml2 — Train the Model
Option A — 🔥 Google Colab (Recommended, free GPU)
- Upload
notebooks/colab_training.ipynbto colab.research.google.com - Set runtime to GPU → T4 via Runtime → Change runtime type
- Run all cells — training takes ~20–30 minutes on T4
- Download
contract_classifier.zipwhen prompted - Extract into your
models/directory: Expand-Archive contractclassifier.zip -DestinationPath models\contractclassifier copy models\contractclassifier\labelmap.json models\label_map.json
Option B — Local Training via API endpoint
Start the server first, then trigger training:
# Start server (model will be unavailable until training finishes)
uv run uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
# In another terminal — launch training
curl -X POST http://localhost:8000/api/v1/train \
-H "Content-Type: application/json" \
-d '{"num_train_epochs": 3, "per_device_train_batch_size": 16, "learning_rate": 2e-5}'Option C — Standalone script
uv run python -m app.services.training3 — Run the API
uv run uvicorn app.main:app --host 0.0.0.0 --port 8000 --reloadAPI docs available at: http://localhost:8000/docs
API Reference
GET /api/v1/health
{
"status": "ok",
"model_loaded": true,
"version": "1.0.0"
}POST /api/v1/predict
Request
{ "text": "This agreement may be terminated by either party upon 30 days written notice." }Response
{ "label": "Termination", "confidence": 0.9821 }POST /api/v1/train
Request (all fields optional)
{
"num_train_epochs": 3,
"per_device_train_batch_size": 16,
"learning_rate": 2e-5,
"test_size": 0.15
}Response 202 Accepted
{
"status": "training_started",
"message": "Fine-tuning started in the background. Check GET /api/v1/health — when model_loaded=true the new model is ready."
}Dataset
- Source: `alisha4walunj/quad_ledgar_merged_dataset` (HuggingFace Hub)
- Text column:
provision - Label column:
label - Model:
distilbert-base-uncased
Performance
Configuration
All settings can be overridden via .env or environment variables. See .env.example for the full list.
