Exposing the fine print. Protecting consumer rights. In your own language.
LexGuard is an advanced, production-grade AI platform designed to decode predatory contracts, complex Terms of Service (ToS), and EULAs in seconds. Built for the Prompt-Wars Hackathon, LexGuard uses a state-of-the-art multi-agent pipeline powered by Google Vertex AI to highlight risks, detect dark patterns, calculate power imbalances, and translate legal jargon into plain, actionable advice in multiple Indic languages.
The platform ships as two integrated products:
- 🖥️ Web Dashboard — A React-based courtroom-themed interface where users can paste or upload contracts (PDF, DOCX, TXT) and receive a full risk verdict with clause-by-clause cross-examination.
- 🧩 Chrome Extension (MV3) — A real-time browser popup that intercepts terms-of-service pages, scans their DOM content, and delivers an instant risk score and flagged clause breakdown without leaving the page — with full Indic language support.
🌍 Production Dashboard: https://lexguard-1068366375204.us-central1.run.app
- Problem Statement Alignment
- Code Quality
- Security
- Efficiency
- Testing
- Accessibility
- Google Services Integration
- Key Features
- System Architecture
- AI Pipeline — Deep Dive
- API Reference
- Installation & Local Setup
- Cloud Deployment
- Cost Analysis
- Code Structure
| Problem Requirement | LexGuard Implementation |
|---|---|
| Analyze uploaded legal documents | ✅ PDF, DOCX, TXT via unified parser with magic-byte detection |
| Extract and classify clauses | ✅ Agent 1+2: LLM-based extraction with 7 legal domain classifiers |
| Identify hidden liabilities & exploitative language | ✅ Agent 3+4: Adversarial reasoning with worst-case intent simulation |
| Generate severity-based risk scores | ✅ overall_risk_score (0–100) + power_imbalance metric |
| Plain language explanations | ✅ plain_english (8th grade rewrite) per clause |
| Multi-language support | ✅ Full Indic language translation in every output field via Agent 5–7 |
| Detect dark patterns | ✅ dark_pattern: true/false + dark_pattern_type per clause |
| Real-time deployment | ✅ Deployed on Google Cloud Run, publicly accessible |
| Explainability over opaque predictions | ✅ Every score includes why_flagged, what_it_means, consequence, negotiation_tip |
LexGuard's backend architecture adheres strictly to microservice-oriented design principles, implementing a rigid separation of concerns across discrete operational layers:
main.py ← Transport Layer (FastAPI routing, schema validation, HTTP context)
parser.py ← Data Ingestion Layer (Magic-byte heuristics, multimodal extraction)
agents/
pipeline.py ← Intelligence Layer (Vertex AI orchestration, deterministic LLM chains)
Architectural Benefits:
- High Cohesion & Low Coupling: Modification of extraction logic (
parser.py) requires zero regressions in the LLM pipeline (pipeline.py). - Maintainability: The API surface area is explicitly contract-bound within
main.pyusing Pydantic models, ensuring strict static typing and request validation at the periphery.
class AnalyzeRequest(BaseModel):
document: str
language: Optional[str] = "en"
document_type: Optional[str] = NoneInbound request payloads are enforced at the ASGI middleware level. Malformed JSON, omitted required parameters, or data type violations automatically yield a deterministic HTTP 422 Unprocessable Entity exception, preventing unvalidated states from reaching the inference layer.
Each Vertex AI system instruction block is engineered for maximum reliability:
- Granular Scoping: Agentic responsibilities are strictly isolated to prevent prompt drift.
- Constrained Generation: Imposing
response_mime_type="application/json"forces structural compliance at the model layer. - Defensive Deserialization: Downstream JSON deserialization utilizes resilient fallback mechanisms to prevent pipeline cascading failures.
| Proposed Refactoring | Engineering Impact |
|---|---|
| Implement PEP 257 standard docstrings | Enhances automated documentation generation and developer onboarding |
| Decouple agents into abstract base classes | Facilitates dependency injection for unit testing and mocking |
| Enforce Pydantic validation on outbound schemas | guarantees strict API contract compliance for consumer interfaces |
Integrate native Python logging handlers |
Replaces exception swallowing with enterprise-grade telemetry and observability |
LexGuard is engineered defensively, mitigating common attack vectors at the infrastructure and application layers.
| Security Practice | Technical Implementation |
|---|---|
| Credential Abstraction | Authentication relies exclusively on Google Application Default Credentials (ADC); zero hardcoded keys are present in the repository. |
| Stateless Architecture | Eliminates entire classes of vulnerabilities (e.g., SQL injection, state manipulation) by avoiding persistent storage mechanisms. |
| Input Sanitization | Aggressive whitespace stripping and defensive Base64 decoding bounds prevent arbitrary payload execution. |
| Exception Obfuscation | Unhandled server-side exceptions are caught globally and sanitized, preventing stack-trace leakage to unauthorized clients. |
| Deterministic Dependencies | requirements.txt enforces strict version pinning to mitigate upstream supply-chain injection attacks. |
| Identified Vulnerability | Current Mitigation | Remediation Strategy |
|---|---|---|
| Volumetric Attacks (DDoS) | None (Susceptible to API exhaustion) | Integrate ASGI-level rate limiting via slowapi (e.g., Leaky Bucket algorithm). |
| Denial of Wallet (DoW) | Unbounded token intake | Enforce strict payload byte-limits in Pydantic to cap maximum Vertex AI token ingestion. |
| Transport Layer Security | Terminated at GCP Load Balancer ✅ | Infrastructure configuration deemed sufficient. |
| Cross-Site Scripting (XSS) | Static frontend lacks strict CSP | Inject X-Content-Type-Options and Content-Security-Policy headers via FastAPI middleware. |
| Vulnerable Dependencies | Manual updates only | Automate dependency auditing in CI/CD via pip-audit or Dependabot. |
MODEL_NAME = "gemini-2.5-flash-lite"LexGuard leverages gemini-2.5-flash-lite—Google's most optimized, high-throughput model designed specifically for low-latency, high-volume tasks. Operating at $0.0375 per 1M input tokens, a standard 5,000-token document incurs an inference cost of approximately $0.0002, enabling hyperscale operation without prohibitive cloud expenditure.
GenerationConfig(response_mime_type="application/json", temperature=0.0)Enforcing temperature=0.0 achieves zero-variance output generation. This architectural choice:
- Eliminates stochastic hallucinations, negating the need for costly multi-attempt validation loops.
- Yields mathematically reproducible risk scores critical for legal compliance auditing.
- Guarantees strict JSON schema adherence, preventing downstream parser failures.
The multi-agent orchestration executes synchronously (Agent 1 → Agent 2 → Agent 3). While parallel fan-out could theoretically reduce latency, synchronous chaining ensures immediate short-circuiting if an upstream classification fails, drastically reducing wasted computational overhead.
if decoded.startswith(b'%PDF'): # PDF Header Verification
elif decoded.startswith(b'PK'): # DOCX (ZIP) Signature VerificationDocument format resolution relies on binary magic-byte inspection (O(1) time complexity) rather than instantiating heavyweight parsing libraries prematurely, preserving critical memory allocations.
| Identified Bottleneck | Proposed Engineering Solution | Anticipated Performance Gain |
|---|---|---|
| Sequential Inference Latency | Implement Redis/Memcached LRU cache utilizing SHA-256 document hashing | O(1) retrieval (near 0ms) for previously analyzed identical documents |
| Blocking I/O Operations | Transition FastAPI transport layer to async def and utilize non-blocking await for Vertex API calls |
Drastic increase in concurrent connection capacity per worker thread |
| Token Overhead in Stage 3 | Implement dynamic pruning to truncate the payload to the top |
30–40% reduction in token consumption and inference duration |
| Time-to-First-Byte (TTFB) | Implement Vertex AI Streaming API coupled with Server-Sent Events (SSE) | Massive reduction in perceived latency via progressive DOM rendering |
The repository includes a comprehensive test suite (/tests) containing 6 discrete testing modules designed to validate API integrity and deterministic model behavior.
| Test Module | Coverage Scope |
|---|---|
test_api.py |
End-to-end integration testing for the FastAPI /analyze endpoint using standard contract fixtures. |
test_b64.py |
Validation of the binary ingestion pipeline, ensuring accurate Base64 decoding and magic-byte extraction for PDF/DOCX streams. |
test_models.py |
Unit testing for Vertex AI SDK instantiation and network connectivity constraints. |
test_user.py |
E2E functional testing simulating full client-side lifecycles (Ingestion → Parsing → Agentic Analysis → Schema Validation). |
test_vertex_lite.py |
Environmental validation confirming gemini-2.5-flash-lite deployment availability and quota state. |
test_vertex_models.py |
Configuration auditing to ensure strict adherence to temperature=0.0 and JSON MIME-type constraints. |
Test assertions transcend basic HTTP status code validation by rigorously enforcing JSON schema contracts, preventing silent serialization mutations between the microservice and the client interface.
# Schema Validation Example (test_api.py)
assert "overall_risk_score" in result
assert "flagged_clauses" in result
assert isinstance(result["flagged_clauses"], list)| Testing Deficit | Remediation Strategy |
|---|---|
| Lack of Offline Mocking | Implement unittest.mock.patch stubs for Vertex AI egress calls to enable hermetic, cost-free local CI execution. |
| Manual Execution | Integrate GitHub Actions workflows to enforce automated gating and test execution on all Pull Requests. |
| Capacity Planning | Deploy locust load-testing scripts to establish empirical concurrency limits and autoscaling thresholds for Cloud Run. |
| Edge-Case Resilience | Expand test fixtures to include malformed binaries, heavily corrupted PDFs, and exotic UTF-8 encodings to validate defensive parsing. |
| Code Coverage Visibility | Integrate pytest --cov to enforce branch-coverage minimums. |
LexGuard is engineered to lower the cognitive and linguistic barriers associated with complex legal agreements.
| Architectural Feature | Accessibility Impact |
|---|---|
| Native Multi-Lingual Generation | Robust Indic support (Hindi, Kannada, Tamil, Telugu, Marathi). LLM constraints enforce translation across all output schema nodes. |
| Cognitive Simplification | Algorithmic generation of Flesch-Kincaid 8th-grade equivalent summaries (plain_english field) for complex legalese. |
| Categorical Risk Heuristics | Normalization of abstract risk vectors into universally recognizable categorical primitives (HIGH / MEDIUM / LOW). |
| Format Agnosticism | Support for heterogeneous ingestion streams (PDF, DOCX, UTF-8 strings), eliminating the need for specialized client-side software. |
| Zero-Friction Extension | The MV3 extension operates ephemerally within the DOM context, preempting blind consent without requiring context switching. |
Zero-Latency Translation Strategy: By dynamically interpolating the client's language preference parameter directly into the Agent 3 system prompt, LexGuard forces the LLM to emit translated payloads natively. This eliminates the requirement for asynchronous, post-generation translation middleware, significantly reducing system latency and API expenditure.
sys_prompt_3 = f"""...translate explanations into {language}..."""| Identified Constraint | Remediation Strategy |
|---|---|
| Screen-Reader Compatibility | Inject explicit aria-label and aria-live DOM attributes to support auditory navigation of dynamic state changes. |
| Keyboard Navigability | Enforce strict tabindex flows and specialized :focus-visible CSS pseudo-classes for users lacking precision pointing devices. |
| Visual Impairment Accommodations | Introduce WCAG-compliant high-contrast theme toggles. |
| RTL Rendering Support | Implement dynamic CSS logical properties and dir="rtl" HTML attributes to support Arabic/Urdu demographics. |
LexGuard leverages a comprehensive suite of Google Cloud Platform (GCP) primitives to achieve a highly scalable, serverless architecture.
graph TD
A[User / Browser] -->|HTTPS POST /analyze| B[Google Cloud Run]
B --> C[FastAPI Application]
C --> D[Google Vertex AI API]
D --> E[gemini-2.5-flash-lite<br/>Stage 1: Ingestion & Classification]
D --> F[gemini-2.5-flash-lite<br/>Stage 2: Adversarial Simulation]
D --> G[gemini-2.5-flash-lite<br/>Stage 3: Linguistic Translation]
G --> C
C -->|Validated JSON Schema| A
| GCP Primitive | Architectural Role | Technical Rationale |
|---|---|---|
| Vertex AI Generative AI | Orchestration of the 3-stage LLM inference pipeline. | Provides enterprise-grade rate limits, deterministic parameter control, and seamless ADC integration. |
| Gemini 2.5 Flash Lite | Core intelligence engine. | Delivers optimal latency-to-performance ratio specifically tailored for high-throughput text classification. |
| Google Cloud Run | Serverless application hosting environment. | Enables scale-to-zero operational economics and autonomous request-based auto-scaling without container orchestration overhead. |
| Google Cloud Build | CI/CD containerization pipeline. | Abstracts image compilation during gcloud run deploy workflows. |
| Application Default Credentials (ADC) | Secure Identity and Access Management (IAM). | Enforces zero-trust authentication protocols, eliminating the risk of hardcoded secret leakage. |
LexGuard avoids superficial API wrapping by utilizing advanced model controls:
- Granular GenerationConfig: Enforcing
response_mime_type="application/json"at the SDK level to guarantee parseable outputs. - Parametric Determinism: Setting
temperature=0.0to eliminate probabilistic variance during legal evaluation.
| Strategic Enhancement | Target GCP Service | System Benefit |
|---|---|---|
| Distributed Caching Layer | Cloud Memorystore (Redis) | Mitigation of redundant inference calls via document hash collisions, drastically reducing operating costs. |
| Persistent State Management | Cloud Firestore | Provisioning NoSQL storage for historical user analytics and report retrieval. |
| Optical Character Recognition (OCR) | Cloud Vision API | Expanding the ingestion pipeline to support rasterized, non-selectable PDF images (common in legacy legal scans). |
| Semantic Vectorization | Vertex AI Embeddings | Enabling high-dimensional vector search to map novel clauses against known predatory archetypes. |
| Asynchronous Streaming | Vertex AI Streaming API | Delivering real-time Server-Sent Events (SSE) to lower perceived time-to-first-byte (TTFB) on the client. |
| Identity Management | Firebase Authentication | Enabling secure, JWT-based session management for personalized legal dashboards. |
Rather than relying on monolithic, zero-shot LLM inference—which suffers from catastrophic context forgetting and probabilistic drift—LexGuard orchestrates a 3-stage deterministic pipeline (temperature=0.0) enforcing strict state boundaries:
- Stage 1 (Heuristic Ingestion & Classification): Extracts unstructured text, filters ambient noise, isolates discrete contractual clauses, and maps them to predefined legal taxonomies (e.g., Arbitration, Data Privacy, Termination).
- Stage 2 (Adversarial Analysis & Pattern Matching): Executes a rigorous evaluation of classified clauses, computing algorithmic risk scores, detecting known predatory architectures (e.g., forced waivers, roach motels), and quantifying the Power Imbalance Metric.
- Stage 3 (Consequence Simulation & Localization): Generates projective simulations of worst-case contractual enforcement, synthesizes balanced consumer-centric alternatives, and executes native linguistic translation across the entire payload.
LexGuard implements a unified, format-agnostic parser relying on high-performance magic-byte validation rather than fragile MIME-type headers:
| Supported Format | Hex/Byte Signature Detection |
|---|---|
| Portable Document Format (PDF) | Magic bytes: b'%PDF' |
| Office Open XML (DOCX) | Magic bytes: b'PK' (ZIP archive signature) |
| UTF-8 Plain Text | Whitespace normalization heuristic: " " in document |
To democratize legal comprehension, LexGuard enforces native translation across both the UI layer and the generated JSON schema without relying on intermediary translation APIs:
| Supported Locale | ISO 639-1 Code |
|---|---|
| English (Default) | en |
| Hindi (हिंदी) | hi |
| Kannada (ಕನ್ನಡ) | kn |
| Tamil (தமிழ்) | ta |
| Telugu (తెలుగు) | te |
| Marathi (मराठी) | mr |
LexGuard extends its operational boundary directly into the client's browser environment via a Manifest V3 extension, providing:
- Ephemeral DOM Interception: Captures active page contexts prior to user consent events.
- Asynchronous State Management: Non-blocking inference polling with status synchronization.
- Inline Risk Visualization: Renders aggregate scores, clause-level heuristics, and language toggles dynamically.
┌─────────────────────────────────────────────────────────────┐
│ USER LAYER │
│ │
│ React Frontend (Vite) Chrome Extension (MV3) │
│ ├── Home.jsx ├── popup.html │
│ └── Analyze.jsx └── manifest.json │
└──────────────────────┬──────────────────────┬──────────────┘
│ HTTP POST /analyze │
┌──────────────────────▼──────────────────────▼──────────────┐
│ CLOUD RUN (API LAYER) │
│ │
│ main.py (FastAPI) │
│ ├── POST /analyze → parser.py → agents/pipeline.py │
│ └── GET / → static/index.html │
└──────────────────────────────────┬─────────────────────────┘
│ Vertex AI API
┌──────────────────────────────────▼─────────────────────────┐
│ VERTEX AI LAYER │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Agent 1+2 │ → │ Agent 3+4 │ → │ Agent 5-7 │ │
│ │ Parser & │ │ Adversary & │ │ Simulator & │ │
│ │ Classifier │ │ Benchmarker │ │ Translator │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ temp=0.0 temp=0.0 temp=0.0 │
└─────────────────────────────────────────────────────────────┘
graph TD
A[Contract Document] -->|Upload PDF/DOCX or Paste Text| B(FastAPI Backend)
B -->|Clean Text Heuristics| C[parser.py]
subgraph 7-Agent Pipeline [Google Vertex AI - gemini-2.5-flash-lite]
C -->|Call 1| D[Clause Extractor & Domain Classifier]
D -->|JSON Output| E[Call 2: Risk Evaluator & Dark Pattern Detector]
E -->|JSON Output| F[Call 3: Consequence Simulator & Indic Translator]
end
F -->|Deterministic JSON| G[Unified API Response]
G -->|Dynamic Map| H[React Dashboard]
G -->|Chrome Event Hook| I[LexGuard Extension Popup]
style 7-Agent Pipeline fill:#1e293b,stroke:#3b82f6,stroke-width:2px;
Input Vector: Unstructured UTF-8 contractual text
Output Schema: JSON Array of discrete clauses mapped to legal domain enumerations.
Supported Semantic Taxonomies:
| Classification Node | Lexical Indicators & Scope |
|---|---|
arbitration |
Class-action waivers, forced binding arbitration, venue restrictions |
privacy |
PII retention, third-party data syndication, telemetry collection |
financial |
Autorenewal architectures, latent fee structures, price variance rights |
termination |
Asymmetric cancellation conditions, punitive notice periods |
employment |
Non-compete covenants, moonlighting restrictions |
ip |
Broad intellectual property assignments, perpetual licensing |
other |
Fallback classification for anomalous structures |
Heuristic Methodology: The intelligence layer is explicitly constrained via system prompts to execute Adversarial Intent Simulation. Rather than benign summarization, the model evaluates theoretical maximum exploitation vectors available to the drafting party.
| Schema Injections | Technical Description |
|---|---|
risk_level |
Categorical severity index (HIGH, MEDIUM, LOW) |
why_flagged |
Concisely articulated vulnerability rationale |
what_it_means |
Pragmatic translation of the theoretical attack vector |
fair_version |
Generation of a statistically normalized, equitable equivalent |
dark_pattern |
Boolean flag (true/false) denoting UI/UX manipulation |
dark_pattern_type |
Specific taxonomy mapping (e.g., forced_consent, hidden_renewal) |
Full output schema:
{
"document_type": "subscription_tos",
"overall_risk_score": 82,
"safe_to_sign": false,
"power_imbalance": "Company: 88% / You: 12%",
"summary": "This agreement gives the company unilateral control...",
"flagged_clauses": [
{
"clause_text": "...",
"clause_type": "arbitration",
"risk_level": "HIGH",
"confidence": 0.97,
"why_flagged": "Eliminates your right to sue in court",
"what_it_means": "You cannot join class-action lawsuits",
"fair_version": "Disputes may be resolved by either arbitration or court...",
"consequence": "You lose all legal recourse against the company",
"financial_impact": "Arbitration fees may exceed the value of your claim",
"dark_pattern": true,
"dark_pattern_type": "forced_consent",
"plain_english": "They won't let you take them to court. Ever.",
"translated_explanation": "...(in selected language)...",
"translated_consequence": "...",
"translated_fair_version": "...",
"negotiation_tip": "Ask for a mutual arbitration clause with a $10K threshold"
}
],
"red_flags_count": 4,
"dark_patterns_count": 2,
"negotiation_summary": "Focus your negotiation on clauses 1, 3, and 5...",
"translated_summary": "..."
}| Factor | Value |
|---|---|
| Model | gemini-2.5-flash-lite |
| Input token price | $0.0375 / 1M tokens |
| Typical document size | ~5,000 tokens |
| Pipeline calls | 3 (sequential) |
| Cost per full analysis | ~$0.0002 |
| Scans per $5 GCP credit | ~19,000 |
- Vertex AI Migration: Migrated fully from Google AI Studio to Vertex AI for stable pay-as-you-go enterprise quotas.
- Deterministic Output:
temperature=0.0ensures 100% reproducible, verifiable risk analysis.
Request:
{
"document": "string (plain text or base64-encoded PDF/DOCX)",
"language": "en | hi | kn | ta | te | mr",
"document_type": "optional hint string"
}Response: Full schema as shown in Stage 3 above.
Error:
{ "error": "descriptive error message" }{ "status": "ok" }# 1. Clone the Repository
git clone https://github.com/chhhee10/Prompt-Wars.git
cd Prompt-Wars
# 2. Set up Virtual Environment & Dependencies
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# 3. Configure Environment Variables (create .env in root)
# GCP_PROJECT="gen-lang-client-0301002559"
# GCP_LOCATION="us-central1"
# 4. Authenticate GCP Application Default Credentials (ADC)
gcloud auth application-default login
# 5. Run the Local Server
uvicorn main:app --reloadOpen your browser to http://127.0.0.1:8000.
- Open Google Chrome →
chrome://extensions/ - Enable Developer mode (toggle in top-right corner).
- Click Load unpacked in the top-left.
- Select the
extension/folder inside the repository. - Pin the LexGuard extension for instant contract interception!
One-command deploy to Google Cloud Run:
./google-cloud-sdk/bin/gcloud run deploy lexguard \
--source . \
--region us-central1 \
--allow-unauthenticatedPrompt-Wars/
├── main.py ← FastAPI app: routing + request/response
├── parser.py ← Multi-format extractor (PDF, DOCX, TXT)
├── agents/
│ └── pipeline.py ← 3-stage Vertex AI orchestration pipeline
├── static/
│ └── index.html ← Fully-localized consumer dashboard UI
├── extension/ ← Chrome Extension (MV3)
│ ├── manifest.json
│ └── popup.html
├── tests/ ← 6 test files covering API + models + formats
└── requirements.txt ← Pinned Python dependencies
Beyond the web dashboard, LexGuard ships a Manifest V3 Chrome Extension that brings contract intelligence directly into the browser — a differentiating feature that eliminates the need for users to copy-paste or upload documents manually.
When the user clicks the extension icon on any webpage, the popup reads the active tab's visible DOM text via chrome.scripting.executeScript, sends it as a POST request to the LexGuard /analyze API (with the user's selected Indic language), and renders the full risk verdict — score, flagged clauses, and dark pattern warnings — inline within the popup itself.
This means a user browsing a Terms of Service page can get a complete risk analysis without ever leaving the page.
| Aspect | Implementation |
|---|---|
| Manifest version | V3 (latest Chrome standard) |
| Permissions | activeTab, scripting |
| DOM extraction | chrome.scripting.executeScript → document.body.innerText |
| API communication | fetch() POST to /analyze endpoint with JSON body |
| Language support | Dropdown in popup header; language code passed to backend for native Indic translation |
| CORS handling | Background service worker proxies requests to bypass strict CORS blocks |
| UI | Self-contained popup.html (HTML + CSS + JS in a single file, no build step) |
extension/
├── manifest.json ← MV3 config: permissions, popup entry point
└── popup.html ← Complete self-contained UI
- Open Chrome →
chrome://extensions/ - Enable Developer mode (toggle, top-right)
- Click Load unpacked
- Select the
extension/folder from this repository - Pin the LexGuard icon in your toolbar
LexGuard is an AI-powered legal intelligence tool designed to assist consumers in understanding standard terms. It does not constitute formal legal advice.