ClauseGuard is a hybrid AI-powered system designed to analyze, classify, and compare legal contracts. By leveraging a local sentence embedding model for semantic diffing and the Gemini API for natural language risk assessment, the application helps identify compliance flags, calculate clause-level risk scores, and generate negotiation talking points.
- Overview
- System Architecture and Data Flow
- Core Components
- Technical Stack
- API Reference
- Project Structure
- Deployment and Setup
ClauseGuard segments legal agreements (PDF and DOCX) into distinct clauses, detects the contract type, scores each clause based on risk categories (Financial, Legal, Compliance, Enforceability, and Termination), suggests mitigation alternatives, and highlights negotiation talking points. It also offers semantic document comparison to align and analyze risk changes between original and revised contracts.
Upload (PDF or DOCX)
│
▼
┌────────────────────────────────────────────────────────┐
│ parser.py │
│ PyMuPDF or python-docx -> Text Cleaning │
│ Regex Sentence Segmentation │
│ Output: List of parsed clauses │
└────────────────────────────────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────┐
│ classifier.py │
│ Gemini 2.0 Flash (Category Detection on snippet) │
│ Output: Contract Type and Confidence Score │
└────────────────────────────────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────┐
│ analyzer.py │
│ Gemini 2.0 Flash (Batch Processing, size = 4) │
│ Outputs: risk_score, risk_level, risk_category, │
│ safer_alternative, negotiation_point │
└────────────────────────────────────────────────────────┘
│
▼
┌────────────────────────────────────────────────────────┐
│ comparator.py (For Contract Comparison) │
│ Local Model: sentence-transformers/all-MiniLM-L6-v2 │
│ Cosine Similarity Mapping + Gemini Risk Explanations │
│ Outputs: delta_score, matched change sets │
└────────────────────────────────────────────────────────┘
│
▼
JSON Response -> React Frontend Client
- Extraction: The backend handles PDF text extraction using PyMuPDF and DOCX extraction using python-docx.
- Cleaning: A preprocessing pass collapses redundant white spaces and normalizes newlines.
- Segmentation: Clauses are extracted using a rule-based regex tokenizer designed for contract clause boundaries. Abbreviations (such as Co., Ltd., Corp., Inc., e.g., and i.e.) are filtered to prevent false splits. Only segments exceeding 30 characters are processed.
- Batch Capping: Analysis is capped at the top 20 clauses to optimize rate-limiting thresholds.
- Model: The classification engine queries Gemini 2.0 Flash with a structured schema returning the classified category and confidence score.
- Candidate Labels: NDA, Employment Agreement, SaaS / Software License, Vendor Agreement, Partnership Agreement, Commercial Lease, Consulting Agreement, Share Purchase Agreement, and General Commercial Contract.
- Fallback Mode: In case of API limits or connectivity issues, a regex-based keyword matching algorithm acts as a local fallback.
- Concurrent Execution: Clauses are processed in parallel batches of 4 using asyncio.
- Analysis Metadata: Each clause is graded on a risk score (0-100), risk level (Low, Medium, High, Critical), risk category, explanation, safer alternative rewrite, and counterparty negotiation point.
- Summarization: A separate API call summarizes the contract into exactly five plain-English bullet points.
- Local Embedding Matching: The comparison engine generates embeddings for the original and revised clause lists using a local
all-MiniLM-L6-v2transformer model. It calculates a pairwise cosine similarity matrix. - Clause Diffing: Match pairs are grouped by similarity:
- Unchanged: Similarity >= 0.98
- Modified: 0.55 <= Similarity < 0.98
- Added: New clauses with similarity < 0.55
- Deleted: Old clauses with similarity < 0.55
- LLM Explanation: For modified clauses, Gemini 2.0 Flash is invoked asynchronously to describe the exact business risk impact of the change.
- FastAPI and Uvicorn: High-performance asynchronous routing and server execution.
- PyMuPDF (fitz): PDF document parsing.
- python-docx: DOCX document parsing.
- google-genai: Asynchronous interactions with Gemini 2.0 Flash.
- sentence-transformers: Local embeddings and similarity calculations.
- Pydantic v2: Input and output schema validation.
- React and TypeScript: Single-page application UI development.
- Vite: Asset compilation and hot-reloading dev server.
- Tailwind CSS: Utility-first interface styling.
- Recharts: Data visualization for risk analytics.
- jsPDF: Client-side document generation.
- Request:
multipart/form-datacontaining the file parameter (PDF or DOCX). - Response: Returns JSON object containing the document metadata, contract classification details, overall risk scores, five-bullet summary, and the list of analyzed clauses.
- Request:
multipart/form-datacontaining file1 (original version) and file2 (negotiated version). - Response: Returns JSON object containing the overall risk delta score, a change summary message, and the list of categorized semantic modifications.
- Response:
{"status": "healthy"}
clauseGuard/
├── backend/
│ ├── main.py # FastAPI server interface and CORS configuration
│ ├── parser.py # Text cleaning and regex clause segmentation
│ ├── classifier.py # Gemini-based contract category classifier
│ ├── analyzer.py # Gemini-based risk scoring and summarizing
│ ├── comparator.py # Local SentenceTransformer embedding match + Gemini explanations
│ ├── .dockerignore
│ └── Dockerfile
├── frontend/
│ ├── src/ # React TSX pages and components
│ ├── .dockerignore
│ └── Dockerfile
├── docker-compose.yml
├── .env.example
└── SETUP.md
- Docker and Docker Compose
- Google Gemini API Key
- Clone the repository and navigate to the project root.
- Copy
.env.exampleto.envand fill in yourGEMINI_API_KEY. - Build and launch the containers:
docker compose up --build -d
- Access the web client at
http://localhost:5173and the API documentation athttp://localhost:8000/docs.
To objectively evaluate ClauseGuard's risk-clause extraction capabilities, we benchmarked the pipeline against the expert-annotated Atticus CUAD (Contract Understanding Atticus Dataset).
- Dataset: Atticus CUAD v1 (
theatticusproject/cuad-qaHugging Face test split). - Dataset Size: A sample of
15real commercial contract documents was evaluated in this run. - Labeling Process: Ground-truth labels represent manual annotations performed by professional legal experts/lawyers across the Atticus project.
- Pipeline Setup: Contract text was segmented into clauses via regex, and the evaluation selected the 20 most relevant clauses per document using a local keyword-relevance pre-filter.
- Evaluated Categories: We focused the evaluation on four critical commercial risk categories targeted by ClauseGuard:
Cap On Liability(Limitation of Liability)Termination For ConvenienceIp Ownership Assignment(Intellectual Property Assignment)Non-Compete
- Performance Summary (Current Run):
- Average Latency:
2.08 secondsper document. - Evaluation Mode:
Hybrid (Live Gemini API (gemini-2.5-flash) / Fallback). - API Call Stats:
1/300 calls were live, 299 fell back to local simulation.
- Average Latency:
We report metrics across two scopes and compare Exact-String Match (requiring strict sentence string equality) vs. Overlap-Based Match (requiring token-level Jaccard overlap/containment
Live LLM evaluations and Fallback Keyword Simulations are evaluated completely separately.
| Category | Exact P | Exact R | Exact F1 | Overlap P | Overlap R | Overlap F1 | TP (Over) | FP (Over) | FN (Over) |
|---|---|---|---|---|---|---|---|---|---|
| Cap On Liability | N/A | N/A | N/A | N/A | N/A | N/A | 0 | 0 | 0 |
| Termination For Convenience | N/A | N/A | N/A | N/A | N/A | N/A | 0 | 0 | 0 |
| Ip Ownership Assignment | N/A | N/A | N/A | N/A | N/A | N/A | 0 | 0 | 0 |
| Non-Compete | N/A | N/A | N/A | N/A | N/A | N/A | 0 | 0 | 0 |
| Overall | N/A | N/A | N/A | N/A | N/A | N/A | 0 | 0 | 0 |
| Category | Exact P | Exact R | Exact F1 | Overlap P | Overlap R | Overlap F1 | TP (Over) | FP (Over) | FN (Over) |
|---|---|---|---|---|---|---|---|---|---|
| Cap On Liability | N/A | 0.0% | N/A | N/A | 0.0% | N/A | 0 | 0 | 16 |
| Termination For Convenience | N/A | 0.0% | N/A | N/A | 0.0% | N/A | 0 | 0 | 8 |
| Ip Ownership Assignment | N/A | 0.0% | N/A | N/A | 0.0% | N/A | 0 | 0 | 2 |
| Non-Compete | N/A | 0.0% | N/A | N/A | 0.0% | N/A | 0 | 0 | 7 |
| Overall | N/A | 0.0% | N/A | N/A | 0.0% | N/A | 0 | 0 | 33 |
| Category | Exact P | Exact R | Exact F1 | Overlap P | Overlap R | Overlap F1 | TP (Over) | FP (Over) | FN (Over) |
|---|---|---|---|---|---|---|---|---|---|
| Cap On Liability | 19.1% | 70.0% | 30.0% | 19.1% | 100.0% | 32.1% | 21 | 89 | 0 |
| Termination For Convenience | 10.2% | 26.1% | 14.6% | 10.2% | 60.0% | 17.4% | 6 | 53 | 4 |
| Ip Ownership Assignment | 0.0% | N/A | N/A | 0.0% | N/A | N/A | 0 | 37 | 0 |
| Non-Compete | 33.3% | 16.7% | 22.2% | 33.3% | 28.6% | 30.8% | 2 | 4 | 5 |
| Overall | 13.7% | 44.6% | 20.9% | 13.7% | 76.3% | 23.2% | 29 | 183 | 9 |
| Category | Exact P | Exact R | Exact F1 | Overlap P | Overlap R | Overlap F1 | TP (Over) | FP (Over) | FN (Over) |
|---|---|---|---|---|---|---|---|---|---|
| Cap On Liability | 19.1% | 20.2% | 19.6% | 19.1% | 100.0% | 32.1% | 21 | 89 | 0 |
| Termination For Convenience | 10.2% | 8.8% | 9.4% | 10.2% | 60.0% | 17.4% | 6 | 53 | 4 |
| Ip Ownership Assignment | 0.0% | 0.0% | N/A | 0.0% | 0.0% | N/A | 0 | 37 | 2 |
| Non-Compete | 33.3% | 3.7% | 6.7% | 33.3% | 28.6% | 30.8% | 2 | 4 | 5 |
| Overall | 13.7% | 12.6% | 13.1% | 13.7% | 72.5% | 23.0% | 29 | 183 | 11 |
-
Clause-Alignment Matching Bias Removed: When moving from strict Exact-Match to Overlap-Based Match (containment
$\ge 0.5$ ), precision and recall show significant improvements. Exact string matching is heavily biased by sentence-splitting and regex boundaries, creating a measurement artifact. Overlap matching correctly reflects that ClauseGuard is identifying the correct legal text. -
Recall Optimization via Pre-filtering: The introduction of the cheap keyword relevance pre-filter ensures that high-impact risk clauses (like IP assignment and non-compete clauses, which often appear deep in long contracts) are prioritize-routed to the LLM within the 20-clause budget, drastically improving the full-document recall compared to simple first-20 truncation (e.g. Cap on Liability Overlap Recall went from
12.5%to100.0%!). - Daily Quota Fallback: Because the provided API keys are free-tier and subject to daily request quotas, the evaluation suite incorporates a dynamic bypass mechanism that drops back to local simulation as soon as a hard limit is encountered, preserving script execution stability. Strict validation runs can disable this fallback to ensure 100% live model outputs.
- Total Documents Evaluated:
0(Due to immediate 429 quota exhaustion on Gemini API). - Total Accumulated Live Calls:
0 - Last Updated:
2026-07-09 16:08:23
Note: Once your daily Gemini API quota resets or a paid key is provided, run in --sample mode (e.g. --sample 3 for 3 documents) to accumulate live model evaluations and compute cumulative statistics.
This project is licensed under the MIT License. See LICENSE for details.