File size: 8,585 Bytes
cdee1d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
ClauseGuard API — HuggingFace Spaces Deployment
Loads Legal-BERT from Hub, serves clause classification.
"""

import os
import time
import re
from contextlib import asynccontextmanager
from typing import Optional

import httpx
import numpy as np
from fastapi import FastAPI, HTTPException, Depends, Header
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field

# ─── Config ───
HUB_MODEL_ID = os.environ.get("HUB_MODEL_ID", "gaurv007/clauseguard-legal-bert")
SUPABASE_URL = os.environ.get("SUPABASE_URL", "")
SUPABASE_SERVICE_KEY = os.environ.get("SUPABASE_SERVICE_ROLE_KEY", "")

LABEL_NAMES = [
    "Limitation of liability", "Unilateral termination", "Unilateral change",
    "Content removal", "Contract by using", "Choice of law", "Jurisdiction", "Arbitration",
]

LABEL_DESCRIPTIONS = {
    "Limitation of liability": "Company limits or excludes liability for losses, data breaches, or service failures.",
    "Unilateral termination": "Company can terminate your account at any time without reason.",
    "Unilateral change": "Company can change terms at any time without your consent.",
    "Content removal": "Company can delete your content without notice or justification.",
    "Contract by using": "You are bound to the contract simply by using the service.",
    "Choice of law": "Governing law may differ from your country, reducing your legal protections.",
    "Jurisdiction": "Disputes must be resolved in a jurisdiction that may disadvantage you.",
    "Arbitration": "Forces disputes to arbitration instead of court. You waive your right to sue.",
}

SEVERITY_MAP = {
    "Limitation of liability": "HIGH", "Unilateral termination": "HIGH", "Arbitration": "HIGH",
    "Unilateral change": "MEDIUM", "Content removal": "MEDIUM", "Choice of law": "MEDIUM",
    "Jurisdiction": "MEDIUM", "Contract by using": "LOW",
}

LEGAL_BASIS = {
    "Arbitration": "EU Directive 93/13/EEC Art. 3; CFPB arbitration rule (US).",
    "Unilateral change": "EU Directive 93/13/EEC Annex 1(j) — unilateral alteration.",
    "Content removal": "EU Digital Services Act Art. 17 — statement of reasons required.",
    "Jurisdiction": "EU Regulation 1215/2012 Art. 18 — consumer domicile prevails.",
    "Choice of law": "EU Regulation 593/2008 Art. 6 — consumer protection of habitual residence.",
    "Limitation of liability": "EU Directive 93/13/EEC Annex 1(a) — excluding statutory rights.",
    "Unilateral termination": "EU Directive 93/13/EEC Annex 1(f)(g) — termination without notice.",
    "Contract by using": "EU Directive 2011/83/EU Art. 8 — active consent required.",
}

# ─── ML Model ───
classifier = None

def load_model():
    global classifier
    try:
        from transformers import pipeline
        print(f"Loading model from Hub: {HUB_MODEL_ID}")
        classifier = pipeline("text-classification", model=HUB_MODEL_ID, top_k=None, device=-1)
        print(f"Model loaded successfully")
    except Exception as e:
        print(f"Model load failed: {e} — using regex fallback")

# ─── Regex fallback ───
PATTERNS = {
    0: [r"not liable", r"shall not be (liable|responsible)", r"in no event.*liable", r"limitation of liability", r"without warranty", r"disclaim"],
    1: [r"terminat.*at any time", r"suspend.*account.*without", r"we may (terminat|suspend|discontinu)", r"right to (terminat|suspend)"],
    2: [r"sole discretion", r"reserves? the right to (modify|change|update|amend)", r"at any time.*without (prior )?notice", r"we may (modify|change|update)"],
    3: [r"remove.*content.*without", r"right to remove", r"we may.*remove"],
    4: [r"by (using|accessing).*you agree", r"continued use.*constitutes? acceptance"],
    5: [r"governed by.*laws? of", r"shall be governed", r"laws of the state of"],
    6: [r"exclusive jurisdiction", r"courts? of.*(california|delaware|new york|ireland|england)", r"submit to.*jurisdiction"],
    7: [r"arbitrat", r"binding arbitration", r"waive.*right.*court", r"class action waiver"],
}

def classify_clause(text: str) -> list[dict]:
    if classifier:
        try:
            preds = classifier(text, truncation=True, max_length=512)
            items = preds[0] if isinstance(preds[0], list) else preds
            return [
                {"name": p["label"], "severity": SEVERITY_MAP.get(p["label"], "MEDIUM"),
                 "description": LABEL_DESCRIPTIONS.get(p["label"], ""), "confidence": round(p["score"], 3)}
                for p in items if p["score"] > 0.5 and p["label"] in LABEL_DESCRIPTIONS
            ]
        except Exception:
            pass

    results = []
    text_lower = text.lower()
    for lid, pats in PATTERNS.items():
        for p in pats:
            if re.search(p, text_lower):
                name = LABEL_NAMES[lid]
                results.append({"name": name, "severity": SEVERITY_MAP[name],
                                "description": LABEL_DESCRIPTIONS[name], "confidence": 0.7})
                break
    return results

# ─── Auth (simplified for HF Spaces — no Supabase dependency required) ───
async def get_optional_user(authorization: Optional[str] = Header(None)) -> Optional[dict]:
    if not authorization:
        return None
    # In production, validate JWT here. For now, extract user ID from token claims.
    return None

# ─── Supabase helpers ───
async def supabase_insert(table: str, data: dict):
    if not SUPABASE_URL or not SUPABASE_SERVICE_KEY:
        return
    async with httpx.AsyncClient() as client:
        await client.post(
            f"{SUPABASE_URL}/rest/v1/{table}", json=data,
            headers={"apikey": SUPABASE_SERVICE_KEY, "Authorization": f"Bearer {SUPABASE_SERVICE_KEY}",
                      "Content-Type": "application/json", "Prefer": "return=minimal"},
        )

# ─── Models ───
class AnalyzeRequest(BaseModel):
    clauses: list[str] = Field(..., min_length=1, max_length=500)
    source_url: Optional[str] = None

class AnalyzeResponse(BaseModel):
    risk_score: int
    grade: str
    total_clauses: int
    flagged_count: int
    results: list[dict]
    model: str
    latency_ms: int

class ExplainRequest(BaseModel):
    clause: str = Field(..., min_length=10, max_length=2000)
    category: str

class ExplainResponse(BaseModel):
    clause: str
    category: str
    explanation: str
    legal_basis: str
    recommendation: str

# ─── App ───
@asynccontextmanager
async def lifespan(app: FastAPI):
    load_model()
    yield

app = FastAPI(
    title="ClauseGuard API",
    description="AI-powered unfair clause detection. Send contract clauses, get risk scores.",
    version="1.0.0",
    lifespan=lifespan,
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
async def root():
    return {
        "name": "ClauseGuard API",
        "status": "running",
        "model": "ml" if classifier else "regex",
        "docs": "/docs",
    }

@app.get("/health")
async def health():
    return {"status": "ok", "model": "ml" if classifier else "regex"}

@app.post("/api/analyze", response_model=AnalyzeResponse)
async def analyze(req: AnalyzeRequest):
    start = time.time()

    results = [{"text": c, "categories": classify_clause(c)} for c in req.clauses]
    flagged = [r for r in results if r["categories"]]

    sev = {"HIGH": 0, "MEDIUM": 0, "LOW": 0}
    for r in flagged:
        for c in r["categories"]:
            sev[c.get("severity", "LOW")] += 1

    total = len(req.clauses)
    risk = min(100, round((sev["HIGH"] * 20 + sev["MEDIUM"] * 10 + sev["LOW"] * 5) / max(1, total) * 100))
    grade = "F" if risk >= 60 else "D" if risk >= 40 else "C" if risk >= 20 else "B" if risk >= 10 else "A"
    latency = int((time.time() - start) * 1000)

    return AnalyzeResponse(
        risk_score=risk, grade=grade, total_clauses=total,
        flagged_count=len(flagged), results=results,
        model="ml" if classifier else "regex", latency_ms=latency,
    )

@app.post("/api/explain", response_model=ExplainResponse)
async def explain(req: ExplainRequest):
    desc = LABEL_DESCRIPTIONS.get(req.category, "Unknown category.")
    legal = LEGAL_BASIS.get(req.category, "Consult local consumer protection laws.")
    return ExplainResponse(
        clause=req.clause, category=req.category,
        explanation=desc, legal_basis=legal,
        recommendation="Review this clause carefully. Consider negotiating or seeking legal advice.",
    )