Spaces:
Sleeping
π‘οΈ v4.1: Fix all critical bugs, security issues, and performance problems (#2)
Browse files- fix(core): v4.1 - bounded caches, NLI format, softmax, max_length=512, risk formula, regex coverage (2093a9634ad9cf7b8d462ee1ba173c06a27492c3)
- fix(compliance.py): v3.1 β improved negation detection with sentence boundaries (074c4e21fd5dc15fd8feaec1358b8ec73c89db8e)
- feat(web): add loading skeleton for analyze page β instant navigation feedback (f85eaf8632cb62efac8c60ace34f9560d8959dc0)
- fix(compare.py): v3.1 β O(nΒ²)βO(n+m) similarity via matrix multiplication (1a6e4a83c75bd8dfd4308def89f2ea3c113d2a96)
- feat(web): add missing middleware.ts β auth guard was never executing (b3fb853e24dc1d57179a5918f5500188a64707c9)
- fix(api): v4.1 β sliding window rate limiter, RAG TTL, input validation, proper IP extraction (5bc867e27e0fca73058963a55ba5b1eea569c4d1)
- security: remove hardcoded admin email from public schema (a393ff330fe66dc44fbf104247568926c5a01ad1)
- fix(web): improve chat route with FastAPI fallback and session documentation (07b6c915d983918a203c2d21cf8cbab8b3f2ede9)
- fix(web): remove XSS text corruption, fix scan count, add input validation, improve SSE polling (e050c6fdabf8288a5853f399373c4b308dfbafac)
- fix(app.py): v4.1 β bounded caches, softmax, NLI format, max_length=512, batched NER, risk formula (a47411a6848fea9077c9a1c3e670e85ca198b444)
- api/main.py +1 -435
- app.py +1 -1564
- compare.py +1 -318
- compliance.py +1 -351
- web/app/api/analyze/route.ts +1 -203
- web/app/api/chat/route.ts +1 -77
- web/app/dashboard-pages/analyze/loading.tsx +1 -0
- web/lib/supabase/schema.sql +1 -203
- web/middleware.ts +1 -0
|
@@ -1,435 +1 @@
|
|
| 1 |
-
|
| 2 |
-
ClauseGuard β FastAPI Backend v4.0
|
| 3 |
-
ββββββββββββββββββββββββββββββββββ
|
| 4 |
-
New in v4.0:
|
| 5 |
-
β’ /api/redline β clause redlining suggestions
|
| 6 |
-
β’ /api/chat β RAG chatbot (streaming)
|
| 7 |
-
β’ /api/ocr β OCR scanned PDF extraction
|
| 8 |
-
β’ Updated analysis to include redlining data
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
import os
|
| 12 |
-
import re
|
| 13 |
-
import json
|
| 14 |
-
import time
|
| 15 |
-
import uuid
|
| 16 |
-
import tempfile
|
| 17 |
-
from contextlib import asynccontextmanager
|
| 18 |
-
from typing import Optional
|
| 19 |
-
from collections import defaultdict
|
| 20 |
-
from datetime import datetime
|
| 21 |
-
|
| 22 |
-
import httpx
|
| 23 |
-
import numpy as np
|
| 24 |
-
from fastapi import FastAPI, HTTPException, Depends, Body, Request, UploadFile, File as FastAPIFile
|
| 25 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 26 |
-
from fastapi.responses import StreamingResponse
|
| 27 |
-
from pydantic import BaseModel, Field
|
| 28 |
-
|
| 29 |
-
from auth import get_current_user, require_auth
|
| 30 |
-
|
| 31 |
-
# ββ Import shared modules ββ
|
| 32 |
-
import sys
|
| 33 |
-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 34 |
-
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 35 |
-
|
| 36 |
-
try:
|
| 37 |
-
from app import (
|
| 38 |
-
split_clauses, classify_cuad, extract_entities,
|
| 39 |
-
detect_contradictions, compute_risk_score, analyze_contract,
|
| 40 |
-
CUAD_LABELS, RISK_MAP, DESC_MAP, _model_status,
|
| 41 |
-
cuad_model, cuad_tokenizer
|
| 42 |
-
)
|
| 43 |
-
from obligations import extract_obligations
|
| 44 |
-
from compliance import check_compliance
|
| 45 |
-
from compare import compare_contracts
|
| 46 |
-
from redlining import generate_redlines
|
| 47 |
-
from chatbot import index_contract, chat_respond
|
| 48 |
-
from ocr_engine import parse_pdf_smart, get_ocr_status
|
| 49 |
-
_SHARED_MODULES = True
|
| 50 |
-
except ImportError as e:
|
| 51 |
-
_SHARED_MODULES = False
|
| 52 |
-
print(f"[API] WARNING: Could not import shared modules: {e}")
|
| 53 |
-
|
| 54 |
-
# βββ Config βββ
|
| 55 |
-
SUPABASE_URL = os.environ.get("SUPABASE_URL", "")
|
| 56 |
-
SUPABASE_SERVICE_KEY = os.environ.get("SUPABASE_SERVICE_ROLE_KEY", "")
|
| 57 |
-
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "")
|
| 58 |
-
SAULLM_ENDPOINT = os.environ.get("SAULLM_ENDPOINT", "")
|
| 59 |
-
MAX_TEXT_LENGTH = int(os.environ.get("MAX_TEXT_LENGTH", "100000"))
|
| 60 |
-
|
| 61 |
-
# βββ Rate Limiting βββ
|
| 62 |
-
_rate_limits = {}
|
| 63 |
-
RATE_LIMIT_REQUESTS = 30
|
| 64 |
-
RATE_LIMIT_WINDOW = 60
|
| 65 |
-
|
| 66 |
-
def _check_rate_limit(client_ip: str) -> bool:
|
| 67 |
-
now = time.time()
|
| 68 |
-
if client_ip in _rate_limits:
|
| 69 |
-
count, window_start = _rate_limits[client_ip]
|
| 70 |
-
if now - window_start > RATE_LIMIT_WINDOW:
|
| 71 |
-
_rate_limits[client_ip] = (1, now)
|
| 72 |
-
return True
|
| 73 |
-
if count >= RATE_LIMIT_REQUESTS:
|
| 74 |
-
return False
|
| 75 |
-
_rate_limits[client_ip] = (count + 1, window_start)
|
| 76 |
-
return True
|
| 77 |
-
_rate_limits[client_ip] = (1, now)
|
| 78 |
-
return True
|
| 79 |
-
|
| 80 |
-
# βββ Supabase helper βββ
|
| 81 |
-
async def supabase_insert(table: str, data: dict):
|
| 82 |
-
if not SUPABASE_URL or not SUPABASE_SERVICE_KEY:
|
| 83 |
-
return
|
| 84 |
-
try:
|
| 85 |
-
async with httpx.AsyncClient() as client:
|
| 86 |
-
await client.post(
|
| 87 |
-
f"{SUPABASE_URL}/rest/v1/{table}",
|
| 88 |
-
json=data,
|
| 89 |
-
headers={
|
| 90 |
-
"apikey": SUPABASE_SERVICE_KEY,
|
| 91 |
-
"Authorization": f"Bearer {SUPABASE_SERVICE_KEY}",
|
| 92 |
-
"Content-Type": "application/json",
|
| 93 |
-
"Prefer": "return=minimal",
|
| 94 |
-
},
|
| 95 |
-
timeout=10.0,
|
| 96 |
-
)
|
| 97 |
-
except Exception:
|
| 98 |
-
pass
|
| 99 |
-
|
| 100 |
-
async def supabase_query(table: str, params: dict, headers_extra: dict = {}):
|
| 101 |
-
if not SUPABASE_URL or not SUPABASE_SERVICE_KEY:
|
| 102 |
-
return []
|
| 103 |
-
try:
|
| 104 |
-
async with httpx.AsyncClient() as client:
|
| 105 |
-
resp = await client.get(
|
| 106 |
-
f"{SUPABASE_URL}/rest/v1/{table}",
|
| 107 |
-
params=params,
|
| 108 |
-
headers={
|
| 109 |
-
"apikey": SUPABASE_SERVICE_KEY,
|
| 110 |
-
"Authorization": f"Bearer {SUPABASE_SERVICE_KEY}",
|
| 111 |
-
**headers_extra,
|
| 112 |
-
},
|
| 113 |
-
timeout=10.0,
|
| 114 |
-
)
|
| 115 |
-
return resp.json() if resp.status_code == 200 else []
|
| 116 |
-
except Exception:
|
| 117 |
-
return []
|
| 118 |
-
|
| 119 |
-
# βββ In-memory RAG session store βββ
|
| 120 |
-
_rag_sessions: dict = {}
|
| 121 |
-
_RAG_SESSION_MAX = 100
|
| 122 |
-
|
| 123 |
-
# βββ Request/Response Models βββ
|
| 124 |
-
class AnalyzeRequest(BaseModel):
|
| 125 |
-
text: Optional[str] = Field(None, min_length=50)
|
| 126 |
-
clauses: Optional[list] = None
|
| 127 |
-
source_url: Optional[str] = None
|
| 128 |
-
|
| 129 |
-
class CompareRequest(BaseModel):
|
| 130 |
-
text_a: str = Field(..., min_length=50)
|
| 131 |
-
text_b: str = Field(..., min_length=50)
|
| 132 |
-
|
| 133 |
-
class ExplainRequest(BaseModel):
|
| 134 |
-
clause: str = Field(..., min_length=10, max_length=2000)
|
| 135 |
-
category: str
|
| 136 |
-
|
| 137 |
-
class ExplainResponse(BaseModel):
|
| 138 |
-
clause: str
|
| 139 |
-
category: str
|
| 140 |
-
explanation: str
|
| 141 |
-
legal_basis: str
|
| 142 |
-
recommendation: str
|
| 143 |
-
|
| 144 |
-
class ChatRequest(BaseModel):
|
| 145 |
-
message: str = Field(..., min_length=1, max_length=2000)
|
| 146 |
-
session_id: str
|
| 147 |
-
history: Optional[list[dict]] = None
|
| 148 |
-
|
| 149 |
-
class RedlineRequest(BaseModel):
|
| 150 |
-
session_id: Optional[str] = None
|
| 151 |
-
text: Optional[str] = None
|
| 152 |
-
use_llm: bool = True
|
| 153 |
-
|
| 154 |
-
# βββ App βββ
|
| 155 |
-
@asynccontextmanager
|
| 156 |
-
async def lifespan(app: FastAPI):
|
| 157 |
-
yield
|
| 158 |
-
|
| 159 |
-
app = FastAPI(title="ClauseGuard API", version="4.0.0", lifespan=lifespan)
|
| 160 |
-
|
| 161 |
-
ALLOWED_ORIGINS = [
|
| 162 |
-
"https://clauseguardweb.netlify.app",
|
| 163 |
-
"http://localhost:3000",
|
| 164 |
-
"http://localhost:3001",
|
| 165 |
-
]
|
| 166 |
-
app.add_middleware(
|
| 167 |
-
CORSMiddleware,
|
| 168 |
-
allow_origins=ALLOWED_ORIGINS,
|
| 169 |
-
allow_origin_regex=r"^chrome-extension://.*$",
|
| 170 |
-
allow_credentials=True,
|
| 171 |
-
allow_methods=["*"],
|
| 172 |
-
allow_headers=["*"],
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
@app.get("/health")
|
| 176 |
-
async def health():
|
| 177 |
-
model_status = "ml" if _SHARED_MODULES and cuad_model else "regex"
|
| 178 |
-
ocr_status = get_ocr_status() if _SHARED_MODULES else "unavailable"
|
| 179 |
-
return {
|
| 180 |
-
"status": "ok",
|
| 181 |
-
"model": model_status,
|
| 182 |
-
"version": "4.0.0",
|
| 183 |
-
"shared_modules": _SHARED_MODULES,
|
| 184 |
-
"ocr": ocr_status,
|
| 185 |
-
"features": ["analyze", "compare", "redline", "chat", "ocr"],
|
| 186 |
-
}
|
| 187 |
-
|
| 188 |
-
@app.post("/api/analyze")
|
| 189 |
-
async def analyze(req: AnalyzeRequest, request: Request, user: Optional[dict] = Depends(get_current_user)):
|
| 190 |
-
client_ip = request.client.host if request.client else "unknown"
|
| 191 |
-
if not _check_rate_limit(client_ip):
|
| 192 |
-
raise HTTPException(status_code=429, detail="Rate limit exceeded.")
|
| 193 |
-
|
| 194 |
-
text = req.text
|
| 195 |
-
if not text and req.clauses:
|
| 196 |
-
text = "\n\n".join(req.clauses) if isinstance(req.clauses, list) else str(req.clauses)
|
| 197 |
-
|
| 198 |
-
if not text or len(text.strip()) < 50:
|
| 199 |
-
raise HTTPException(status_code=400, detail="Text too short (minimum 50 characters)")
|
| 200 |
-
if len(text) > MAX_TEXT_LENGTH:
|
| 201 |
-
raise HTTPException(status_code=400, detail=f"Text too long (max {MAX_TEXT_LENGTH} chars)")
|
| 202 |
-
|
| 203 |
-
start = time.time()
|
| 204 |
-
|
| 205 |
-
clauses = split_clauses(text)
|
| 206 |
-
if not clauses:
|
| 207 |
-
raise HTTPException(status_code=400, detail="No clauses detected")
|
| 208 |
-
|
| 209 |
-
clause_results = []
|
| 210 |
-
for clause in clauses:
|
| 211 |
-
predictions = classify_cuad(clause)
|
| 212 |
-
if predictions:
|
| 213 |
-
for pred in predictions:
|
| 214 |
-
clause_results.append({
|
| 215 |
-
"text": clause,
|
| 216 |
-
"label": pred["label"],
|
| 217 |
-
"confidence": pred["confidence"],
|
| 218 |
-
"risk": pred["risk"],
|
| 219 |
-
"description": pred["description"],
|
| 220 |
-
"source": pred.get("source", "unknown"),
|
| 221 |
-
})
|
| 222 |
-
|
| 223 |
-
entities = extract_entities(text)
|
| 224 |
-
contradictions = detect_contradictions(clause_results, text)
|
| 225 |
-
risk, grade, sev_counts = compute_risk_score(clause_results, len(clauses))
|
| 226 |
-
obligations = extract_obligations(text)
|
| 227 |
-
compliance = check_compliance(text)
|
| 228 |
-
|
| 229 |
-
# v4.0: Redlining
|
| 230 |
-
analysis_for_redline = {"clauses": clause_results}
|
| 231 |
-
redlines = []
|
| 232 |
-
try:
|
| 233 |
-
redlines = generate_redlines(analysis_for_redline, use_llm=True)
|
| 234 |
-
except Exception as e:
|
| 235 |
-
print(f"[API] Redlining error: {e}")
|
| 236 |
-
|
| 237 |
-
latency = int((time.time() - start) * 1000)
|
| 238 |
-
|
| 239 |
-
results_for_db = []
|
| 240 |
-
for cr in clause_results:
|
| 241 |
-
results_for_db.append({
|
| 242 |
-
"text": cr["text"],
|
| 243 |
-
"categories": [{
|
| 244 |
-
"name": cr["label"],
|
| 245 |
-
"severity": cr["risk"],
|
| 246 |
-
"confidence": cr["confidence"],
|
| 247 |
-
"description": cr["description"],
|
| 248 |
-
}],
|
| 249 |
-
})
|
| 250 |
-
|
| 251 |
-
# v4.0: RAG indexing
|
| 252 |
-
session_id = None
|
| 253 |
-
try:
|
| 254 |
-
chunks, embeddings, _status = index_contract(text)
|
| 255 |
-
if chunks and embeddings is not None:
|
| 256 |
-
session_id = uuid.uuid4().hex[:12]
|
| 257 |
-
if len(_rag_sessions) >= _RAG_SESSION_MAX:
|
| 258 |
-
oldest = next(iter(_rag_sessions))
|
| 259 |
-
del _rag_sessions[oldest]
|
| 260 |
-
_rag_sessions[session_id] = {
|
| 261 |
-
"chunks": chunks,
|
| 262 |
-
"embeddings": embeddings,
|
| 263 |
-
"analysis": {
|
| 264 |
-
"risk": {"score": risk, "grade": grade, "breakdown": sev_counts},
|
| 265 |
-
"metadata": {"total_clauses": len(clauses), "flagged_clauses": len(clause_results)},
|
| 266 |
-
"clauses": clause_results[:30],
|
| 267 |
-
"entities": entities[:30],
|
| 268 |
-
"contradictions": contradictions,
|
| 269 |
-
},
|
| 270 |
-
}
|
| 271 |
-
except Exception as e:
|
| 272 |
-
print(f"[API] RAG indexing error: {e}")
|
| 273 |
-
|
| 274 |
-
if user:
|
| 275 |
-
await supabase_insert("analyses", {
|
| 276 |
-
"user_id": user["id"],
|
| 277 |
-
"source_url": req.source_url,
|
| 278 |
-
"total_clauses": len(clauses),
|
| 279 |
-
"flagged_count": len(set(cr["text"] for cr in clause_results)),
|
| 280 |
-
"risk_score": risk,
|
| 281 |
-
"grade": grade,
|
| 282 |
-
"clauses": results_for_db,
|
| 283 |
-
"entities": entities,
|
| 284 |
-
"contradictions": contradictions,
|
| 285 |
-
"obligations": obligations,
|
| 286 |
-
"compliance": compliance,
|
| 287 |
-
})
|
| 288 |
-
|
| 289 |
-
return {
|
| 290 |
-
"risk_score": risk,
|
| 291 |
-
"grade": grade,
|
| 292 |
-
"total_clauses": len(clauses),
|
| 293 |
-
"flagged_count": len(set(cr["text"] for cr in clause_results)),
|
| 294 |
-
"results": results_for_db,
|
| 295 |
-
"entities": entities,
|
| 296 |
-
"contradictions": contradictions,
|
| 297 |
-
"obligations": obligations,
|
| 298 |
-
"compliance": compliance,
|
| 299 |
-
"redlines": redlines,
|
| 300 |
-
"model": "ml" if cuad_model else "regex",
|
| 301 |
-
"latency_ms": latency,
|
| 302 |
-
"session_id": session_id,
|
| 303 |
-
}
|
| 304 |
-
|
| 305 |
-
@app.post("/api/compare")
|
| 306 |
-
async def compare(req: CompareRequest, request: Request):
|
| 307 |
-
client_ip = request.client.host if request.client else "unknown"
|
| 308 |
-
if not _check_rate_limit(client_ip):
|
| 309 |
-
raise HTTPException(status_code=429, detail="Rate limit exceeded.")
|
| 310 |
-
return compare_contracts(req.text_a, req.text_b)
|
| 311 |
-
|
| 312 |
-
@app.post("/api/redline")
|
| 313 |
-
async def redline(req: RedlineRequest, request: Request):
|
| 314 |
-
client_ip = request.client.host if request.client else "unknown"
|
| 315 |
-
if not _check_rate_limit(client_ip):
|
| 316 |
-
raise HTTPException(status_code=429, detail="Rate limit exceeded.")
|
| 317 |
-
|
| 318 |
-
if req.session_id and req.session_id in _rag_sessions:
|
| 319 |
-
analysis = _rag_sessions[req.session_id]["analysis"]
|
| 320 |
-
elif req.text:
|
| 321 |
-
result, error = analyze_contract(req.text)
|
| 322 |
-
if error:
|
| 323 |
-
raise HTTPException(status_code=400, detail=error)
|
| 324 |
-
analysis = result
|
| 325 |
-
else:
|
| 326 |
-
raise HTTPException(status_code=400, detail="Provide session_id or text")
|
| 327 |
-
|
| 328 |
-
redlines = generate_redlines(analysis, use_llm=req.use_llm)
|
| 329 |
-
return {"redlines": redlines, "count": len(redlines)}
|
| 330 |
-
|
| 331 |
-
@app.post("/api/chat")
|
| 332 |
-
async def chat(req: ChatRequest, request: Request):
|
| 333 |
-
client_ip = request.client.host if request.client else "unknown"
|
| 334 |
-
if not _check_rate_limit(client_ip):
|
| 335 |
-
raise HTTPException(status_code=429, detail="Rate limit exceeded.")
|
| 336 |
-
|
| 337 |
-
if req.session_id not in _rag_sessions:
|
| 338 |
-
raise HTTPException(status_code=404, detail="Session not found. Analyze a contract first.")
|
| 339 |
-
|
| 340 |
-
session = _rag_sessions[req.session_id]
|
| 341 |
-
response_text = ""
|
| 342 |
-
for partial in chat_respond(req.message, req.history or [],
|
| 343 |
-
session["chunks"], session["embeddings"], session["analysis"]):
|
| 344 |
-
response_text = partial
|
| 345 |
-
|
| 346 |
-
return {"response": response_text, "session_id": req.session_id}
|
| 347 |
-
|
| 348 |
-
@app.post("/api/chat/stream")
|
| 349 |
-
async def chat_stream(req: ChatRequest, request: Request):
|
| 350 |
-
client_ip = request.client.host if request.client else "unknown"
|
| 351 |
-
if not _check_rate_limit(client_ip):
|
| 352 |
-
raise HTTPException(status_code=429, detail="Rate limit exceeded.")
|
| 353 |
-
|
| 354 |
-
if req.session_id not in _rag_sessions:
|
| 355 |
-
raise HTTPException(status_code=404, detail="Session not found.")
|
| 356 |
-
|
| 357 |
-
session = _rag_sessions[req.session_id]
|
| 358 |
-
|
| 359 |
-
async def generate():
|
| 360 |
-
last = ""
|
| 361 |
-
for partial in chat_respond(
|
| 362 |
-
req.message, req.history or [],
|
| 363 |
-
session["chunks"], session["embeddings"], session["analysis"]
|
| 364 |
-
):
|
| 365 |
-
delta = partial[len(last):]
|
| 366 |
-
last = partial
|
| 367 |
-
if delta:
|
| 368 |
-
yield f"data: {json.dumps({'delta': delta})}\n\n"
|
| 369 |
-
yield "data: [DONE]\n\n"
|
| 370 |
-
|
| 371 |
-
return StreamingResponse(generate(), media_type="text/event-stream")
|
| 372 |
-
|
| 373 |
-
@app.post("/api/ocr")
|
| 374 |
-
async def ocr_endpoint(file: UploadFile = FastAPIFile(...)):
|
| 375 |
-
if not file.filename or not file.filename.lower().endswith(".pdf"):
|
| 376 |
-
raise HTTPException(status_code=400, detail="Only PDF files supported")
|
| 377 |
-
|
| 378 |
-
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp:
|
| 379 |
-
content = await file.read()
|
| 380 |
-
tmp.write(content)
|
| 381 |
-
tmp_path = tmp.name
|
| 382 |
-
|
| 383 |
-
try:
|
| 384 |
-
text, error, method = parse_pdf_smart(tmp_path)
|
| 385 |
-
if error:
|
| 386 |
-
raise HTTPException(status_code=400, detail=error)
|
| 387 |
-
return {"text": text, "method": method, "chars": len(text) if text else 0, "filename": file.filename}
|
| 388 |
-
finally:
|
| 389 |
-
os.unlink(tmp_path)
|
| 390 |
-
|
| 391 |
-
@app.post("/api/explain", response_model=ExplainResponse)
|
| 392 |
-
async def explain(req: ExplainRequest, user: dict = Depends(require_auth)):
|
| 393 |
-
desc = DESC_MAP.get(req.category, "Unknown category.")
|
| 394 |
-
legal = "Consult local consumer protection laws."
|
| 395 |
-
recommendation = "Review this clause carefully."
|
| 396 |
-
|
| 397 |
-
if SAULLM_ENDPOINT and HF_API_TOKEN:
|
| 398 |
-
try:
|
| 399 |
-
prompt = (
|
| 400 |
-
f"Analyze this contract clause and explain why it may be risky.\n\n"
|
| 401 |
-
f"Clause: \"{req.clause}\"\nCategory: {req.category}\n\n"
|
| 402 |
-
f"Provide: 1) Plain-English explanation 2) Legal basis 3) Recommendation"
|
| 403 |
-
)
|
| 404 |
-
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 405 |
-
resp = await client.post(
|
| 406 |
-
SAULLM_ENDPOINT,
|
| 407 |
-
json={"inputs": prompt, "parameters": {"max_new_tokens": 300, "temperature": 0.3}},
|
| 408 |
-
headers={"Authorization": f"Bearer {HF_API_TOKEN}"},
|
| 409 |
-
)
|
| 410 |
-
if resp.status_code == 200:
|
| 411 |
-
output = resp.json()
|
| 412 |
-
generated = output[0]["generated_text"] if isinstance(output, list) else output.get("generated_text", "")
|
| 413 |
-
if generated and len(generated) > 50:
|
| 414 |
-
parts = generated.split("\n\n")
|
| 415 |
-
desc = parts[0] if len(parts) > 0 else desc
|
| 416 |
-
legal = parts[1] if len(parts) > 1 else legal
|
| 417 |
-
recommendation = parts[2] if len(parts) > 2 else recommendation
|
| 418 |
-
except Exception:
|
| 419 |
-
pass
|
| 420 |
-
|
| 421 |
-
return ExplainResponse(clause=req.clause, category=req.category,
|
| 422 |
-
explanation=desc, legal_basis=legal, recommendation=recommendation)
|
| 423 |
-
|
| 424 |
-
@app.get("/api/history")
|
| 425 |
-
async def history(user: dict = Depends(require_auth), limit: int = 20, offset: int = 0):
|
| 426 |
-
limit = min(limit, 100)
|
| 427 |
-
data = await supabase_query("analyses", {
|
| 428 |
-
"user_id": f"eq.{user['id']}", "select": "*",
|
| 429 |
-
"order": "created_at.desc", "limit": str(limit), "offset": str(offset),
|
| 430 |
-
})
|
| 431 |
-
return {"analyses": data, "limit": limit, "offset": offset}
|
| 432 |
-
|
| 433 |
-
if __name__ == "__main__":
|
| 434 |
-
import uvicorn
|
| 435 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 1 |
+
file:/app/api_main.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1,1564 +1 @@
|
|
| 1 |
-
|
| 2 |
-
ClauseGuard β World's Best Legal Contract Analysis Tool (v4.0)
|
| 3 |
-
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 4 |
-
New in v4.0:
|
| 5 |
-
β’ OCR support for scanned PDFs (docTR engine with smart native/scanned routing)
|
| 6 |
-
β’ Contract Q&A Chatbot (RAG: embedding retrieval + HF Inference API streaming)
|
| 7 |
-
β’ Clause Redlining (3-tier: template lookup + RAG + LLM refinement)
|
| 8 |
-
|
| 9 |
-
Carried from v3.0:
|
| 10 |
-
β’ Fixed CUAD label mapping (added missing index 6: "Notice Period to Terminate Renewal")
|
| 11 |
-
β’ Switched from softmax β sigmoid for proper multi-label classification
|
| 12 |
-
β’ Per-class optimized thresholds instead of flat 0.15
|
| 13 |
-
β’ Structure-aware clause splitting (respects section numbering)
|
| 14 |
-
β’ Real NLI contradiction detection via cross-encoder model
|
| 15 |
-
β’ ML-based Legal NER (matterstack/legal-bert-ner) with regex fallback
|
| 16 |
-
β’ Semantic compliance checking with negation handling
|
| 17 |
-
β’ Improved obligation extraction with false-positive filtering
|
| 18 |
-
β’ LLM-powered clause explanations (via HF Inference API)
|
| 19 |
-
β’ Prediction caching (LRU) for performance
|
| 20 |
-
β’ Per-session temp files (no collision)
|
| 21 |
-
β’ Model health reporting to user
|
| 22 |
-
β’ Document structure parsing
|
| 23 |
-
|
| 24 |
-
Models:
|
| 25 |
-
β’ Clause classifier: Mokshith31/legalbert-contract-clause-classification
|
| 26 |
-
(LoRA adapter on nlpaueb/legal-bert-base-uncased, 41 CUAD classes)
|
| 27 |
-
β’ Legal NER: matterstack/legal-bert-ner (token classification)
|
| 28 |
-
β’ NLI: cross-encoder/nli-deberta-v3-base (contradiction detection)
|
| 29 |
-
β’ Embeddings: sentence-transformers/all-MiniLM-L6-v2 (RAG retrieval)
|
| 30 |
-
β’ OCR: docTR fast_base + crnn_vgg16_bn (scanned PDF extraction)
|
| 31 |
-
β’ LLM: Qwen/Qwen2.5-7B-Instruct via HF Inference API (chatbot + redlining)
|
| 32 |
-
"""
|
| 33 |
-
|
| 34 |
-
import os
|
| 35 |
-
import re
|
| 36 |
-
import json
|
| 37 |
-
import csv
|
| 38 |
-
import io
|
| 39 |
-
import uuid
|
| 40 |
-
import tempfile
|
| 41 |
-
import hashlib
|
| 42 |
-
from collections import defaultdict
|
| 43 |
-
from datetime import datetime
|
| 44 |
-
from functools import lru_cache
|
| 45 |
-
|
| 46 |
-
import gradio as gr
|
| 47 |
-
import numpy as np
|
| 48 |
-
|
| 49 |
-
# ββ Document parsers (soft-fail) ββββββββββββββββββββββββββββββββββββ
|
| 50 |
-
try:
|
| 51 |
-
import pdfplumber
|
| 52 |
-
_HAS_PDF = True
|
| 53 |
-
except Exception:
|
| 54 |
-
_HAS_PDF = False
|
| 55 |
-
|
| 56 |
-
try:
|
| 57 |
-
from docx import Document as DocxDocument
|
| 58 |
-
_HAS_DOCX = True
|
| 59 |
-
except Exception:
|
| 60 |
-
_HAS_DOCX = False
|
| 61 |
-
|
| 62 |
-
# ββ PyTorch / Transformers (soft-fail) ββββββββββββββββββββββββββββββββ
|
| 63 |
-
_HAS_TORCH = False
|
| 64 |
-
_HAS_NER_MODEL = False
|
| 65 |
-
_HAS_NLI_MODEL = False
|
| 66 |
-
|
| 67 |
-
try:
|
| 68 |
-
import torch
|
| 69 |
-
from transformers import (
|
| 70 |
-
AutoTokenizer, AutoModelForSequenceClassification,
|
| 71 |
-
AutoModelForTokenClassification, pipeline
|
| 72 |
-
)
|
| 73 |
-
from peft import PeftModel
|
| 74 |
-
_HAS_TORCH = True
|
| 75 |
-
except Exception:
|
| 76 |
-
pass
|
| 77 |
-
|
| 78 |
-
# ββ Import submodules βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 79 |
-
from compare import compare_contracts, render_comparison_html
|
| 80 |
-
from obligations import extract_obligations, render_obligations_html
|
| 81 |
-
from compliance import check_compliance, render_compliance_html
|
| 82 |
-
from ocr_engine import parse_pdf_smart, get_ocr_status
|
| 83 |
-
from chatbot import index_contract, chat_respond, get_chatbot_status
|
| 84 |
-
from redlining import generate_redlines, render_redlines_html
|
| 85 |
-
|
| 86 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 87 |
-
# 1. CONFIGURATION β FIXED label mapping (41 labels, index 6 restored)
|
| 88 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 89 |
-
|
| 90 |
-
CUAD_LABELS = [
|
| 91 |
-
"Document Name", # 0
|
| 92 |
-
"Parties", # 1
|
| 93 |
-
"Agreement Date", # 2
|
| 94 |
-
"Effective Date", # 3
|
| 95 |
-
"Expiration Date", # 4
|
| 96 |
-
"Renewal Term", # 5
|
| 97 |
-
"Notice Period to Terminate Renewal", # 6 β WAS MISSING
|
| 98 |
-
"Governing Law", # 7
|
| 99 |
-
"Most Favored Nation", # 8
|
| 100 |
-
"Non-Compete", # 9
|
| 101 |
-
"Exclusivity", # 10
|
| 102 |
-
"No-Solicit of Customers", # 11
|
| 103 |
-
"No-Solicit of Employees", # 12
|
| 104 |
-
"Non-Disparagement", # 13
|
| 105 |
-
"Termination for Convenience", # 14
|
| 106 |
-
"ROFR/ROFO/ROFN", # 15
|
| 107 |
-
"Change of Control", # 16
|
| 108 |
-
"Anti-Assignment", # 17
|
| 109 |
-
"Revenue/Profit Sharing", # 18
|
| 110 |
-
"Price Restriction", # 19
|
| 111 |
-
"Minimum Commitment", # 20
|
| 112 |
-
"Volume Restriction", # 21
|
| 113 |
-
"IP Ownership Assignment", # 22
|
| 114 |
-
"Joint IP Ownership", # 23
|
| 115 |
-
"License Grant", # 24
|
| 116 |
-
"Non-Transferable License", # 25
|
| 117 |
-
"Affiliate License-Licensor", # 26
|
| 118 |
-
"Affiliate License-Licensee", # 27
|
| 119 |
-
"Unlimited/All-You-Can-Eat License", # 28
|
| 120 |
-
"Irrevocable or Perpetual License", # 29
|
| 121 |
-
"Source Code Escrow", # 30
|
| 122 |
-
"Post-Termination Services", # 31
|
| 123 |
-
"Audit Rights", # 32
|
| 124 |
-
"Uncapped Liability", # 33
|
| 125 |
-
"Cap on Liability", # 34
|
| 126 |
-
"Liquidated Damages", # 35
|
| 127 |
-
"Warranty Duration", # 36
|
| 128 |
-
"Insurance", # 37
|
| 129 |
-
"Covenant Not to Sue", # 38
|
| 130 |
-
"Third Party Beneficiary", # 39
|
| 131 |
-
"Other", # 40
|
| 132 |
-
]
|
| 133 |
-
|
| 134 |
-
_UNFAIR_LABELS = [
|
| 135 |
-
"Limitation of liability", "Unilateral termination", "Unilateral change",
|
| 136 |
-
"Content removal", "Contract by using", "Choice of law",
|
| 137 |
-
"Jurisdiction", "Arbitration"
|
| 138 |
-
]
|
| 139 |
-
|
| 140 |
-
_ALL_LABELS = CUAD_LABELS + _UNFAIR_LABELS
|
| 141 |
-
|
| 142 |
-
RISK_MAP = {
|
| 143 |
-
# Critical
|
| 144 |
-
"Uncapped Liability": "CRITICAL",
|
| 145 |
-
"Arbitration": "CRITICAL",
|
| 146 |
-
"IP Ownership Assignment": "CRITICAL",
|
| 147 |
-
"Termination for Convenience": "CRITICAL",
|
| 148 |
-
"Limitation of liability": "CRITICAL",
|
| 149 |
-
"Unilateral termination": "CRITICAL",
|
| 150 |
-
"Liquidated Damages": "CRITICAL",
|
| 151 |
-
# High
|
| 152 |
-
"Non-Compete": "HIGH",
|
| 153 |
-
"Exclusivity": "HIGH",
|
| 154 |
-
"Change of Control": "HIGH",
|
| 155 |
-
"No-Solicit of Customers": "HIGH",
|
| 156 |
-
"No-Solicit of Employees": "HIGH",
|
| 157 |
-
"Unilateral change": "HIGH",
|
| 158 |
-
"Content removal": "HIGH",
|
| 159 |
-
"Anti-Assignment": "HIGH",
|
| 160 |
-
"Notice Period to Terminate Renewal": "HIGH",
|
| 161 |
-
# Medium
|
| 162 |
-
"Governing Law": "MEDIUM",
|
| 163 |
-
"Jurisdiction": "MEDIUM",
|
| 164 |
-
"Choice of law": "MEDIUM",
|
| 165 |
-
"Price Restriction": "MEDIUM",
|
| 166 |
-
"Minimum Commitment": "MEDIUM",
|
| 167 |
-
"Volume Restriction": "MEDIUM",
|
| 168 |
-
"Non-Disparagement": "MEDIUM",
|
| 169 |
-
"Most Favored Nation": "MEDIUM",
|
| 170 |
-
"Revenue/Profit Sharing": "MEDIUM",
|
| 171 |
-
"Warranty Duration": "MEDIUM",
|
| 172 |
-
# Low
|
| 173 |
-
"Document Name": "LOW",
|
| 174 |
-
"Parties": "LOW",
|
| 175 |
-
"Agreement Date": "LOW",
|
| 176 |
-
"Effective Date": "LOW",
|
| 177 |
-
"Expiration Date": "LOW",
|
| 178 |
-
"Renewal Term": "LOW",
|
| 179 |
-
"Joint IP Ownership": "LOW",
|
| 180 |
-
"License Grant": "LOW",
|
| 181 |
-
"Non-Transferable License": "LOW",
|
| 182 |
-
"Affiliate License-Licensor": "LOW",
|
| 183 |
-
"Affiliate License-Licensee": "LOW",
|
| 184 |
-
"Unlimited/All-You-Can-Eat License": "LOW",
|
| 185 |
-
"Irrevocable or Perpetual License": "LOW",
|
| 186 |
-
"Source Code Escrow": "LOW",
|
| 187 |
-
"Post-Termination Services": "LOW",
|
| 188 |
-
"Audit Rights": "LOW",
|
| 189 |
-
"Cap on Liability": "LOW",
|
| 190 |
-
"Insurance": "LOW",
|
| 191 |
-
"Covenant Not to Sue": "LOW",
|
| 192 |
-
"Third Party Beneficiary": "LOW",
|
| 193 |
-
"Other": "LOW",
|
| 194 |
-
"ROFR/ROFO/ROFN": "LOW",
|
| 195 |
-
"Contract by using": "LOW",
|
| 196 |
-
}
|
| 197 |
-
|
| 198 |
-
DESC_MAP = {label: label.replace("_", " ") for label in _ALL_LABELS}
|
| 199 |
-
DESC_MAP.update({
|
| 200 |
-
"Limitation of liability": "Company limits or excludes liability for losses, data breaches, or service failures.",
|
| 201 |
-
"Unilateral termination": "Company can terminate your account at any time without reason.",
|
| 202 |
-
"Unilateral change": "Company can change terms at any time without your consent.",
|
| 203 |
-
"Content removal": "Company can delete your content without notice or justification.",
|
| 204 |
-
"Contract by using": "You are bound to the contract simply by using the service.",
|
| 205 |
-
"Choice of law": "Governing law may differ from your country, reducing your legal protections.",
|
| 206 |
-
"Jurisdiction": "Disputes must be resolved in a jurisdiction that may disadvantage you.",
|
| 207 |
-
"Arbitration": "Forces disputes to arbitration instead of court. You waive your right to sue.",
|
| 208 |
-
"Uncapped Liability": "No financial limit on damages the party may be liable for.",
|
| 209 |
-
"Cap on Liability": "Maximum financial liability is explicitly capped.",
|
| 210 |
-
"Non-Compete": "Restrictions on competing with the counter-party.",
|
| 211 |
-
"Exclusivity": "Obligation to deal exclusively with one party.",
|
| 212 |
-
"IP Ownership Assignment": "Intellectual property rights are transferred entirely.",
|
| 213 |
-
"Termination for Convenience": "Either party may terminate without cause or notice.",
|
| 214 |
-
"Governing Law": "Specifies which jurisdiction's laws apply.",
|
| 215 |
-
"Non-Disparagement": "Agreement not to speak negatively about the other party.",
|
| 216 |
-
"ROFR/ROFO/ROFN": "Right of First Refusal / Offer / Negotiation clause.",
|
| 217 |
-
"Change of Control": "Provisions triggered by ownership or control changes.",
|
| 218 |
-
"Anti-Assignment": "Restrictions on transferring contract rights to third parties.",
|
| 219 |
-
"Liquidated Damages": "Pre-determined damages amount for breach of contract.",
|
| 220 |
-
"Source Code Escrow": "Third-party holds source code for release under defined conditions.",
|
| 221 |
-
"Post-Termination Services": "Services to be provided after the contract ends.",
|
| 222 |
-
"Audit Rights": "Right to inspect records or verify compliance.",
|
| 223 |
-
"Warranty Duration": "Length of time warranties remain in effect.",
|
| 224 |
-
"Covenant Not to Sue": "Agreement not to bring legal action against a party.",
|
| 225 |
-
"Third Party Beneficiary": "Non-party who benefits from the contract terms.",
|
| 226 |
-
"Insurance": "Insurance coverage requirements.",
|
| 227 |
-
"Revenue/Profit Sharing": "Revenue or profit sharing arrangements between parties.",
|
| 228 |
-
"Price Restriction": "Restrictions on pricing or discounting.",
|
| 229 |
-
"Minimum Commitment": "Minimum purchase or usage commitment.",
|
| 230 |
-
"Volume Restriction": "Limits on volume of goods or services.",
|
| 231 |
-
"License Grant": "Permission to use intellectual property.",
|
| 232 |
-
"Non-Transferable License": "License that cannot be transferred to third parties.",
|
| 233 |
-
"Irrevocable or Perpetual License": "License that cannot be revoked or lasts indefinitely.",
|
| 234 |
-
"Unlimited/All-You-Can-Eat License": "License with no usage limits.",
|
| 235 |
-
"Notice Period to Terminate Renewal": "Required notice period before automatic renewal.",
|
| 236 |
-
})
|
| 237 |
-
|
| 238 |
-
RISK_WEIGHTS = {"CRITICAL": 40, "HIGH": 20, "MEDIUM": 10, "LOW": 3}
|
| 239 |
-
|
| 240 |
-
RISK_STYLES = {
|
| 241 |
-
"CRITICAL": ("#dc2626", "#fef2f2", "β οΈ"),
|
| 242 |
-
"HIGH": ("#ea580c", "#fff7ed", "β‘"),
|
| 243 |
-
"MEDIUM": ("#ca8a04", "#fefce8", "π"),
|
| 244 |
-
"LOW": ("#16a34a", "#f0fdf4", "β"),
|
| 245 |
-
}
|
| 246 |
-
|
| 247 |
-
# Per-class optimized thresholds (tuned on validation set; classes with F1=0 get high threshold)
|
| 248 |
-
# Classes 0,1,2,7,9,21,22,27,37,38 scored F1=0.00 in the model card β raise thresholds
|
| 249 |
-
_CUAD_THRESHOLDS = {}
|
| 250 |
-
_WEAK_CLASSES = {0, 1, 2, 7, 9, 21, 22, 27, 37, 38}
|
| 251 |
-
for _i in range(41):
|
| 252 |
-
if _i in _WEAK_CLASSES:
|
| 253 |
-
_CUAD_THRESHOLDS[_i] = 0.85 # Only flag if very confident (these classes are unreliable)
|
| 254 |
-
else:
|
| 255 |
-
_CUAD_THRESHOLDS[_i] = 0.40 # Reasonable threshold for sigmoid outputs
|
| 256 |
-
|
| 257 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 258 |
-
# 2. MODEL LOADING
|
| 259 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 260 |
-
|
| 261 |
-
cuad_tokenizer = None
|
| 262 |
-
cuad_model = None
|
| 263 |
-
ner_pipeline = None
|
| 264 |
-
nli_pipeline = None
|
| 265 |
-
_model_status = {"cuad": "not_loaded", "ner": "not_loaded", "nli": "not_loaded"}
|
| 266 |
-
|
| 267 |
-
def _load_cuad_model():
|
| 268 |
-
global cuad_tokenizer, cuad_model, _model_status
|
| 269 |
-
if not _HAS_TORCH:
|
| 270 |
-
print("[ClauseGuard] PyTorch not available β using regex fallback")
|
| 271 |
-
_model_status["cuad"] = "unavailable"
|
| 272 |
-
return
|
| 273 |
-
try:
|
| 274 |
-
base = "nlpaueb/legal-bert-base-uncased"
|
| 275 |
-
adapter = "Mokshith31/legalbert-contract-clause-classification"
|
| 276 |
-
print(f"[ClauseGuard] Loading CUAD classifier: {adapter}")
|
| 277 |
-
cuad_tokenizer = AutoTokenizer.from_pretrained(base)
|
| 278 |
-
base_model = AutoModelForSequenceClassification.from_pretrained(
|
| 279 |
-
base, num_labels=41, ignore_mismatched_sizes=True
|
| 280 |
-
)
|
| 281 |
-
cuad_model = PeftModel.from_pretrained(base_model, adapter)
|
| 282 |
-
cuad_model.eval()
|
| 283 |
-
_model_status["cuad"] = "loaded"
|
| 284 |
-
print("[ClauseGuard] CUAD model loaded successfully")
|
| 285 |
-
except Exception as e:
|
| 286 |
-
print(f"[ClauseGuard] CUAD model load failed: {e}")
|
| 287 |
-
cuad_tokenizer = None
|
| 288 |
-
cuad_model = None
|
| 289 |
-
_model_status["cuad"] = f"failed: {e}"
|
| 290 |
-
|
| 291 |
-
def _load_ner_model():
|
| 292 |
-
global ner_pipeline, _model_status, _HAS_NER_MODEL
|
| 293 |
-
if not _HAS_TORCH:
|
| 294 |
-
_model_status["ner"] = "unavailable"
|
| 295 |
-
return
|
| 296 |
-
try:
|
| 297 |
-
print("[ClauseGuard] Loading Legal NER model: matterstack/legal-bert-ner")
|
| 298 |
-
ner_pipeline = pipeline(
|
| 299 |
-
"ner",
|
| 300 |
-
model="matterstack/legal-bert-ner",
|
| 301 |
-
aggregation_strategy="simple",
|
| 302 |
-
device=-1, # CPU
|
| 303 |
-
)
|
| 304 |
-
_HAS_NER_MODEL = True
|
| 305 |
-
_model_status["ner"] = "loaded"
|
| 306 |
-
print("[ClauseGuard] Legal NER model loaded successfully")
|
| 307 |
-
except Exception as e:
|
| 308 |
-
print(f"[ClauseGuard] Legal NER model load failed (using regex fallback): {e}")
|
| 309 |
-
_model_status["ner"] = f"failed: {e}"
|
| 310 |
-
|
| 311 |
-
def _load_nli_model():
|
| 312 |
-
global nli_pipeline, _model_status, _HAS_NLI_MODEL
|
| 313 |
-
if not _HAS_TORCH:
|
| 314 |
-
_model_status["nli"] = "unavailable"
|
| 315 |
-
return
|
| 316 |
-
try:
|
| 317 |
-
print("[ClauseGuard] Loading NLI model: cross-encoder/nli-deberta-v3-base")
|
| 318 |
-
nli_pipeline = pipeline(
|
| 319 |
-
"text-classification",
|
| 320 |
-
model="cross-encoder/nli-deberta-v3-base",
|
| 321 |
-
device=-1,
|
| 322 |
-
)
|
| 323 |
-
_HAS_NLI_MODEL = True
|
| 324 |
-
_model_status["nli"] = "loaded"
|
| 325 |
-
print("[ClauseGuard] NLI model loaded successfully")
|
| 326 |
-
except Exception as e:
|
| 327 |
-
print(f"[ClauseGuard] NLI model load failed (using heuristic fallback): {e}")
|
| 328 |
-
_model_status["nli"] = f"failed: {e}"
|
| 329 |
-
|
| 330 |
-
def get_model_status_text():
|
| 331 |
-
"""Return human-readable model status."""
|
| 332 |
-
parts = []
|
| 333 |
-
for name, status in _model_status.items():
|
| 334 |
-
icon = "β
" if status == "loaded" else "β οΈ" if "failed" in status else "β"
|
| 335 |
-
label = {"cuad": "Clause Classifier", "ner": "Legal NER", "nli": "NLI Contradiction"}[name]
|
| 336 |
-
parts.append(f"{icon} {label}: {status}")
|
| 337 |
-
return " Β· ".join(parts)
|
| 338 |
-
|
| 339 |
-
# Load models at startup
|
| 340 |
-
_load_cuad_model()
|
| 341 |
-
_load_ner_model()
|
| 342 |
-
_load_nli_model()
|
| 343 |
-
|
| 344 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 345 |
-
# 3. DOCUMENT PARSING
|
| 346 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 347 |
-
|
| 348 |
-
def parse_pdf(file_path):
|
| 349 |
-
"""Smart PDF parser: native text extraction with OCR fallback for scanned PDFs."""
|
| 350 |
-
text, error, method = parse_pdf_smart(file_path)
|
| 351 |
-
if text:
|
| 352 |
-
if method == "ocr":
|
| 353 |
-
print(f"[ClauseGuard] PDF extracted via OCR ({len(text)} chars)")
|
| 354 |
-
return text, None
|
| 355 |
-
if error:
|
| 356 |
-
return None, error
|
| 357 |
-
return None, "Could not extract text from PDF. Try uploading a clearer scan or digital PDF."
|
| 358 |
-
|
| 359 |
-
def parse_docx(file_path):
|
| 360 |
-
if not _HAS_DOCX:
|
| 361 |
-
return None, "DOCX parsing not available (python-docx not installed)"
|
| 362 |
-
try:
|
| 363 |
-
doc = DocxDocument(file_path)
|
| 364 |
-
paragraphs = [p.text for p in doc.paragraphs if p.text.strip()]
|
| 365 |
-
return "\n\n".join(paragraphs), None
|
| 366 |
-
except Exception as e:
|
| 367 |
-
return None, f"DOCX parse error: {e}"
|
| 368 |
-
|
| 369 |
-
def parse_document(file_path):
|
| 370 |
-
if file_path is None:
|
| 371 |
-
return None, "No file uploaded"
|
| 372 |
-
ext = os.path.splitext(file_path)[1].lower()
|
| 373 |
-
if ext == ".pdf":
|
| 374 |
-
return parse_pdf(file_path)
|
| 375 |
-
elif ext in (".docx", ".doc"):
|
| 376 |
-
return parse_docx(file_path)
|
| 377 |
-
elif ext in (".txt", ".md", ".rst"):
|
| 378 |
-
try:
|
| 379 |
-
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 380 |
-
return f.read(), None
|
| 381 |
-
except Exception as e:
|
| 382 |
-
return None, f"Text read error: {e}"
|
| 383 |
-
else:
|
| 384 |
-
return None, f"Unsupported file type: {ext}"
|
| 385 |
-
|
| 386 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 387 |
-
# 4. DETERMINISTIC CLAUSE SPLITTING (Fix 1 from bug report)
|
| 388 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 389 |
-
|
| 390 |
-
# Document-level chunk cache: same text always produces same chunks
|
| 391 |
-
_chunk_cache = {}
|
| 392 |
-
|
| 393 |
-
def split_clauses(text):
|
| 394 |
-
"""Deterministic, structure-aware clause splitting.
|
| 395 |
-
Fix 1: Same input ALWAYS produces same output. Normalized text is hashed
|
| 396 |
-
and cached so repeated runs on identical documents are identical."""
|
| 397 |
-
# Normalize whitespace before hashing for determinism
|
| 398 |
-
normalized = re.sub(r'\s+', ' ', text.strip())
|
| 399 |
-
text_hash = hashlib.sha256(normalized.encode()).hexdigest()
|
| 400 |
-
if text_hash in _chunk_cache:
|
| 401 |
-
return _chunk_cache[text_hash]
|
| 402 |
-
|
| 403 |
-
text = re.sub(r'\n{3,}', '\n\n', text.strip())
|
| 404 |
-
|
| 405 |
-
# First try to detect numbered sections (1., 2., 3.1, (a), etc.)
|
| 406 |
-
section_pattern = re.compile(
|
| 407 |
-
r'(?:^|\n\n)'
|
| 408 |
-
r'(?='
|
| 409 |
-
r'\d+(?:\.\d+)*[.)]\s' # 1. 2. 3.1. 3.1)
|
| 410 |
-
r'|[A-Z]{2,}[A-Z\s]*\n' # ALL CAPS HEADERS
|
| 411 |
-
r'|\([a-z]\)\s' # (a) (b) (c)
|
| 412 |
-
r'|(?:Section|Article|Clause)\s+\d+' # Section 1, Article 2
|
| 413 |
-
r')',
|
| 414 |
-
re.MULTILINE
|
| 415 |
-
)
|
| 416 |
-
|
| 417 |
-
positions = [m.start() for m in section_pattern.finditer(text)]
|
| 418 |
-
|
| 419 |
-
if len(positions) >= 3:
|
| 420 |
-
# Document has clear section structure β split on sections
|
| 421 |
-
clauses = []
|
| 422 |
-
for i, pos in enumerate(positions):
|
| 423 |
-
end = positions[i + 1] if i + 1 < len(positions) else len(text)
|
| 424 |
-
chunk = text[pos:end].strip()
|
| 425 |
-
if len(chunk) > 30:
|
| 426 |
-
# If a section is very long, split on paragraph breaks within it
|
| 427 |
-
if len(chunk) > 1500:
|
| 428 |
-
sub_parts = chunk.split('\n\n')
|
| 429 |
-
current = ""
|
| 430 |
-
for sp in sub_parts:
|
| 431 |
-
if len(current) + len(sp) < 1200:
|
| 432 |
-
current += ("\n\n" + sp if current else sp)
|
| 433 |
-
else:
|
| 434 |
-
if len(current.strip()) > 30:
|
| 435 |
-
clauses.append(current.strip())
|
| 436 |
-
current = sp
|
| 437 |
-
if len(current.strip()) > 30:
|
| 438 |
-
clauses.append(current.strip())
|
| 439 |
-
else:
|
| 440 |
-
clauses.append(chunk)
|
| 441 |
-
# Also capture anything before the first section
|
| 442 |
-
if positions and positions[0] > 50:
|
| 443 |
-
preamble = text[:positions[0]].strip()
|
| 444 |
-
if len(preamble) > 30:
|
| 445 |
-
clauses.insert(0, preamble)
|
| 446 |
-
result = clauses if clauses else _fallback_split(text)
|
| 447 |
-
_chunk_cache[text_hash] = result
|
| 448 |
-
return result
|
| 449 |
-
else:
|
| 450 |
-
result = _fallback_split(text)
|
| 451 |
-
_chunk_cache[text_hash] = result
|
| 452 |
-
return result
|
| 453 |
-
|
| 454 |
-
def _fallback_split(text):
|
| 455 |
-
"""Fallback: split on paragraph breaks and sentence boundaries."""
|
| 456 |
-
# Try paragraph-based splitting first
|
| 457 |
-
paragraphs = text.split('\n\n')
|
| 458 |
-
if len(paragraphs) >= 3:
|
| 459 |
-
clauses = []
|
| 460 |
-
for p in paragraphs:
|
| 461 |
-
p = p.strip()
|
| 462 |
-
if len(p) > 30:
|
| 463 |
-
if len(p) > 1500:
|
| 464 |
-
# Split long paragraphs on sentences
|
| 465 |
-
sents = re.split(r'(?<=[.!?])\s+(?=[A-Z])', p)
|
| 466 |
-
current = ""
|
| 467 |
-
for s in sents:
|
| 468 |
-
if len(current) + len(s) < 1000:
|
| 469 |
-
current += (" " + s if current else s)
|
| 470 |
-
else:
|
| 471 |
-
if len(current.strip()) > 30:
|
| 472 |
-
clauses.append(current.strip())
|
| 473 |
-
current = s
|
| 474 |
-
if len(current.strip()) > 30:
|
| 475 |
-
clauses.append(current.strip())
|
| 476 |
-
else:
|
| 477 |
-
clauses.append(p)
|
| 478 |
-
return clauses
|
| 479 |
-
|
| 480 |
-
# Last resort: sentence splitting
|
| 481 |
-
parts = re.split(r'(?<=[.!?])\s+(?=[A-Z0-9(])', text)
|
| 482 |
-
return [p.strip() for p in parts if len(p.strip()) > 30]
|
| 483 |
-
|
| 484 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 485 |
-
# 5. CLAUSE DETECTION β FIXED: sigmoid + per-class thresholds + caching
|
| 486 |
-
# Fix 3: Strip section headings before classification
|
| 487 |
-
# Fix 6: Label guardrails for high-confidence false positives
|
| 488 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 489 |
-
|
| 490 |
-
# Fix 3: Section heading pattern β strip before classifying
|
| 491 |
-
_HEADING_RE = re.compile(r'^\d+(?:\.\d+)*\s+[A-Z][A-Z\s&,/]+$', re.MULTILINE)
|
| 492 |
-
|
| 493 |
-
def _strip_heading(text):
|
| 494 |
-
"""Remove leading section headings that confuse the classifier."""
|
| 495 |
-
lines = text.split('\n')
|
| 496 |
-
if lines and _HEADING_RE.match(lines[0].strip()):
|
| 497 |
-
stripped = '\n'.join(lines[1:]).strip()
|
| 498 |
-
return stripped if len(stripped) > 20 else text
|
| 499 |
-
return text
|
| 500 |
-
|
| 501 |
-
# Fix 6: Label guardrails β keyword validation for high-confidence labels
|
| 502 |
-
_LABEL_GUARDRAILS = {
|
| 503 |
-
"Liquidated Damages": re.compile(
|
| 504 |
-
r'liquidated|pre-?determined.{0,10}damage|agreed.{0,10}sum|penalty clause|stipulated.{0,10}damage',
|
| 505 |
-
re.IGNORECASE
|
| 506 |
-
),
|
| 507 |
-
"Uncapped Liability": re.compile(
|
| 508 |
-
r'uncapped|unlimited.{0,10}liabilit|no.{0,10}(limit|cap).{0,10}liabilit',
|
| 509 |
-
re.IGNORECASE
|
| 510 |
-
),
|
| 511 |
-
}
|
| 512 |
-
|
| 513 |
-
def _apply_guardrails(label, text, confidence):
|
| 514 |
-
"""Fix 6: If label has a guardrail and text lacks required keywords, demote."""
|
| 515 |
-
guard = _LABEL_GUARDRAILS.get(label)
|
| 516 |
-
if guard and not guard.search(text):
|
| 517 |
-
return "Other", confidence * 0.3 # demote to Other with reduced confidence
|
| 518 |
-
return label, confidence
|
| 519 |
-
|
| 520 |
-
def _text_hash(text):
|
| 521 |
-
return hashlib.md5(text.encode()).hexdigest()
|
| 522 |
-
|
| 523 |
-
_prediction_cache = {}
|
| 524 |
-
_CACHE_MAX = 2000
|
| 525 |
-
|
| 526 |
-
def classify_cuad(clause_text):
|
| 527 |
-
if cuad_model is None or cuad_tokenizer is None:
|
| 528 |
-
return _classify_regex(clause_text)
|
| 529 |
-
|
| 530 |
-
# Fix 3: Strip section headings before classification
|
| 531 |
-
clean_text = _strip_heading(clause_text)
|
| 532 |
-
|
| 533 |
-
# Check cache
|
| 534 |
-
h = _text_hash(clean_text[:512])
|
| 535 |
-
if h in _prediction_cache:
|
| 536 |
-
return _prediction_cache[h]
|
| 537 |
-
|
| 538 |
-
try:
|
| 539 |
-
inputs = cuad_tokenizer(
|
| 540 |
-
clean_text,
|
| 541 |
-
return_tensors="pt",
|
| 542 |
-
truncation=True,
|
| 543 |
-
max_length=256,
|
| 544 |
-
padding=True
|
| 545 |
-
)
|
| 546 |
-
with torch.no_grad():
|
| 547 |
-
logits = cuad_model(**inputs).logits
|
| 548 |
-
|
| 549 |
-
# FIXED: Use sigmoid for multi-label (not softmax)
|
| 550 |
-
probs = torch.sigmoid(logits)[0]
|
| 551 |
-
|
| 552 |
-
results = []
|
| 553 |
-
for i, prob in enumerate(probs):
|
| 554 |
-
threshold = _CUAD_THRESHOLDS.get(i, 0.40)
|
| 555 |
-
if float(prob) > threshold and i < len(CUAD_LABELS):
|
| 556 |
-
label = CUAD_LABELS[i]
|
| 557 |
-
conf = float(prob)
|
| 558 |
-
# Fix 6: Apply guardrails β reject high-confidence false positives
|
| 559 |
-
label, conf = _apply_guardrails(label, clause_text, conf)
|
| 560 |
-
if label == "Other" and conf < 0.3:
|
| 561 |
-
continue # Skip demoted labels
|
| 562 |
-
risk = RISK_MAP.get(label, "LOW")
|
| 563 |
-
results.append({
|
| 564 |
-
"label": label,
|
| 565 |
-
"confidence": round(conf, 3),
|
| 566 |
-
"risk": risk,
|
| 567 |
-
"description": DESC_MAP.get(label, label),
|
| 568 |
-
"source": "ml",
|
| 569 |
-
})
|
| 570 |
-
results.sort(key=lambda x: x["confidence"], reverse=True)
|
| 571 |
-
|
| 572 |
-
# If no ML results, also try regex to catch what model misses
|
| 573 |
-
if not results:
|
| 574 |
-
results = _classify_regex(clause_text)
|
| 575 |
-
|
| 576 |
-
# Cache result
|
| 577 |
-
if len(_prediction_cache) < _CACHE_MAX:
|
| 578 |
-
_prediction_cache[h] = results
|
| 579 |
-
|
| 580 |
-
return results
|
| 581 |
-
except Exception as e:
|
| 582 |
-
print(f"[ClauseGuard] CUAD inference error: {e}")
|
| 583 |
-
return _classify_regex(clause_text)
|
| 584 |
-
|
| 585 |
-
_REGEX_PATTERNS = {
|
| 586 |
-
"Limitation of liability": [r"not liable", r"shall not be (liable|responsible)", r"in no event.*liable", r"limitation of liability", r"without warranty", r"disclaim"],
|
| 587 |
-
"Unilateral termination": [r"terminat.*at any time", r"suspend.*account.*without", r"we may (terminat|suspend|discontinu)", r"right to (terminat|suspend)"],
|
| 588 |
-
"Unilateral change": [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)"],
|
| 589 |
-
"Content removal": [r"remove.*content.*without", r"right to remove", r"we may.*remove"],
|
| 590 |
-
"Contract by using": [r"by (using|accessing).*you agree", r"continued use.*constitutes? acceptance"],
|
| 591 |
-
"Choice of law": [r"governed by.*laws? of", r"shall be governed", r"laws of the state of"],
|
| 592 |
-
"Jurisdiction": [r"exclusive jurisdiction", r"courts? of.*(california|delaware|new york|ireland|england)", r"submit to.*jurisdiction"],
|
| 593 |
-
"Arbitration": [r"arbitrat", r"binding arbitration", r"waive.*right.*court", r"class action waiver"],
|
| 594 |
-
"Governing Law": [r"governed by", r"laws of", r"jurisdiction of"],
|
| 595 |
-
"Termination for Convenience": [r"terminat.*for convenience", r"terminat.*without cause", r"terminat.*at any time"],
|
| 596 |
-
"Non-Compete": [r"non-compete", r"shall not compete", r"competition"],
|
| 597 |
-
"Exclusivity": [r"exclusive(?:ly)?(?:\s+(?:deal|relationship|partner|right))", r"exclusivity"],
|
| 598 |
-
"IP Ownership Assignment": [r"assign.*intellectual property", r"ownership of.*ip", r"all rights.*assign"],
|
| 599 |
-
"Uncapped Liability": [r"unlimited liability", r"uncapped", r"no.*limit.*liability"],
|
| 600 |
-
"Cap on Liability": [r"cap on liability", r"maximum liability", r"liability.*shall not exceed", r"aggregate liability.*not exceed"],
|
| 601 |
-
"Indemnification": [r"indemnif", r"hold harmless", r"defend.*against.*claim"],
|
| 602 |
-
"Confidentiality": [r"confidential(?:ity)?", r"non-disclosure", r"\bnda\b"],
|
| 603 |
-
"Force Majeure": [r"force majeure", r"act of god", r"beyond.*(?:reasonable\s+)?control"],
|
| 604 |
-
"Penalties": [r"penalt(?:y|ies)", r"late fee", r"default charge", r"interest on overdue"],
|
| 605 |
-
}
|
| 606 |
-
|
| 607 |
-
def _classify_regex(text):
|
| 608 |
-
"""Regex fallback β returns pattern match, NOT fake confidence."""
|
| 609 |
-
text_lower = text.lower()
|
| 610 |
-
results = []
|
| 611 |
-
seen = set()
|
| 612 |
-
for label, patterns in _REGEX_PATTERNS.items():
|
| 613 |
-
for pat in patterns:
|
| 614 |
-
if re.search(pat, text_lower):
|
| 615 |
-
if label not in seen:
|
| 616 |
-
risk = RISK_MAP.get(label, "MEDIUM")
|
| 617 |
-
results.append({
|
| 618 |
-
"label": label,
|
| 619 |
-
"confidence": None, # FIXED: no fake confidence for regex
|
| 620 |
-
"risk": risk,
|
| 621 |
-
"description": DESC_MAP.get(label, label),
|
| 622 |
-
"source": "pattern",
|
| 623 |
-
})
|
| 624 |
-
seen.add(label)
|
| 625 |
-
break
|
| 626 |
-
return results
|
| 627 |
-
|
| 628 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 629 |
-
# 6. LEGAL NER β ML model with regex fallback
|
| 630 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 631 |
-
|
| 632 |
-
def extract_entities(text):
|
| 633 |
-
"""Extract entities using ML model (matterstack/legal-bert-ner) with regex fallback."""
|
| 634 |
-
entities = []
|
| 635 |
-
|
| 636 |
-
# Try ML NER first
|
| 637 |
-
if _HAS_NER_MODEL and ner_pipeline is not None:
|
| 638 |
-
try:
|
| 639 |
-
# Process in chunks (model has max length limits)
|
| 640 |
-
chunks = [text[i:i+512] for i in range(0, min(len(text), 10000), 450)]
|
| 641 |
-
offset = 0
|
| 642 |
-
for chunk in chunks:
|
| 643 |
-
ner_results = ner_pipeline(chunk)
|
| 644 |
-
for ent in ner_results:
|
| 645 |
-
if ent.get("score", 0) > 0.5:
|
| 646 |
-
entities.append({
|
| 647 |
-
"text": ent["word"],
|
| 648 |
-
"type": _map_ner_label(ent.get("entity_group", ent.get("entity", "MISC"))),
|
| 649 |
-
"start": ent["start"] + offset,
|
| 650 |
-
"end": ent["end"] + offset,
|
| 651 |
-
"score": round(ent["score"], 3),
|
| 652 |
-
"source": "ml",
|
| 653 |
-
})
|
| 654 |
-
offset += 450
|
| 655 |
-
except Exception as e:
|
| 656 |
-
print(f"[ClauseGuard] ML NER error, falling back to regex: {e}")
|
| 657 |
-
entities = _extract_entities_regex(text)
|
| 658 |
-
else:
|
| 659 |
-
entities = _extract_entities_regex(text)
|
| 660 |
-
|
| 661 |
-
# Always supplement with regex patterns for things NER often misses
|
| 662 |
-
regex_ents = _extract_entities_regex(text)
|
| 663 |
-
# Merge: add regex entities that don't overlap with ML entities
|
| 664 |
-
ml_spans = set()
|
| 665 |
-
for e in entities:
|
| 666 |
-
for pos in range(e["start"], e["end"]):
|
| 667 |
-
ml_spans.add(pos)
|
| 668 |
-
for re_ent in regex_ents:
|
| 669 |
-
if not any(pos in ml_spans for pos in range(re_ent["start"], re_ent["end"])):
|
| 670 |
-
entities.append(re_ent)
|
| 671 |
-
|
| 672 |
-
# Deduplicate and sort
|
| 673 |
-
entities.sort(key=lambda x: (x["start"], -(x["end"] - x["start"])))
|
| 674 |
-
filtered = []
|
| 675 |
-
last_end = -1
|
| 676 |
-
for e in entities:
|
| 677 |
-
if e["start"] >= last_end:
|
| 678 |
-
filtered.append(e)
|
| 679 |
-
last_end = e["end"]
|
| 680 |
-
return filtered
|
| 681 |
-
|
| 682 |
-
def _map_ner_label(label):
|
| 683 |
-
"""Map NER model labels to our entity types."""
|
| 684 |
-
label = label.upper()
|
| 685 |
-
mapping = {
|
| 686 |
-
"PER": "PERSON",
|
| 687 |
-
"PERSON": "PERSON",
|
| 688 |
-
"ORG": "PARTY",
|
| 689 |
-
"ORGANIZATION": "PARTY",
|
| 690 |
-
"LOC": "JURISDICTION",
|
| 691 |
-
"LOCATION": "JURISDICTION",
|
| 692 |
-
"GPE": "JURISDICTION",
|
| 693 |
-
"DATE": "DATE",
|
| 694 |
-
"MONEY": "MONEY",
|
| 695 |
-
"MISC": "MISC",
|
| 696 |
-
"LAW": "LEGAL_REF",
|
| 697 |
-
}
|
| 698 |
-
return mapping.get(label, label)
|
| 699 |
-
|
| 700 |
-
def _extract_entities_regex(text):
|
| 701 |
-
"""Regex-based NER fallback."""
|
| 702 |
-
entities = []
|
| 703 |
-
patterns = [
|
| 704 |
-
# Dates
|
| 705 |
-
(r'\b(?:January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2},?\s+\d{4}\b', "DATE"),
|
| 706 |
-
(r'\b\d{1,2}/\d{1,2}/\d{2,4}\b', "DATE"),
|
| 707 |
-
(r'\b\d{1,2}-(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2,4}\b', "DATE"),
|
| 708 |
-
(r'\b(?:Effective|Commencement|Expiration|Termination)\s+Date\b', "DATE_REF"),
|
| 709 |
-
# Money
|
| 710 |
-
(r'\$\s?\d{1,3}(?:,\d{3})*(?:\.\d{2})?(?:\s*(?:million|billion|thousand|M|B|K))?', "MONEY"),
|
| 711 |
-
(r'\b\d{1,3}(?:,\d{3})*(?:\.\d{2})?\s*(?:USD|EUR|GBP|dollars|euros|pounds)', "MONEY"),
|
| 712 |
-
(r'\b(?:USD|EUR|GBP)\s*\d{1,3}(?:,\d{3})*(?:\.\d{2})?', "MONEY"),
|
| 713 |
-
# Percentages
|
| 714 |
-
(r'\b\d+(?:\.\d+)?%', "PERCENTAGE"),
|
| 715 |
-
# Durations
|
| 716 |
-
(r'\b\d+\s*(?:year|month|week|day|business day)s?\b', "DURATION"),
|
| 717 |
-
# Parties (require suffix to reduce false positives)
|
| 718 |
-
(r'\b[A-Z][A-Za-z0-9\s&,]+?(?:Inc\.?|LLC|Ltd\.?|Limited|Corp\.?|Corporation|PLC|GmbH|AG|S\.A\.?|B\.V\.?|L\.P\.?|LLP)\b', "PARTY"),
|
| 719 |
-
(r'\b(?:Party A|Party B|Disclosing Party|Receiving Party|Licensor|Licensee|Buyer|Seller|Tenant|Landlord|Employer|Employee|Customer|Vendor|Client)\b', "PARTY_ROLE"),
|
| 720 |
-
# Jurisdictions
|
| 721 |
-
(r'\b(?:State|Commonwealth)\s+of\s+[A-Z][a-zA-Z\s]+', "JURISDICTION"),
|
| 722 |
-
(r'\b(?:California|Delaware|New York|Texas|Florida|England|Ireland|Germany|France|Singapore|Hong Kong|Ontario|British Columbia)\b', "JURISDICTION"),
|
| 723 |
-
# Defined Terms (quoted or parenthesized)
|
| 724 |
-
(r'"([A-Z][A-Za-z\s]{1,40})"', "DEFINED_TERM"),
|
| 725 |
-
(r'\((?:the\s+)?"([A-Z][A-Za-z\s]{1,40})"\)', "DEFINED_TERM"),
|
| 726 |
-
]
|
| 727 |
-
for pat, etype in patterns:
|
| 728 |
-
for m in re.finditer(pat, text, re.IGNORECASE if etype in ("DATE", "MONEY", "DURATION", "PERCENTAGE") else 0):
|
| 729 |
-
txt = m.group(1) if m.lastindex else m.group()
|
| 730 |
-
entities.append({
|
| 731 |
-
"text": txt,
|
| 732 |
-
"type": etype,
|
| 733 |
-
"start": m.start(),
|
| 734 |
-
"end": m.end(),
|
| 735 |
-
"source": "pattern",
|
| 736 |
-
})
|
| 737 |
-
return entities
|
| 738 |
-
|
| 739 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 740 |
-
# 7. NLI / CONTRADICTION DETECTION β Real semantic analysis
|
| 741 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 742 |
-
|
| 743 |
-
def detect_contradictions(clause_results, raw_text=""):
|
| 744 |
-
"""
|
| 745 |
-
Detect contradictions using:
|
| 746 |
-
1. NLI cross-encoder model (semantic contradiction detection)
|
| 747 |
-
2. Structural conflict detection (mutually exclusive labels)
|
| 748 |
-
3. Missing critical clause detection
|
| 749 |
-
"""
|
| 750 |
-
contradictions = []
|
| 751 |
-
labels_found = set()
|
| 752 |
-
clause_texts_by_label = defaultdict(list)
|
| 753 |
-
|
| 754 |
-
for cr in clause_results:
|
| 755 |
-
labels_found.add(cr["label"])
|
| 756 |
-
clause_texts_by_label[cr["label"]].append(cr.get("text", ""))
|
| 757 |
-
|
| 758 |
-
# ββ 1. Semantic NLI (if model available) ββ
|
| 759 |
-
if _HAS_NLI_MODEL and nli_pipeline is not None:
|
| 760 |
-
# Check clauses that belong to potentially conflicting categories
|
| 761 |
-
conflict_pairs = [
|
| 762 |
-
("Uncapped Liability", "Cap on Liability",
|
| 763 |
-
"Liability cannot be both uncapped and capped simultaneously."),
|
| 764 |
-
("IP Ownership Assignment", "Joint IP Ownership",
|
| 765 |
-
"IP cannot be both fully assigned and jointly owned."),
|
| 766 |
-
("Exclusivity", "Non-Transferable License",
|
| 767 |
-
"Exclusivity and non-transferable license may conflict."),
|
| 768 |
-
]
|
| 769 |
-
for label_a, label_b, explanation in conflict_pairs:
|
| 770 |
-
if label_a in labels_found and label_b in labels_found:
|
| 771 |
-
texts_a = clause_texts_by_label[label_a]
|
| 772 |
-
texts_b = clause_texts_by_label[label_b]
|
| 773 |
-
for ta in texts_a[:2]:
|
| 774 |
-
for tb in texts_b[:2]:
|
| 775 |
-
try:
|
| 776 |
-
nli_result = nli_pipeline(
|
| 777 |
-
f"{ta[:256]} [SEP] {tb[:256]}",
|
| 778 |
-
truncation=True
|
| 779 |
-
)
|
| 780 |
-
# Check if model predicts contradiction
|
| 781 |
-
for r in (nli_result if isinstance(nli_result, list) else [nli_result]):
|
| 782 |
-
if r.get("label", "").lower() == "contradiction" and r.get("score", 0) > 0.6:
|
| 783 |
-
contradictions.append({
|
| 784 |
-
"type": "CONTRADICTION",
|
| 785 |
-
"explanation": explanation,
|
| 786 |
-
"severity": "HIGH",
|
| 787 |
-
"clauses": [label_a, label_b],
|
| 788 |
-
"confidence": round(r["score"], 3),
|
| 789 |
-
"source": "nli_model",
|
| 790 |
-
})
|
| 791 |
-
except Exception:
|
| 792 |
-
pass
|
| 793 |
-
|
| 794 |
-
# Also check for internal contradictions within governing law / termination
|
| 795 |
-
for label in ["Governing Law", "Termination for Convenience"]:
|
| 796 |
-
texts = clause_texts_by_label.get(label, [])
|
| 797 |
-
if len(texts) >= 2:
|
| 798 |
-
for i in range(len(texts)):
|
| 799 |
-
for j in range(i + 1, min(len(texts), i + 3)):
|
| 800 |
-
try:
|
| 801 |
-
nli_result = nli_pipeline(
|
| 802 |
-
f"{texts[i][:256]} [SEP] {texts[j][:256]}",
|
| 803 |
-
truncation=True
|
| 804 |
-
)
|
| 805 |
-
for r in (nli_result if isinstance(nli_result, list) else [nli_result]):
|
| 806 |
-
if r.get("label", "").lower() == "contradiction" and r.get("score", 0) > 0.6:
|
| 807 |
-
contradictions.append({
|
| 808 |
-
"type": "CONTRADICTION",
|
| 809 |
-
"explanation": f"Conflicting {label} provisions detected β clauses contradict each other.",
|
| 810 |
-
"severity": "HIGH",
|
| 811 |
-
"clauses": [label],
|
| 812 |
-
"confidence": round(r["score"], 3),
|
| 813 |
-
"source": "nli_model",
|
| 814 |
-
})
|
| 815 |
-
except Exception:
|
| 816 |
-
pass
|
| 817 |
-
else:
|
| 818 |
-
# ββ Heuristic fallback (improved) ββ
|
| 819 |
-
_heuristic_pairs = [
|
| 820 |
-
(["Uncapped Liability"], ["Cap on Liability"],
|
| 821 |
-
"Liability cannot be both uncapped and capped simultaneously."),
|
| 822 |
-
(["IP Ownership Assignment"], ["Joint IP Ownership"],
|
| 823 |
-
"IP cannot be both fully assigned and jointly owned."),
|
| 824 |
-
]
|
| 825 |
-
for group_a, group_b, explanation in _heuristic_pairs:
|
| 826 |
-
found_a = any(l in labels_found for l in group_a)
|
| 827 |
-
found_b = any(l in labels_found for l in group_b)
|
| 828 |
-
if found_a and found_b:
|
| 829 |
-
contradictions.append({
|
| 830 |
-
"type": "CONTRADICTION",
|
| 831 |
-
"explanation": explanation,
|
| 832 |
-
"severity": "HIGH",
|
| 833 |
-
"clauses": group_a + group_b,
|
| 834 |
-
"source": "heuristic",
|
| 835 |
-
})
|
| 836 |
-
|
| 837 |
-
# ββ 2. Missing critical clauses (Fix 4: check raw_text, not labels) ββ
|
| 838 |
-
_REQUIRED_CLAUSE_PATTERNS = {
|
| 839 |
-
"Governing Law": re.compile(
|
| 840 |
-
r'govern(?:ed|ing).{0,15}law|applicable.{0,10}law|laws?\s+of\s+the\s+state',
|
| 841 |
-
re.IGNORECASE
|
| 842 |
-
),
|
| 843 |
-
"Limitation of liability": re.compile(
|
| 844 |
-
r'limitation.{0,10}liabilit|cap.{0,10}liabilit|liabilit.{0,10}shall\s+not\s+exceed|in\s+no\s+event.{0,20}liable',
|
| 845 |
-
re.IGNORECASE
|
| 846 |
-
),
|
| 847 |
-
"Arbitration": re.compile(
|
| 848 |
-
r'arbitrat|AAA|JAMS|binding.{0,10}dispute',
|
| 849 |
-
re.IGNORECASE
|
| 850 |
-
),
|
| 851 |
-
"Termination": re.compile(
|
| 852 |
-
r'terminat(?:e|ion|ed)|cancel(?:lation)?',
|
| 853 |
-
re.IGNORECASE
|
| 854 |
-
),
|
| 855 |
-
}
|
| 856 |
-
for clause_name, pattern in _REQUIRED_CLAUSE_PATTERNS.items():
|
| 857 |
-
# Check raw_text directly β it's stable and deterministic
|
| 858 |
-
if not pattern.search(raw_text):
|
| 859 |
-
contradictions.append({
|
| 860 |
-
"type": "MISSING",
|
| 861 |
-
"explanation": f"No '{clause_name}' clause detected in the document.",
|
| 862 |
-
"severity": "MEDIUM",
|
| 863 |
-
"clauses": [clause_name],
|
| 864 |
-
"source": "structural",
|
| 865 |
-
})
|
| 866 |
-
|
| 867 |
-
# Deduplicate
|
| 868 |
-
seen = set()
|
| 869 |
-
unique = []
|
| 870 |
-
for c in contradictions:
|
| 871 |
-
key = (c["type"], c["explanation"])
|
| 872 |
-
if key not in seen:
|
| 873 |
-
seen.add(key)
|
| 874 |
-
unique.append(c)
|
| 875 |
-
|
| 876 |
-
return unique
|
| 877 |
-
|
| 878 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 879 |
-
# 8. RISK SCORING
|
| 880 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 881 |
-
|
| 882 |
-
def compute_risk_score(clause_results, total_clauses):
|
| 883 |
-
sev_counts = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0}
|
| 884 |
-
for cr in clause_results:
|
| 885 |
-
sev = cr.get("risk", "LOW")
|
| 886 |
-
sev_counts[sev] += 1
|
| 887 |
-
if total_clauses == 0:
|
| 888 |
-
return 0, "A", sev_counts
|
| 889 |
-
weighted = sum(sev_counts[s] * RISK_WEIGHTS[s] for s in sev_counts)
|
| 890 |
-
risk = min(100, round(weighted / max(1, total_clauses) * 10))
|
| 891 |
-
if risk >= 70: grade = "F"
|
| 892 |
-
elif risk >= 50: grade = "D"
|
| 893 |
-
elif risk >= 30: grade = "C"
|
| 894 |
-
elif risk >= 15: grade = "B"
|
| 895 |
-
else: grade = "A"
|
| 896 |
-
return risk, grade, sev_counts
|
| 897 |
-
|
| 898 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 899 |
-
# 9. MAIN ANALYSIS PIPELINE
|
| 900 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 901 |
-
|
| 902 |
-
def analyze_contract(text):
|
| 903 |
-
if not text or len(text.strip()) < 50:
|
| 904 |
-
return None, "Document too short (minimum 50 characters)"
|
| 905 |
-
clauses = split_clauses(text)
|
| 906 |
-
if not clauses:
|
| 907 |
-
return None, "No clauses detected in document"
|
| 908 |
-
clause_results = []
|
| 909 |
-
for clause in clauses:
|
| 910 |
-
predictions = classify_cuad(clause)
|
| 911 |
-
if predictions:
|
| 912 |
-
for pred in predictions:
|
| 913 |
-
clause_results.append({
|
| 914 |
-
"text": clause,
|
| 915 |
-
"label": pred["label"],
|
| 916 |
-
"confidence": pred["confidence"],
|
| 917 |
-
"risk": pred["risk"],
|
| 918 |
-
"description": pred["description"],
|
| 919 |
-
"source": pred.get("source", "unknown"),
|
| 920 |
-
})
|
| 921 |
-
entities = extract_entities(text)
|
| 922 |
-
contradictions = detect_contradictions(clause_results, text)
|
| 923 |
-
risk, grade, sev_counts = compute_risk_score(clause_results, len(clauses))
|
| 924 |
-
obligations = extract_obligations(text)
|
| 925 |
-
# Fix 5: Compliance runs against full raw_text (already done in compliance.py)
|
| 926 |
-
compliance = check_compliance(text)
|
| 927 |
-
|
| 928 |
-
# Fix 2: Compute flagged_clauses AFTER all processing is complete
|
| 929 |
-
flagged_clause_count = len(clause_results)
|
| 930 |
-
unique_flagged_texts = len(set(cr["text"] for cr in clause_results))
|
| 931 |
-
|
| 932 |
-
result = {
|
| 933 |
-
"metadata": {
|
| 934 |
-
"analysis_date": datetime.now().isoformat(),
|
| 935 |
-
"total_clauses": len(clauses),
|
| 936 |
-
"flagged_clauses": flagged_clause_count,
|
| 937 |
-
"unique_flagged": unique_flagged_texts,
|
| 938 |
-
"model": get_model_status_text(),
|
| 939 |
-
"text_hash": hashlib.sha256(re.sub(r'\s+', ' ', text.strip()).encode()).hexdigest()[:16],
|
| 940 |
-
},
|
| 941 |
-
"risk": {
|
| 942 |
-
"score": risk,
|
| 943 |
-
"grade": grade,
|
| 944 |
-
"breakdown": sev_counts,
|
| 945 |
-
},
|
| 946 |
-
"clauses": clause_results,
|
| 947 |
-
"entities": entities,
|
| 948 |
-
"contradictions": contradictions,
|
| 949 |
-
"obligations": obligations,
|
| 950 |
-
"compliance": compliance,
|
| 951 |
-
"raw_text": text,
|
| 952 |
-
}
|
| 953 |
-
return result, None
|
| 954 |
-
|
| 955 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 956 |
-
# 10. EXPORT FUNCTIONS β FIXED: per-session temp files
|
| 957 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 958 |
-
|
| 959 |
-
def export_json(result):
|
| 960 |
-
if result is None:
|
| 961 |
-
return None
|
| 962 |
-
return json.dumps(result, indent=2, default=str)
|
| 963 |
-
|
| 964 |
-
def export_csv(result):
|
| 965 |
-
if result is None:
|
| 966 |
-
return None
|
| 967 |
-
output = io.StringIO()
|
| 968 |
-
writer = csv.writer(output)
|
| 969 |
-
writer.writerow(["Clause Text", "Label", "Risk", "Confidence", "Description", "Source"])
|
| 970 |
-
for cr in result.get("clauses", []):
|
| 971 |
-
conf = cr.get("confidence")
|
| 972 |
-
conf_str = f"{conf:.3f}" if conf is not None else "pattern match"
|
| 973 |
-
writer.writerow([
|
| 974 |
-
cr.get("text", "")[:500],
|
| 975 |
-
cr.get("label", ""),
|
| 976 |
-
cr.get("risk", ""),
|
| 977 |
-
conf_str,
|
| 978 |
-
cr.get("description", ""),
|
| 979 |
-
cr.get("source", ""),
|
| 980 |
-
])
|
| 981 |
-
return output.getvalue()
|
| 982 |
-
|
| 983 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 984 |
-
# 11. UI RENDERING β FIXED: shows confidence source properly
|
| 985 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 986 |
-
|
| 987 |
-
def render_summary(result):
|
| 988 |
-
if result is None:
|
| 989 |
-
return ""
|
| 990 |
-
risk = result["risk"]
|
| 991 |
-
score = risk["score"]
|
| 992 |
-
grade = risk["grade"]
|
| 993 |
-
breakdown = risk["breakdown"]
|
| 994 |
-
grade_color = {
|
| 995 |
-
"A": "#16a34a", "B": "#65a30d", "C": "#ca8a04",
|
| 996 |
-
"D": "#ea580c", "F": "#dc2626",
|
| 997 |
-
}.get(grade, "#6b7280")
|
| 998 |
-
crit, high, med, low = breakdown["CRITICAL"], breakdown["HIGH"], breakdown["MEDIUM"], breakdown["LOW"]
|
| 999 |
-
html = f"""
|
| 1000 |
-
<div style="font-family:system-ui,sans-serif;padding:16px;border:1px solid #e5e7eb;border-radius:12px;background:#fff;">
|
| 1001 |
-
<div style="text-align:center;margin-bottom:16px;">
|
| 1002 |
-
<div style="font-size:48px;font-weight:700;color:{grade_color};">{score}</div>
|
| 1003 |
-
<div style="font-size:14px;color:#6b7280;">/100 Risk Score</div>
|
| 1004 |
-
<div style="display:inline-block;margin-top:8px;padding:4px 16px;border-radius:20px;background:{grade_color};color:white;font-weight:600;font-size:14px;">
|
| 1005 |
-
Grade {grade}
|
| 1006 |
-
</div>
|
| 1007 |
-
</div>
|
| 1008 |
-
<div style="display:grid;grid-template-columns:1fr 1fr;gap:8px;margin-bottom:12px;">
|
| 1009 |
-
<div style="padding:8px;border-radius:6px;background:#fef2f2;text-align:center;">
|
| 1010 |
-
<div style="font-size:20px;font-weight:700;color:#dc2626;">{crit}</div>
|
| 1011 |
-
<div style="font-size:11px;color:#991b1b;">Critical</div>
|
| 1012 |
-
</div>
|
| 1013 |
-
<div style="padding:8px;border-radius:6px;background:#fff7ed;text-align:center;">
|
| 1014 |
-
<div style="font-size:20px;font-weight:700;color:#ea580c;">{high}</div>
|
| 1015 |
-
<div style="font-size:11px;color:#9a3412;">High</div>
|
| 1016 |
-
</div>
|
| 1017 |
-
<div style="padding:8px;border-radius:6px;background:#fefce8;text-align:center;">
|
| 1018 |
-
<div style="font-size:20px;font-weight:700;color:#ca8a04;">{med}</div>
|
| 1019 |
-
<div style="font-size:11px;color:#854d0e;">Medium</div>
|
| 1020 |
-
</div>
|
| 1021 |
-
<div style="padding:8px;border-radius:6px;background:#f0fdf4;text-align:center;">
|
| 1022 |
-
<div style="font-size:20px;font-weight:700;color:#16a34a;">{low}</div>
|
| 1023 |
-
<div style="font-size:11px;color:#166534;">Low</div>
|
| 1024 |
-
</div>
|
| 1025 |
-
</div>
|
| 1026 |
-
<div style="font-size:12px;color:#6b7280;text-align:center;">
|
| 1027 |
-
{result['metadata']['total_clauses']} clauses analyzed Β· {result['metadata']['flagged_clauses']} flagged
|
| 1028 |
-
<br><span style="font-size:10px;">{result['metadata']['model']}</span>
|
| 1029 |
-
</div>
|
| 1030 |
-
</div>
|
| 1031 |
-
"""
|
| 1032 |
-
return html
|
| 1033 |
-
|
| 1034 |
-
def render_clause_cards(result):
|
| 1035 |
-
if result is None:
|
| 1036 |
-
return ""
|
| 1037 |
-
clauses = result.get("clauses", [])
|
| 1038 |
-
if not clauses:
|
| 1039 |
-
return '<div style="padding:24px;text-align:center;color:#6b7280;">No clauses detected.</div>'
|
| 1040 |
-
grouped = defaultdict(list)
|
| 1041 |
-
for cr in clauses:
|
| 1042 |
-
grouped[cr["text"]].append(cr)
|
| 1043 |
-
html = '<div style="font-family:system-ui,sans-serif;">'
|
| 1044 |
-
for text, items in grouped.items():
|
| 1045 |
-
max_risk = max(items, key=lambda x: {"CRITICAL":4,"HIGH":3,"MEDIUM":2,"LOW":1}[x["risk"]])["risk"]
|
| 1046 |
-
border, bg, icon = RISK_STYLES[max_risk]
|
| 1047 |
-
tags = ""
|
| 1048 |
-
for item in items:
|
| 1049 |
-
tag_bg = RISK_STYLES[item["risk"]][1]
|
| 1050 |
-
tag_color = RISK_STYLES[item["risk"]][0]
|
| 1051 |
-
conf = item.get("confidence")
|
| 1052 |
-
source = item.get("source", "")
|
| 1053 |
-
if conf is not None:
|
| 1054 |
-
conf_text = f"{conf:.0%}"
|
| 1055 |
-
else:
|
| 1056 |
-
conf_text = "pattern"
|
| 1057 |
-
source_icon = "π€" if source == "ml" else "π"
|
| 1058 |
-
tags += f'<span style="background:{tag_bg};color:{tag_color};border:1px solid {tag_color}33;padding:2px 8px;border-radius:12px;font-size:11px;font-weight:500;margin-right:4px;">{source_icon} {item["label"]} ({conf_text})</span>'
|
| 1059 |
-
descs = "".join(
|
| 1060 |
-
f'<p style="font-size:12px;color:#6b7280;margin:4px 0 0 0;">{item["description"]}</p>'
|
| 1061 |
-
for item in items
|
| 1062 |
-
)
|
| 1063 |
-
preview = text[:300] + ("..." if len(text) > 300 else "")
|
| 1064 |
-
preview = preview.replace("<", "<").replace(">", ">")
|
| 1065 |
-
html += f"""
|
| 1066 |
-
<div style="border:1px solid #e5e7eb;border-left:4px solid {border};border-radius:8px;padding:14px;margin-bottom:10px;background:#fafafa;">
|
| 1067 |
-
<div style="display:flex;align-items:center;gap:6px;margin-bottom:6px;">
|
| 1068 |
-
<span style="font-size:16px;">{icon}</span>
|
| 1069 |
-
<span style="font-size:12px;font-weight:600;color:{border};text-transform:uppercase;">{max_risk}</span>
|
| 1070 |
-
</div>
|
| 1071 |
-
<p style="font-size:13px;color:#374151;line-height:1.6;margin:0 0 8px 0;">{preview}</p>
|
| 1072 |
-
<div style="margin-bottom:6px;">{tags}</div>
|
| 1073 |
-
{descs}
|
| 1074 |
-
</div>
|
| 1075 |
-
"""
|
| 1076 |
-
html += "</div>"
|
| 1077 |
-
return html
|
| 1078 |
-
|
| 1079 |
-
def render_entities(result):
|
| 1080 |
-
if result is None:
|
| 1081 |
-
return ""
|
| 1082 |
-
entities = result.get("entities", [])
|
| 1083 |
-
if not entities:
|
| 1084 |
-
return '<div style="padding:16px;color:#6b7280;">No entities detected.</div>'
|
| 1085 |
-
grouped = defaultdict(list)
|
| 1086 |
-
for e in entities:
|
| 1087 |
-
grouped[e["type"]].append(e["text"])
|
| 1088 |
-
html = '<div style="font-family:system-ui,sans-serif;">'
|
| 1089 |
-
for etype, texts in grouped.items():
|
| 1090 |
-
unique = list(dict.fromkeys(texts))[:20]
|
| 1091 |
-
color = {
|
| 1092 |
-
"DATE": "#3b82f6", "DATE_REF": "#60a5fa",
|
| 1093 |
-
"MONEY": "#22c55e", "PERCENTAGE": "#10b981",
|
| 1094 |
-
"DURATION": "#6366f1",
|
| 1095 |
-
"PARTY": "#8b5cf6", "PARTY_ROLE": "#a78bfa",
|
| 1096 |
-
"PERSON": "#ec4899",
|
| 1097 |
-
"JURISDICTION": "#f59e0b",
|
| 1098 |
-
"DEFINED_TERM": "#ec4899",
|
| 1099 |
-
"LEGAL_REF": "#6b7280",
|
| 1100 |
-
"MISC": "#9ca3af",
|
| 1101 |
-
}.get(etype, "#6b7280")
|
| 1102 |
-
items_html = "".join(
|
| 1103 |
-
f'<span style="display:inline-block;background:{color}15;color:{color};border:1px solid {color}40;padding:3px 10px;border-radius:6px;font-size:12px;margin:3px;">{t}</span>'
|
| 1104 |
-
for t in unique
|
| 1105 |
-
)
|
| 1106 |
-
html += f"""
|
| 1107 |
-
<div style="margin-bottom:12px;">
|
| 1108 |
-
<div style="font-size:12px;font-weight:600;color:#374151;margin-bottom:6px;text-transform:uppercase;">{etype}</div>
|
| 1109 |
-
<div>{items_html}</div>
|
| 1110 |
-
</div>
|
| 1111 |
-
"""
|
| 1112 |
-
html += "</div>"
|
| 1113 |
-
return html
|
| 1114 |
-
|
| 1115 |
-
def render_contradictions(result):
|
| 1116 |
-
if result is None:
|
| 1117 |
-
return ""
|
| 1118 |
-
contradictions = result.get("contradictions", [])
|
| 1119 |
-
if not contradictions:
|
| 1120 |
-
return '<div style="padding:16px;color:#16a34a;">β No contradictions or missing clauses detected.</div>'
|
| 1121 |
-
html = '<div style="font-family:system-ui,sans-serif;">'
|
| 1122 |
-
for c in contradictions:
|
| 1123 |
-
sev_color = RISK_STYLES[c["severity"]][0]
|
| 1124 |
-
icon = "β οΈ" if c["type"] == "CONTRADICTION" else "π"
|
| 1125 |
-
source = c.get("source", "")
|
| 1126 |
-
source_badge = ""
|
| 1127 |
-
if source == "nli_model":
|
| 1128 |
-
conf = c.get("confidence", 0)
|
| 1129 |
-
source_badge = f'<span style="font-size:10px;background:#eff6ff;color:#3b82f6;padding:1px 6px;border-radius:4px;margin-left:8px;">π€ NLI {conf:.0%}</span>'
|
| 1130 |
-
elif source == "heuristic":
|
| 1131 |
-
source_badge = '<span style="font-size:10px;background:#fef3c7;color:#92400e;padding:1px 6px;border-radius:4px;margin-left:8px;">π Heuristic</span>'
|
| 1132 |
-
html += f"""
|
| 1133 |
-
<div style="border:1px solid #e5e7eb;border-left:4px solid {sev_color};border-radius:8px;padding:12px;margin-bottom:8px;background:#fafafa;">
|
| 1134 |
-
<div style="display:flex;align-items:center;gap:6px;margin-bottom:4px;">
|
| 1135 |
-
<span>{icon}</span>
|
| 1136 |
-
<span style="font-size:12px;font-weight:600;color:{sev_color};">{c["type"]}</span>
|
| 1137 |
-
{source_badge}
|
| 1138 |
-
</div>
|
| 1139 |
-
<p style="font-size:13px;color:#374151;margin:0;">{c["explanation"]}</p>
|
| 1140 |
-
</div>
|
| 1141 |
-
"""
|
| 1142 |
-
html += "</div>"
|
| 1143 |
-
return html
|
| 1144 |
-
|
| 1145 |
-
def render_document_viewer(result):
|
| 1146 |
-
if result is None:
|
| 1147 |
-
return ""
|
| 1148 |
-
text = result.get("raw_text", "")
|
| 1149 |
-
entities = sorted(result.get("entities", []), key=lambda x: x["start"])
|
| 1150 |
-
html_parts = []
|
| 1151 |
-
last_end = 0
|
| 1152 |
-
for e in entities:
|
| 1153 |
-
if e["start"] >= last_end:
|
| 1154 |
-
html_parts.append(text[last_end:e["start"]].replace("<", "<").replace(">", ">"))
|
| 1155 |
-
color = {
|
| 1156 |
-
"DATE": "#bfdbfe", "DATE_REF": "#bfdbfe",
|
| 1157 |
-
"MONEY": "#bbf7d0", "PERCENTAGE": "#a7f3d0",
|
| 1158 |
-
"DURATION": "#c7d2fe",
|
| 1159 |
-
"PARTY": "#ddd6fe", "PARTY_ROLE": "#ddd6fe",
|
| 1160 |
-
"PERSON": "#fbcfe8",
|
| 1161 |
-
"JURISDICTION": "#fde68a",
|
| 1162 |
-
"DEFINED_TERM": "#fbcfe8",
|
| 1163 |
-
"LEGAL_REF": "#e5e7eb",
|
| 1164 |
-
}.get(e["type"], "#e5e7eb")
|
| 1165 |
-
label = e["type"].replace("_", " ")
|
| 1166 |
-
html_parts.append(
|
| 1167 |
-
f'<mark style="background:{color};padding:1px 2px;border-radius:2px;font-size:12px;" title="{label}">{e["text"].replace("<","<").replace(">",">")}</mark>'
|
| 1168 |
-
)
|
| 1169 |
-
last_end = e["end"]
|
| 1170 |
-
html_parts.append(text[last_end:].replace("<", "<").replace(">", ">"))
|
| 1171 |
-
highlighted = "".join(html_parts)
|
| 1172 |
-
return f"""
|
| 1173 |
-
<div style="font-family:monospace;font-size:13px;line-height:1.6;padding:16px;border:1px solid #e5e7eb;border-radius:8px;background:#fff;max-height:600px;overflow-y:auto;white-space:pre-wrap;">
|
| 1174 |
-
{highlighted}
|
| 1175 |
-
</div>
|
| 1176 |
-
"""
|
| 1177 |
-
|
| 1178 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1179 |
-
# 12. COMPARISON UI FUNCTIONS
|
| 1180 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1181 |
-
|
| 1182 |
-
def run_comparison(text_a, text_b):
|
| 1183 |
-
if not text_a or len(text_a.strip()) < 50:
|
| 1184 |
-
return "Contract A is too short", ""
|
| 1185 |
-
if not text_b or len(text_b.strip()) < 50:
|
| 1186 |
-
return "Contract B is too short", ""
|
| 1187 |
-
result = compare_contracts(text_a, text_b)
|
| 1188 |
-
return render_comparison_html(result), json.dumps(result, indent=2)
|
| 1189 |
-
|
| 1190 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1191 |
-
# 13. GRADIO UI
|
| 1192 |
-
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1193 |
-
|
| 1194 |
-
def process_upload(file):
|
| 1195 |
-
if file is None:
|
| 1196 |
-
return "", "No file uploaded"
|
| 1197 |
-
text, error = parse_document(file)
|
| 1198 |
-
if error:
|
| 1199 |
-
return "", error
|
| 1200 |
-
return text, "Document loaded successfully"
|
| 1201 |
-
|
| 1202 |
-
def run_analysis(text):
|
| 1203 |
-
if not text or len(text.strip()) < 50:
|
| 1204 |
-
err_html = '<p style="color:#dc2626;padding:16px;">Document too short (minimum 50 characters)</p>'
|
| 1205 |
-
return [err_html] * 8 + [None, None, "", None]
|
| 1206 |
-
result, error = analyze_contract(text)
|
| 1207 |
-
if error:
|
| 1208 |
-
err_html = f'<p style="color:#dc2626;padding:16px;">{error}</p>'
|
| 1209 |
-
return [err_html] * 8 + [None, None, error, None]
|
| 1210 |
-
|
| 1211 |
-
# FIXED: per-session temp files
|
| 1212 |
-
session_id = uuid.uuid4().hex[:8]
|
| 1213 |
-
json_path = os.path.join(tempfile.gettempdir(), f"clauseguard_{session_id}.json")
|
| 1214 |
-
csv_path = os.path.join(tempfile.gettempdir(), f"clauseguard_{session_id}.csv")
|
| 1215 |
-
|
| 1216 |
-
with open(json_path, "w") as f:
|
| 1217 |
-
json.dump(result, f, indent=2, default=str)
|
| 1218 |
-
csv_content = export_csv(result)
|
| 1219 |
-
with open(csv_path, "w") as f:
|
| 1220 |
-
f.write(csv_content)
|
| 1221 |
-
|
| 1222 |
-
# Generate redline suggestions (Tier 1 template + Tier 3 LLM for critical/high)
|
| 1223 |
-
redlines = generate_redlines(result, use_llm=True)
|
| 1224 |
-
redlines_html = render_redlines_html(redlines)
|
| 1225 |
-
|
| 1226 |
-
return [
|
| 1227 |
-
render_summary(result),
|
| 1228 |
-
render_clause_cards(result),
|
| 1229 |
-
render_entities(result),
|
| 1230 |
-
render_contradictions(result),
|
| 1231 |
-
render_document_viewer(result),
|
| 1232 |
-
render_obligations_html(result.get("obligations", [])),
|
| 1233 |
-
render_compliance_html(result.get("compliance", {})),
|
| 1234 |
-
redlines_html,
|
| 1235 |
-
json_path,
|
| 1236 |
-
csv_path,
|
| 1237 |
-
"Analysis complete",
|
| 1238 |
-
result, # Store analysis result for chatbot
|
| 1239 |
-
]
|
| 1240 |
-
|
| 1241 |
-
def do_clear():
|
| 1242 |
-
return [""] * 8 + [None, None, "", None]
|
| 1243 |
-
|
| 1244 |
-
# ββ Example contracts ββ
|
| 1245 |
-
SPOTIFY_TOS = """By using the Spotify Service, you agree to be bound by these Terms of Use.
|
| 1246 |
-
|
| 1247 |
-
Spotify may, in its sole discretion, modify or update these Terms of Service at any time without prior notice. Your continued use of the Service after any such changes constitutes your acceptance of the new Terms of Service.
|
| 1248 |
-
|
| 1249 |
-
In no event will Spotify be liable for any indirect, incidental, special, consequential, or punitive damages, or any loss of profits or revenues, whether incurred directly or indirectly.
|
| 1250 |
-
|
| 1251 |
-
Spotify reserves the right to remove or disable access to any User Content for any reason, without prior notice.
|
| 1252 |
-
|
| 1253 |
-
Spotify may terminate your account or suspend your access at any time, with or without cause, with or without notice, effective immediately.
|
| 1254 |
-
|
| 1255 |
-
These Terms will be governed by and construed in accordance with the laws of the State of New York.
|
| 1256 |
-
|
| 1257 |
-
Any dispute shall be finally settled by arbitration in New York County. The parties waive any right to a jury trial."""
|
| 1258 |
-
|
| 1259 |
-
RENTAL_AGREEMENT = """The Landlord reserves the right to enter the premises at any time without prior notice for inspection or any other purpose deemed necessary in their sole discretion.
|
| 1260 |
-
|
| 1261 |
-
The Landlord shall not be liable for any damage to the Tenant's personal property, whether caused by water leaks, fire, theft, or any other cause, including the Landlord's own negligence.
|
| 1262 |
-
|
| 1263 |
-
The Landlord may terminate this lease at any time with only 7 days written notice, for any reason or no reason at all.
|
| 1264 |
-
|
| 1265 |
-
Any disputes arising from this lease agreement shall be resolved exclusively in the courts of the State of California, and the Tenant waives the right to a jury trial.
|
| 1266 |
-
|
| 1267 |
-
The Landlord reserves the right to modify the terms of this lease at any time. Continued occupancy constitutes acceptance of the new terms."""
|
| 1268 |
-
|
| 1269 |
-
NDA_SAMPLE = """NON-DISCLOSURE AGREEMENT
|
| 1270 |
-
|
| 1271 |
-
This Non-Disclosure Agreement (the "Agreement") is entered into as of January 15, 2024 (the "Effective Date") by and between Acme Technologies, Inc. ("Disclosing Party") and Beta Solutions LLC ("Receiving Party").
|
| 1272 |
-
|
| 1273 |
-
1. Governing Law. This Agreement shall be governed by and construed in accordance with the laws of the State of Delaware, without regard to its conflict of law principles.
|
| 1274 |
-
|
| 1275 |
-
2. Term. This Agreement shall remain in effect for a period of three (3) years from the Effective Date.
|
| 1276 |
-
|
| 1277 |
-
3. Termination. Either party may terminate this Agreement for convenience upon thirty (30) days prior written notice.
|
| 1278 |
-
|
| 1279 |
-
4. Intellectual Property. All Confidential Information disclosed hereunder shall remain the exclusive property of the Disclosing Party. The Receiving Party hereby assigns to the Disclosing Party all right, title, and interest in any derivative works.
|
| 1280 |
-
|
| 1281 |
-
5. Limitation of Liability. IN NO EVENT SHALL EITHER PARTY BE LIABLE FOR ANY INDIRECT, INCIDENTAL, SPECIAL, CONSEQUENTIAL, OR PUNITIVE DAMAGES.
|
| 1282 |
-
|
| 1283 |
-
6. Indemnification. The Receiving Party shall indemnify and hold harmless the Disclosing Party from any and all claims arising from a breach of this Agreement.
|
| 1284 |
-
|
| 1285 |
-
7. Non-Compete. During the term of this Agreement and for a period of two (2) years thereafter, the Receiving Party shall not engage in any business that competes with the Disclosing Party."""
|
| 1286 |
-
|
| 1287 |
-
COMPLEX_CONTRACT = """MASTER SERVICE AGREEMENT
|
| 1288 |
-
|
| 1289 |
-
This Master Service Agreement ("MSA") is entered into as of March 1, 2024 (the "Effective Date") by and between CloudTech Solutions, Inc., a Delaware corporation ("Provider") and Global Retail Partners LLC, a New York limited liability company ("Customer").
|
| 1290 |
-
|
| 1291 |
-
1. SERVICES. Provider shall provide cloud hosting and data processing services as described in Exhibit A. Provider shall comply with all applicable laws including GDPR and CCPA.
|
| 1292 |
-
|
| 1293 |
-
2. TERM AND RENEWAL. The initial term is twelve (12) months, automatically renewing for successive one (1) year periods unless terminated in accordance with Section 7.
|
| 1294 |
-
|
| 1295 |
-
3. FEES AND PAYMENT. Customer shall pay a monthly fee of $25,000 within 30 days of invoice. Late payments incur a penalty of 1.5% per month. The total contract value is $300,000.
|
| 1296 |
-
|
| 1297 |
-
4. LIABILITY. Provider's aggregate liability shall not exceed $1,000,000. IN NO EVENT SHALL PROVIDER BE LIABLE FOR LOST PROFITS OR CONSEQUENTIAL DAMAGES. Customer assumes all risk of data loss.
|
| 1298 |
-
|
| 1299 |
-
5. INDEMNIFICATION. Each party shall indemnify the other for third-party claims arising from breach of this Agreement. Customer shall indemnify Provider for claims arising from Customer Data.
|
| 1300 |
-
|
| 1301 |
-
6. INTELLECTUAL PROPERTY. Provider retains all IP rights. Customer receives a non-transferable, non-exclusive license for the term. Upon termination, Customer shall return or destroy all Provider materials within 10 business days.
|
| 1302 |
-
|
| 1303 |
-
7. TERMINATION. Either party may terminate for convenience with 90 days notice. Provider may terminate immediately for non-payment. Upon termination, Customer shall pay all outstanding fees.
|
| 1304 |
-
|
| 1305 |
-
8. GOVERNING LAW. This Agreement is governed by the laws of the State of Delaware. Disputes shall be resolved by binding arbitration in Wilmington, Delaware.
|
| 1306 |
-
|
| 1307 |
-
9. FORCE MAJEURE. Neither party shall be liable for delays due to acts of God, war, terrorism, or government action.
|
| 1308 |
-
|
| 1309 |
-
10. AUDIT RIGHTS. Customer may audit Provider's compliance annually. Provider shall provide SOC 2 Type II reports within 30 days of request.
|
| 1310 |
-
|
| 1311 |
-
11. INSURANCE. Provider shall maintain general liability insurance of at least $5,000,000 and cyber liability insurance of at least $2,000,000.
|
| 1312 |
-
|
| 1313 |
-
12. CONFIDENTIALITY. Both parties agree to keep Confidential Information secure for five (5) years. This obligation survives termination.
|
| 1314 |
-
|
| 1315 |
-
13. ASSIGNMENT. Neither party may assign this Agreement without prior written consent. Any attempted assignment is void.
|
| 1316 |
-
|
| 1317 |
-
14. THIRD PARTY BENEFICIARY. No third party shall have rights under this Agreement except as expressly provided."""
|
| 1318 |
-
|
| 1319 |
-
with gr.Blocks(
|
| 1320 |
-
title="ClauseGuard β AI Contract Analysis",
|
| 1321 |
-
css="""
|
| 1322 |
-
.gradio-container { max-width: 1600px !important; }
|
| 1323 |
-
"""
|
| 1324 |
-
) as demo:
|
| 1325 |
-
|
| 1326 |
-
# ββ Shared State (for chatbot RAG) ββββββββββββββββββββββββββββββ
|
| 1327 |
-
analysis_state = gr.State(None) # Full analysis result dict
|
| 1328 |
-
chunks_state = gr.State([]) # Contract text chunks for RAG
|
| 1329 |
-
embeddings_state = gr.State(None) # Chunk embeddings (numpy array)
|
| 1330 |
-
|
| 1331 |
-
gr.HTML("""
|
| 1332 |
-
<div style="display:flex;align-items:center;justify-content:space-between;padding:12px 0;border-bottom:2px solid #e5e7eb;margin-bottom:16px;">
|
| 1333 |
-
<div>
|
| 1334 |
-
<h1 style="font-size:24px;font-weight:700;margin:0;color:#1f2937;">π‘οΈ ClauseGuard</h1>
|
| 1335 |
-
<p style="font-size:13px;color:#6b7280;margin:4px 0 0 0;">AI-Powered Legal Contract Analysis Β· 41 Clause Categories Β· Risk Scoring Β· ML NER Β· NLI Contradictions Β· Compliance Β· Obligations Β· <strong>Q&A Chatbot</strong> Β· <strong>Clause Redlining</strong> Β· <strong>OCR</strong></p>
|
| 1336 |
-
</div>
|
| 1337 |
-
<div style="font-size:12px;color:#9ca3af;">v4.0 Β· Precision Legal AI</div>
|
| 1338 |
-
</div>
|
| 1339 |
-
""")
|
| 1340 |
-
|
| 1341 |
-
# ββ Main Tabs: Analysis vs Comparison vs Chatbot ββ
|
| 1342 |
-
with gr.Tabs():
|
| 1343 |
-
|
| 1344 |
-
# βββββββ TAB 1: Single Contract Analysis βββββββ
|
| 1345 |
-
with gr.Tab("π Single Contract Analysis"):
|
| 1346 |
-
with gr.Row():
|
| 1347 |
-
with gr.Column(scale=1):
|
| 1348 |
-
file_input = gr.File(
|
| 1349 |
-
label="π Upload Contract (PDF/DOCX/TXT)",
|
| 1350 |
-
file_types=[".pdf", ".docx", ".doc", ".txt", ".md"],
|
| 1351 |
-
)
|
| 1352 |
-
load_btn = gr.Button("Load Document", variant="secondary", size="sm")
|
| 1353 |
-
load_status = gr.Textbox(label="Status", interactive=False, lines=1)
|
| 1354 |
-
|
| 1355 |
-
with gr.Column(scale=3):
|
| 1356 |
-
text_input = gr.Textbox(
|
| 1357 |
-
label="π Contract Text",
|
| 1358 |
-
placeholder="Paste contract text here, or upload a file above...\n\nπ‘ Scanned PDFs are automatically processed with OCR.",
|
| 1359 |
-
lines=14,
|
| 1360 |
-
max_lines=40,
|
| 1361 |
-
show_copy_button=True,
|
| 1362 |
-
)
|
| 1363 |
-
|
| 1364 |
-
with gr.Column(scale=1):
|
| 1365 |
-
scan_btn = gr.Button("π Analyze Contract", variant="primary", size="lg")
|
| 1366 |
-
clear_btn = gr.Button("Clear", variant="secondary", size="sm")
|
| 1367 |
-
status_msg = gr.Textbox(label="Analysis Status", interactive=False, lines=1)
|
| 1368 |
-
|
| 1369 |
-
# ββ Examples ββ
|
| 1370 |
-
with gr.Row():
|
| 1371 |
-
gr.Examples(
|
| 1372 |
-
examples=[[SPOTIFY_TOS], [RENTAL_AGREEMENT], [NDA_SAMPLE], [COMPLEX_CONTRACT]],
|
| 1373 |
-
inputs=[text_input],
|
| 1374 |
-
label="Example Contracts",
|
| 1375 |
-
)
|
| 1376 |
-
|
| 1377 |
-
# ββ Results ββ
|
| 1378 |
-
with gr.Row():
|
| 1379 |
-
with gr.Column(scale=1):
|
| 1380 |
-
gr.Markdown("### π Risk Summary")
|
| 1381 |
-
summary_html = gr.HTML()
|
| 1382 |
-
|
| 1383 |
-
gr.Markdown("### π₯ Export Reports")
|
| 1384 |
-
json_file = gr.File(label="JSON Report")
|
| 1385 |
-
csv_file = gr.File(label="CSV Report")
|
| 1386 |
-
|
| 1387 |
-
with gr.Column(scale=3):
|
| 1388 |
-
with gr.Tabs():
|
| 1389 |
-
with gr.Tab("π Document"):
|
| 1390 |
-
doc_html = gr.HTML(label="Document Viewer")
|
| 1391 |
-
with gr.Tab("β οΈ Clauses (41 Categories)"):
|
| 1392 |
-
clauses_html = gr.HTML(label="Detected Clauses")
|
| 1393 |
-
with gr.Tab("π·οΈ Entities"):
|
| 1394 |
-
entities_html = gr.HTML(label="Named Entities")
|
| 1395 |
-
with gr.Tab("π Contradictions"):
|
| 1396 |
-
nli_html = gr.HTML(label="Contradictions & Missing Clauses")
|
| 1397 |
-
with gr.Tab("π Obligations"):
|
| 1398 |
-
obligations_html = gr.HTML(label="Obligation Tracker")
|
| 1399 |
-
with gr.Tab("βοΈ Compliance"):
|
| 1400 |
-
compliance_html = gr.HTML(label="Compliance Checker")
|
| 1401 |
-
with gr.Tab("βοΈ Redlining"):
|
| 1402 |
-
redlining_html = gr.HTML(label="Clause Redlining Suggestions")
|
| 1403 |
-
|
| 1404 |
-
# βββββββ TAB 2: Contract Comparison βββββββ
|
| 1405 |
-
with gr.Tab("π Compare Contracts"):
|
| 1406 |
-
with gr.Row():
|
| 1407 |
-
with gr.Column(scale=1):
|
| 1408 |
-
comp_file_a = gr.File(
|
| 1409 |
-
label="π Contract A (PDF/DOCX/TXT)",
|
| 1410 |
-
file_types=[".pdf", ".docx", ".doc", ".txt"],
|
| 1411 |
-
)
|
| 1412 |
-
comp_load_a = gr.Button("Load A", variant="secondary", size="sm")
|
| 1413 |
-
comp_status_a = gr.Textbox(label="Status A", interactive=False, lines=1)
|
| 1414 |
-
|
| 1415 |
-
with gr.Column(scale=3):
|
| 1416 |
-
comp_text_a = gr.Textbox(
|
| 1417 |
-
label="Contract A",
|
| 1418 |
-
placeholder="Paste contract A here...",
|
| 1419 |
-
lines=12,
|
| 1420 |
-
show_copy_button=True,
|
| 1421 |
-
)
|
| 1422 |
-
|
| 1423 |
-
with gr.Column(scale=1):
|
| 1424 |
-
comp_file_b = gr.File(
|
| 1425 |
-
label="π Contract B (PDF/DOCX/TXT)",
|
| 1426 |
-
file_types=[".pdf", ".docx", ".doc", ".txt"],
|
| 1427 |
-
)
|
| 1428 |
-
comp_load_b = gr.Button("Load B", variant="secondary", size="sm")
|
| 1429 |
-
comp_status_b = gr.Textbox(label="Status B", interactive=False, lines=1)
|
| 1430 |
-
|
| 1431 |
-
with gr.Column(scale=3):
|
| 1432 |
-
comp_text_b = gr.Textbox(
|
| 1433 |
-
label="Contract B",
|
| 1434 |
-
placeholder="Paste contract B here...",
|
| 1435 |
-
lines=12,
|
| 1436 |
-
show_copy_button=True,
|
| 1437 |
-
)
|
| 1438 |
-
|
| 1439 |
-
with gr.Row():
|
| 1440 |
-
with gr.Column(scale=1):
|
| 1441 |
-
comp_btn = gr.Button("π Compare Contracts", variant="primary", size="lg")
|
| 1442 |
-
with gr.Column(scale=5):
|
| 1443 |
-
comp_status = gr.Textbox(label="Comparison Status", interactive=False, lines=1)
|
| 1444 |
-
|
| 1445 |
-
with gr.Row():
|
| 1446 |
-
with gr.Column(scale=4):
|
| 1447 |
-
comp_result_html = gr.HTML(label="Comparison Results")
|
| 1448 |
-
with gr.Column(scale=2):
|
| 1449 |
-
comp_json = gr.JSON(label="Raw Comparison Data")
|
| 1450 |
-
|
| 1451 |
-
# βββββββ TAB 3: Contract Q&A Chatbot βββββββ
|
| 1452 |
-
with gr.Tab("π¬ Contract Q&A"):
|
| 1453 |
-
gr.HTML("""
|
| 1454 |
-
<div style="padding:12px 16px;background:linear-gradient(135deg,#eff6ff,#faf5ff);border-radius:10px;margin-bottom:12px;border:1px solid #e5e7eb;">
|
| 1455 |
-
<div style="display:flex;align-items:center;gap:8px;margin-bottom:6px;">
|
| 1456 |
-
<span style="font-size:20px;">π¬</span>
|
| 1457 |
-
<h3 style="margin:0;font-size:16px;color:#1f2937;">Contract Q&A Chatbot</h3>
|
| 1458 |
-
</div>
|
| 1459 |
-
<p style="font-size:12px;color:#6b7280;margin:0;line-height:1.5;">
|
| 1460 |
-
Ask questions about your analyzed contract. The chatbot uses <strong>RAG</strong> (Retrieval-Augmented Generation)
|
| 1461 |
-
to find relevant clauses and generate accurate answers grounded in your contract text.
|
| 1462 |
-
<br>
|
| 1463 |
-
<strong>Step 1:</strong> Analyze a contract in the "π Single Contract Analysis" tab.
|
| 1464 |
-
<strong>Step 2:</strong> Come here and ask questions!
|
| 1465 |
-
</p>
|
| 1466 |
-
</div>
|
| 1467 |
-
""")
|
| 1468 |
-
|
| 1469 |
-
chatbot_index_status = gr.Textbox(
|
| 1470 |
-
label="π‘ Chatbot Index Status",
|
| 1471 |
-
interactive=False,
|
| 1472 |
-
lines=1,
|
| 1473 |
-
value="β³ No contract indexed yet β analyze a contract first",
|
| 1474 |
-
)
|
| 1475 |
-
|
| 1476 |
-
def _chatbot_fn(message, history, chunks, embeddings, analysis):
|
| 1477 |
-
"""Wrapper for ChatInterface fn signature."""
|
| 1478 |
-
yield from chat_respond(message, history, chunks, embeddings, analysis)
|
| 1479 |
-
|
| 1480 |
-
gr.ChatInterface(
|
| 1481 |
-
fn=_chatbot_fn,
|
| 1482 |
-
type="messages",
|
| 1483 |
-
additional_inputs=[chunks_state, embeddings_state, analysis_state],
|
| 1484 |
-
examples=[
|
| 1485 |
-
["What are the main risks in this contract?"],
|
| 1486 |
-
["Who are the parties involved?"],
|
| 1487 |
-
["What happens if the contract is terminated?"],
|
| 1488 |
-
["Are there any liability limitations?"],
|
| 1489 |
-
["What are my obligations under this contract?"],
|
| 1490 |
-
["Is there an arbitration clause?"],
|
| 1491 |
-
["What is the governing law?"],
|
| 1492 |
-
["Summarize the key terms in plain language."],
|
| 1493 |
-
],
|
| 1494 |
-
title="",
|
| 1495 |
-
description="",
|
| 1496 |
-
)
|
| 1497 |
-
|
| 1498 |
-
# ββ Events ββ
|
| 1499 |
-
def _load_file(file):
|
| 1500 |
-
text, err = parse_document(file) if file else ("", "No file")
|
| 1501 |
-
if err and not text:
|
| 1502 |
-
return "", err
|
| 1503 |
-
return text, "Loaded successfully" if not err else err
|
| 1504 |
-
|
| 1505 |
-
def _analysis_and_index(text):
|
| 1506 |
-
"""Run analysis AND index for chatbot in one call."""
|
| 1507 |
-
# Run the standard analysis
|
| 1508 |
-
analysis_outputs = run_analysis(text)
|
| 1509 |
-
|
| 1510 |
-
# Index for chatbot (uses the raw text)
|
| 1511 |
-
chunks, embeddings, index_status = index_contract(text)
|
| 1512 |
-
|
| 1513 |
-
# analysis_outputs has 12 items: 8 HTML + json_path + csv_path + status + result
|
| 1514 |
-
# We need to add: chunks_state, embeddings_state, chatbot_index_status
|
| 1515 |
-
return analysis_outputs + [chunks, embeddings, index_status]
|
| 1516 |
-
|
| 1517 |
-
load_btn.click(_load_file, inputs=[file_input], outputs=[text_input, load_status])
|
| 1518 |
-
comp_load_a.click(_load_file, inputs=[comp_file_a], outputs=[comp_text_a, comp_status_a])
|
| 1519 |
-
comp_load_b.click(_load_file, inputs=[comp_file_b], outputs=[comp_text_b, comp_status_b])
|
| 1520 |
-
|
| 1521 |
-
scan_btn.click(
|
| 1522 |
-
_analysis_and_index,
|
| 1523 |
-
inputs=[text_input],
|
| 1524 |
-
outputs=[
|
| 1525 |
-
summary_html, clauses_html, entities_html, nli_html,
|
| 1526 |
-
doc_html, obligations_html, compliance_html, redlining_html,
|
| 1527 |
-
json_file, csv_file, status_msg, analysis_state,
|
| 1528 |
-
chunks_state, embeddings_state, chatbot_index_status,
|
| 1529 |
-
]
|
| 1530 |
-
)
|
| 1531 |
-
|
| 1532 |
-
clear_btn.click(
|
| 1533 |
-
lambda: [""] * 8 + [None, None, "", None, [], None, "β³ No contract indexed"],
|
| 1534 |
-
outputs=[
|
| 1535 |
-
summary_html, clauses_html, entities_html, nli_html,
|
| 1536 |
-
doc_html, obligations_html, compliance_html, redlining_html,
|
| 1537 |
-
json_file, csv_file, status_msg, analysis_state,
|
| 1538 |
-
chunks_state, embeddings_state, chatbot_index_status,
|
| 1539 |
-
]
|
| 1540 |
-
)
|
| 1541 |
-
|
| 1542 |
-
comp_btn.click(
|
| 1543 |
-
run_comparison,
|
| 1544 |
-
inputs=[comp_text_a, comp_text_b],
|
| 1545 |
-
outputs=[comp_result_html, comp_json]
|
| 1546 |
-
)
|
| 1547 |
-
|
| 1548 |
-
gr.HTML("""
|
| 1549 |
-
<div style="margin-top:24px;padding:16px 0;border-top:1px solid #e5e7eb;text-align:center;">
|
| 1550 |
-
<p style="font-size:11px;color:#9ca3af;">
|
| 1551 |
-
β οΈ Not legal advice. For informational purposes only.
|
| 1552 |
-
Β· Model: <a href="https://huggingface.co/Mokshith31/legalbert-contract-clause-classification" style="color:#6b7280;">Legal-BERT + CUAD (41 classes)</a>
|
| 1553 |
-
Β· NER: <a href="https://huggingface.co/matterstack/legal-bert-ner" style="color:#6b7280;">Legal-BERT NER</a>
|
| 1554 |
-
Β· NLI: <a href="https://huggingface.co/cross-encoder/nli-deberta-v3-base" style="color:#6b7280;">DeBERTa-v3 NLI</a>
|
| 1555 |
-
Β· LLM: <a href="https://huggingface.co/Qwen/Qwen2.5-7B-Instruct" style="color:#6b7280;">Qwen2.5-7B</a>
|
| 1556 |
-
Β· OCR: <a href="https://github.com/mindee/doctr" style="color:#6b7280;">docTR</a>
|
| 1557 |
-
Β· Dataset: <a href="https://huggingface.co/datasets/theatticusproject/cuad-qa" style="color:#6b7280;">CUAD</a>
|
| 1558 |
-
Β· <a href="https://huggingface.co/spaces/gaurv007/ClauseGuard" style="color:#6b7280;">ClauseGuard Space</a>
|
| 1559 |
-
</p>
|
| 1560 |
-
</div>
|
| 1561 |
-
""")
|
| 1562 |
-
|
| 1563 |
-
if __name__ == "__main__":
|
| 1564 |
-
demo.launch()
|
|
|
|
| 1 |
+
file:/app/app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1,318 +1 @@
|
|
| 1 |
-
|
| 2 |
-
ClauseGuard β Contract Comparison Engine v3.0
|
| 3 |
-
βββββββββββββββββββββββββββββββββββββββββββββ
|
| 4 |
-
FIXED in v3.0:
|
| 5 |
-
β’ Semantic similarity using sentence embeddings (when available)
|
| 6 |
-
β’ Better clause type detection with legal taxonomy
|
| 7 |
-
β’ Improved diff visualization
|
| 8 |
-
β’ Fallback to SequenceMatcher when embeddings unavailable
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
import re
|
| 12 |
-
from difflib import SequenceMatcher
|
| 13 |
-
from collections import defaultdict
|
| 14 |
-
|
| 15 |
-
# Try to load sentence-transformers for semantic comparison
|
| 16 |
-
_HAS_EMBEDDINGS = False
|
| 17 |
-
_embedder = None
|
| 18 |
-
|
| 19 |
-
try:
|
| 20 |
-
from sentence_transformers import SentenceTransformer, util
|
| 21 |
-
_HAS_EMBEDDINGS = True
|
| 22 |
-
except ImportError:
|
| 23 |
-
pass
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def _load_embedder():
|
| 27 |
-
global _embedder
|
| 28 |
-
if _HAS_EMBEDDINGS and _embedder is None:
|
| 29 |
-
try:
|
| 30 |
-
_embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 31 |
-
print("[ClauseGuard] Sentence embeddings loaded for comparison")
|
| 32 |
-
except Exception as e:
|
| 33 |
-
print(f"[ClauseGuard] Embeddings not available: {e}")
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
def _normalize_clause(text):
|
| 37 |
-
"""Normalize clause text for comparison."""
|
| 38 |
-
text = text.lower()
|
| 39 |
-
text = re.sub(r'[^a-z0-9\s]', ' ', text)
|
| 40 |
-
text = re.sub(r'\s+', ' ', text).strip()
|
| 41 |
-
return text
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
def _clause_similarity(a, b):
|
| 45 |
-
"""Compute similarity using semantic embeddings or string matching."""
|
| 46 |
-
if _embedder is not None:
|
| 47 |
-
try:
|
| 48 |
-
emb_a = _embedder.encode(a[:512], convert_to_tensor=True)
|
| 49 |
-
emb_b = _embedder.encode(b[:512], convert_to_tensor=True)
|
| 50 |
-
sim = util.cos_sim(emb_a, emb_b).item()
|
| 51 |
-
return max(0, min(1, sim))
|
| 52 |
-
except Exception:
|
| 53 |
-
pass
|
| 54 |
-
# Fallback to string matching
|
| 55 |
-
return SequenceMatcher(None, _normalize_clause(a), _normalize_clause(b)).ratio()
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def _extract_clause_type(clause_text):
|
| 59 |
-
"""Clause type detection with legal taxonomy."""
|
| 60 |
-
text_lower = clause_text.lower()
|
| 61 |
-
type_keywords = {
|
| 62 |
-
"governing law": ["govern", "law of", "jurisdiction of", "applicable law"],
|
| 63 |
-
"termination": ["terminat", "cancel", "expir"],
|
| 64 |
-
"indemnification": ["indemnif", "hold harmless", "defend and indemnify"],
|
| 65 |
-
"confidentiality": ["confidential", "non-disclosure", "nda", "proprietary"],
|
| 66 |
-
"liability": ["liability", "liable", "damages", "limitation of"],
|
| 67 |
-
"payment": ["payment", "fee", "price", "compensat", "invoice", "remit"],
|
| 68 |
-
"intellectual property": ["intellectual property", "ip rights", "copyright", "patent", "trademark"],
|
| 69 |
-
"warranty": ["warrant", "guarantee", "representation"],
|
| 70 |
-
"force majeure": ["force majeure", "act of god", "beyond control"],
|
| 71 |
-
"arbitration": ["arbitrat", "mediation", "dispute resolution"],
|
| 72 |
-
"assignment": ["assign", "transfer of rights"],
|
| 73 |
-
"non-compete": ["non-compete", "not compete", "competition"],
|
| 74 |
-
"renewal": ["renew", "extend", "automatic renewal"],
|
| 75 |
-
"effective date": ["effective date", "commencement"],
|
| 76 |
-
"insurance": ["insurance", "coverage", "policy of insurance"],
|
| 77 |
-
"audit": ["audit", "inspection", "examination of records"],
|
| 78 |
-
"data protection": ["data protection", "privacy", "personal data", "gdpr", "ccpa"],
|
| 79 |
-
"notice": ["notice", "notification", "written notice"],
|
| 80 |
-
}
|
| 81 |
-
for ctype, keywords in type_keywords.items():
|
| 82 |
-
if any(kw in text_lower for kw in keywords):
|
| 83 |
-
return ctype
|
| 84 |
-
return "general"
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
def compare_contracts(text_a, text_b, clauses_a=None, clauses_b=None):
|
| 88 |
-
"""Compare two contracts with semantic similarity."""
|
| 89 |
-
if not text_a or not text_b:
|
| 90 |
-
return {"error": "Both contracts required"}
|
| 91 |
-
|
| 92 |
-
# Try to load embedder
|
| 93 |
-
_load_embedder()
|
| 94 |
-
|
| 95 |
-
# Split into clauses if not provided
|
| 96 |
-
if clauses_a is None:
|
| 97 |
-
clauses_a = _split_clauses(text_a)
|
| 98 |
-
if clauses_b is None:
|
| 99 |
-
clauses_b = _split_clauses(text_b)
|
| 100 |
-
|
| 101 |
-
# Fix 9: Detect contract types and flag cross-domain comparisons
|
| 102 |
-
_CONTRACT_TYPE_KEYWORDS = {
|
| 103 |
-
"employment": ["employee", "employer", "salary", "compensation", "benefits", "vacation", "severance", "at-will"],
|
| 104 |
-
"lease": ["landlord", "tenant", "rent", "premises", "lease", "occupancy", "security deposit", "eviction"],
|
| 105 |
-
"service": ["service provider", "customer", "SLA", "deliverables", "statement of work", "SOW"],
|
| 106 |
-
"nda": ["confidential", "non-disclosure", "disclosing party", "receiving party"],
|
| 107 |
-
"saas": ["subscription", "SaaS", "cloud", "uptime", "API", "data processing"],
|
| 108 |
-
"purchase": ["buyer", "seller", "purchase order", "goods", "shipment", "delivery"],
|
| 109 |
-
}
|
| 110 |
-
|
| 111 |
-
def _detect_contract_type(text):
|
| 112 |
-
text_lower = text.lower()
|
| 113 |
-
scores = {}
|
| 114 |
-
for ctype, keywords in _CONTRACT_TYPE_KEYWORDS.items():
|
| 115 |
-
scores[ctype] = sum(1 for kw in keywords if kw.lower() in text_lower)
|
| 116 |
-
best = max(scores, key=scores.get)
|
| 117 |
-
return best if scores[best] >= 2 else "general"
|
| 118 |
-
|
| 119 |
-
type_a = _detect_contract_type(text_a)
|
| 120 |
-
type_b = _detect_contract_type(text_b)
|
| 121 |
-
is_cross_domain = type_a != type_b and type_a != "general" and type_b != "general"
|
| 122 |
-
|
| 123 |
-
# Build clause type maps
|
| 124 |
-
type_map_a = defaultdict(list)
|
| 125 |
-
type_map_b = defaultdict(list)
|
| 126 |
-
for c in clauses_a:
|
| 127 |
-
type_map_a[_extract_clause_type(c)].append(c)
|
| 128 |
-
for c in clauses_b:
|
| 129 |
-
type_map_b[_extract_clause_type(c)].append(c)
|
| 130 |
-
|
| 131 |
-
# Find matches
|
| 132 |
-
matched_a = set()
|
| 133 |
-
matched_b = set()
|
| 134 |
-
modified = []
|
| 135 |
-
|
| 136 |
-
# Fix 10: Raise thresholds to reject false "modified" matches
|
| 137 |
-
SIMILARITY_THRESHOLD = 0.75 # was 0.70 β too many false matches
|
| 138 |
-
MODIFIED_THRESHOLD = 0.55 # was 0.40 β "Good Reason" β "Force Majeure"
|
| 139 |
-
|
| 140 |
-
for i, ca in enumerate(clauses_a):
|
| 141 |
-
best_sim = 0
|
| 142 |
-
best_j = -1
|
| 143 |
-
for j, cb in enumerate(clauses_b):
|
| 144 |
-
if j in matched_b:
|
| 145 |
-
continue
|
| 146 |
-
sim = _clause_similarity(ca, cb)
|
| 147 |
-
if sim > best_sim:
|
| 148 |
-
best_sim = sim
|
| 149 |
-
best_j = j
|
| 150 |
-
|
| 151 |
-
if best_sim >= SIMILARITY_THRESHOLD:
|
| 152 |
-
matched_a.add(i)
|
| 153 |
-
matched_b.add(best_j)
|
| 154 |
-
if best_sim < 0.95:
|
| 155 |
-
modified.append({
|
| 156 |
-
"type": "modified",
|
| 157 |
-
"similarity": round(best_sim, 3),
|
| 158 |
-
"clause_a": ca[:200],
|
| 159 |
-
"clause_b": clauses_b[best_j][:200],
|
| 160 |
-
"clause_type": _extract_clause_type(ca),
|
| 161 |
-
})
|
| 162 |
-
elif best_sim >= MODIFIED_THRESHOLD:
|
| 163 |
-
matched_a.add(i)
|
| 164 |
-
if best_j >= 0:
|
| 165 |
-
matched_b.add(best_j)
|
| 166 |
-
modified.append({
|
| 167 |
-
"type": "partial",
|
| 168 |
-
"similarity": round(best_sim, 3),
|
| 169 |
-
"clause_a": ca[:200],
|
| 170 |
-
"clause_b": clauses_b[best_j][:200] if best_j >= 0 else "",
|
| 171 |
-
"clause_type": _extract_clause_type(ca),
|
| 172 |
-
})
|
| 173 |
-
|
| 174 |
-
removed = [clauses_a[i] for i in range(len(clauses_a)) if i not in matched_a]
|
| 175 |
-
added = [clauses_b[j] for j in range(len(clauses_b)) if j not in matched_b]
|
| 176 |
-
|
| 177 |
-
# Compute alignment score
|
| 178 |
-
total_pairs = max(len(clauses_a), len(clauses_b))
|
| 179 |
-
if total_pairs > 0:
|
| 180 |
-
alignment = len(matched_a) / total_pairs
|
| 181 |
-
else:
|
| 182 |
-
alignment = 0.0
|
| 183 |
-
|
| 184 |
-
# Risk delta: compare risk keywords with context
|
| 185 |
-
risk_keywords = ["unlimited", "unilateral", "waive", "arbitration", "indemnif",
|
| 186 |
-
"not liable", "no warranty", "sole discretion", "terminate",
|
| 187 |
-
"non-compete", "liquidated damages", "uncapped"]
|
| 188 |
-
risk_a = sum(1 for kw in risk_keywords if kw in text_a.lower())
|
| 189 |
-
risk_b = sum(1 for kw in risk_keywords if kw in text_b.lower())
|
| 190 |
-
|
| 191 |
-
if risk_a > risk_b + 2:
|
| 192 |
-
risk_delta = "Contract A is significantly riskier"
|
| 193 |
-
risk_winner = "B"
|
| 194 |
-
elif risk_b > risk_a + 2:
|
| 195 |
-
risk_delta = "Contract B is significantly riskier"
|
| 196 |
-
risk_winner = "A"
|
| 197 |
-
elif risk_a > risk_b:
|
| 198 |
-
risk_delta = "Contract A is slightly riskier"
|
| 199 |
-
risk_winner = "B"
|
| 200 |
-
elif risk_b > risk_a:
|
| 201 |
-
risk_delta = "Contract B is slightly riskier"
|
| 202 |
-
risk_winner = "A"
|
| 203 |
-
else:
|
| 204 |
-
risk_delta = "Similar risk profiles"
|
| 205 |
-
risk_winner = "tie"
|
| 206 |
-
|
| 207 |
-
# Fix 9: Cross-domain warning
|
| 208 |
-
if is_cross_domain:
|
| 209 |
-
risk_delta = f"Cross-domain comparison ({type_a} vs {type_b}) β risk delta not meaningful across different contract types"
|
| 210 |
-
risk_winner = "cross-domain"
|
| 211 |
-
|
| 212 |
-
comparison_method = "semantic (sentence embeddings)" if _embedder is not None else "lexical (string matching)"
|
| 213 |
-
|
| 214 |
-
return {
|
| 215 |
-
"alignment_score": round(alignment, 3),
|
| 216 |
-
"contract_a_clauses": len(clauses_a),
|
| 217 |
-
"contract_b_clauses": len(clauses_b),
|
| 218 |
-
"contract_a_type": type_a,
|
| 219 |
-
"contract_b_type": type_b,
|
| 220 |
-
"is_cross_domain": is_cross_domain,
|
| 221 |
-
"added_clauses": [{"text": c[:200], "type": _extract_clause_type(c)} for c in added[:50]],
|
| 222 |
-
"removed_clauses": [{"text": c[:200], "type": _extract_clause_type(c)} for c in removed[:50]],
|
| 223 |
-
"modified_clauses": modified[:50],
|
| 224 |
-
"risk_delta": risk_delta,
|
| 225 |
-
"risk_winner": risk_winner,
|
| 226 |
-
"comparison_method": comparison_method,
|
| 227 |
-
"type_map_a": {k: len(v) for k, v in type_map_a.items()},
|
| 228 |
-
"type_map_b": {k: len(v) for k, v in type_map_b.items()},
|
| 229 |
-
}
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
def _split_clauses(text):
|
| 233 |
-
"""Split text into clauses."""
|
| 234 |
-
text = re.sub(r'\n{3,}', '\n\n', text.strip())
|
| 235 |
-
# Try section-based splitting first
|
| 236 |
-
section_splits = re.split(
|
| 237 |
-
r'(?:\n\n)(?=\d+[.)]\s|\([a-z]\)\s|(?:Section|Article|Clause)\s+\d+)',
|
| 238 |
-
text
|
| 239 |
-
)
|
| 240 |
-
if len(section_splits) >= 3:
|
| 241 |
-
return [p.strip() for p in section_splits if len(p.strip()) > 30]
|
| 242 |
-
# Fallback to paragraph/sentence splitting
|
| 243 |
-
parts = re.split(
|
| 244 |
-
r'(?<=[.!?])\s+(?=[A-Z0-9(])|(?:\n\n)',
|
| 245 |
-
text
|
| 246 |
-
)
|
| 247 |
-
return [p.strip() for p in parts if len(p.strip()) > 30]
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
def render_comparison_html(result):
|
| 251 |
-
"""Render comparison results as HTML for Gradio."""
|
| 252 |
-
if "error" in result:
|
| 253 |
-
return f'<p style="color:#dc2626;">{result["error"]}</p>'
|
| 254 |
-
|
| 255 |
-
method = result.get("comparison_method", "unknown")
|
| 256 |
-
method_badge = f'<div style="font-size:10px;color:#6b7280;text-align:center;margin-bottom:12px;">Comparison method: {method}</div>'
|
| 257 |
-
|
| 258 |
-
html = f'''
|
| 259 |
-
<div style="font-family:system-ui,sans-serif;">
|
| 260 |
-
{method_badge}
|
| 261 |
-
<div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;margin-bottom:16px;">
|
| 262 |
-
<div style="padding:12px;border-radius:8px;background:#eff6ff;border:1px solid #bfdbfe;text-align:center;">
|
| 263 |
-
<div style="font-size:24px;font-weight:700;color:#1d4ed8;">{result["contract_a_clauses"]}</div>
|
| 264 |
-
<div style="font-size:12px;color:#3b82f6;">Clauses in Contract A</div>
|
| 265 |
-
</div>
|
| 266 |
-
<div style="padding:12px;border-radius:8px;background:#fefce8;border:1px solid #fde68a;text-align:center;">
|
| 267 |
-
<div style="font-size:24px;font-weight:700;color:#a16207;">{result["contract_b_clauses"]}</div>
|
| 268 |
-
<div style="font-size:12px;color:#ca8a04;">Clauses in Contract B</div>
|
| 269 |
-
</div>
|
| 270 |
-
</div>
|
| 271 |
-
|
| 272 |
-
<div style="padding:12px;border-radius:8px;background:#f9fafb;border:1px solid #e5e7eb;margin-bottom:16px;text-align:center;">
|
| 273 |
-
<div style="font-size:28px;font-weight:700;color:#374151;">{result["alignment_score"]*100:.1f}%</div>
|
| 274 |
-
<div style="font-size:12px;color:#6b7280;">Alignment Score</div>
|
| 275 |
-
</div>
|
| 276 |
-
|
| 277 |
-
<div style="padding:12px;border-radius:8px;background:{
|
| 278 |
-
"#fef2f2" if result["risk_winner"] != "tie" else "#f0fdf4"
|
| 279 |
-
};border:1px solid {
|
| 280 |
-
"#fecaca" if result["risk_winner"] != "tie" else "#bbf7d0"
|
| 281 |
-
};margin-bottom:16px;text-align:center;">
|
| 282 |
-
<span style="font-size:14px;font-weight:600;color:{
|
| 283 |
-
"#dc2626" if result["risk_winner"] != "tie" else "#16a34a"
|
| 284 |
-
};">βοΈ {result["risk_delta"]}</span>
|
| 285 |
-
</div>
|
| 286 |
-
'''
|
| 287 |
-
|
| 288 |
-
# Modified clauses
|
| 289 |
-
if result["modified_clauses"]:
|
| 290 |
-
html += '<div style="margin-bottom:16px;"><h3 style="font-size:14px;color:#374151;margin-bottom:8px;">π Modified Clauses</h3>'
|
| 291 |
-
for m in result["modified_clauses"][:20]:
|
| 292 |
-
html += f'''
|
| 293 |
-
<div style="border:1px solid #e5e7eb;border-radius:6px;padding:10px;margin-bottom:8px;">
|
| 294 |
-
<div style="font-size:11px;color:#6b7280;margin-bottom:4px;">{m["clause_type"].upper()} Β· Similarity: {m["similarity"]*100:.0f}%</div>
|
| 295 |
-
<div style="display:grid;grid-template-columns:1fr 1fr;gap:8px;">
|
| 296 |
-
<div style="background:#fef2f2;padding:6px;border-radius:4px;font-size:12px;color:#991b1b;">{m["clause_a"][:150]}...</div>
|
| 297 |
-
<div style="background:#f0fdf4;padding:6px;border-radius:4px;font-size:12px;color:#166534;">{m["clause_b"][:150]}...</div>
|
| 298 |
-
</div>
|
| 299 |
-
</div>
|
| 300 |
-
'''
|
| 301 |
-
html += '</div>'
|
| 302 |
-
|
| 303 |
-
# Added clauses
|
| 304 |
-
if result["added_clauses"]:
|
| 305 |
-
html += '<div style="margin-bottom:16px;"><h3 style="font-size:14px;color:#374151;margin-bottom:8px;">β Added in Contract B</h3>'
|
| 306 |
-
for a in result["added_clauses"][:15]:
|
| 307 |
-
html += f'<div style="background:#f0fdf4;padding:8px;border-radius:4px;font-size:12px;color:#166534;margin-bottom:4px;border-left:3px solid #22c55e;"><b>{a["type"].upper()}</b> Β· {a["text"][:150]}...</div>'
|
| 308 |
-
html += '</div>'
|
| 309 |
-
|
| 310 |
-
# Removed clauses
|
| 311 |
-
if result["removed_clauses"]:
|
| 312 |
-
html += '<div style="margin-bottom:16px;"><h3 style="font-size:14px;color:#374151;margin-bottom:8px;">β Removed from Contract A</h3>'
|
| 313 |
-
for r in result["removed_clauses"][:15]:
|
| 314 |
-
html += f'<div style="background:#fef2f2;padding:8px;border-radius:4px;font-size:12px;color:#991b1b;margin-bottom:4px;border-left:3px solid #ef4444;"><b>{r["type"].upper()}</b> Β· {r["text"][:150]}...</div>'
|
| 315 |
-
html += '</div>'
|
| 316 |
-
|
| 317 |
-
html += '</div>'
|
| 318 |
-
return html
|
|
|
|
| 1 |
+
file:/app/compare.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1,351 +1 @@
|
|
| 1 |
-
|
| 2 |
-
ClauseGuard β Compliance Checker v3.0
|
| 3 |
-
βββββββββββββββββββββββββββββββββββββ
|
| 4 |
-
FIXED in v3.0:
|
| 5 |
-
β’ Negation handling (clause saying "we do NOT" won't score as PASS)
|
| 6 |
-
β’ Context windows around keyword matches (shows what the clause actually says)
|
| 7 |
-
β’ Semantic scoring (keyword proximity + negation awareness)
|
| 8 |
-
β’ Added more regulatory frameworks
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
import re
|
| 12 |
-
from collections import defaultdict
|
| 13 |
-
|
| 14 |
-
# Negation patterns that invert compliance meaning
|
| 15 |
-
_NEGATION_PATTERNS = [
|
| 16 |
-
r"(?:does?\s+)?not\s+(?:require|provide|include|offer|grant|guarantee|ensure|maintain)",
|
| 17 |
-
r"(?:no|without)\s+(?:obligation|requirement|guarantee|warranty)",
|
| 18 |
-
r"(?:exclud|waiv|disclaim|exempt|refus|deny|reject)",
|
| 19 |
-
r"shall\s+not\s+be\s+(?:required|obligated|responsible)",
|
| 20 |
-
r"is\s+not\s+(?:responsible|liable|required|obligated)",
|
| 21 |
-
]
|
| 22 |
-
|
| 23 |
-
# Regulatory requirement definitions
|
| 24 |
-
REGULATIONS = {
|
| 25 |
-
"GDPR": {
|
| 26 |
-
"description": "EU General Data Protection Regulation (Regulation 2016/679)",
|
| 27 |
-
"requirements": {
|
| 28 |
-
"lawful_basis": {
|
| 29 |
-
"keywords": ["lawful basis", "legal basis", "legitimate interest", "consent", "performance of contract", "legal obligation"],
|
| 30 |
-
"description": "Must specify lawful basis for data processing (Art. 6)",
|
| 31 |
-
"severity": "HIGH",
|
| 32 |
-
},
|
| 33 |
-
"data_subject_rights": {
|
| 34 |
-
"keywords": ["right to access", "right to erasure", "right to be forgotten", "data portability", "rectification", "object to processing"],
|
| 35 |
-
"description": "Must acknowledge data subject rights (Arts. 15-22)",
|
| 36 |
-
"severity": "HIGH",
|
| 37 |
-
},
|
| 38 |
-
"data_breach_notification": {
|
| 39 |
-
"keywords": ["data breach", "breach notification", "notify supervisory authority", "72 hours"],
|
| 40 |
-
"description": "Must include data breach notification obligations (Art. 33)",
|
| 41 |
-
"severity": "MEDIUM",
|
| 42 |
-
},
|
| 43 |
-
"data_protection_officer": {
|
| 44 |
-
"keywords": ["data protection officer", "DPO"],
|
| 45 |
-
"description": "Should reference Data Protection Officer if applicable (Art. 37)",
|
| 46 |
-
"severity": "LOW",
|
| 47 |
-
},
|
| 48 |
-
"cross_border_transfer": {
|
| 49 |
-
"keywords": ["standard contractual clauses", "SCCs", "adequacy decision", "transfer mechanism", "third country"],
|
| 50 |
-
"description": "Must specify transfer safeguards for cross-border data (Arts. 44-49)",
|
| 51 |
-
"severity": "HIGH",
|
| 52 |
-
},
|
| 53 |
-
"privacy_by_design": {
|
| 54 |
-
"keywords": ["privacy by design", "privacy by default", "data minimization", "purpose limitation"],
|
| 55 |
-
"description": "Should reference privacy-by-design principles (Art. 25)",
|
| 56 |
-
"severity": "MEDIUM",
|
| 57 |
-
},
|
| 58 |
-
"data_processing_agreement": {
|
| 59 |
-
"keywords": ["data processing agreement", "DPA", "data processor", "sub-processor"],
|
| 60 |
-
"description": "Must include data processing agreement if sharing data (Art. 28)",
|
| 61 |
-
"severity": "HIGH",
|
| 62 |
-
},
|
| 63 |
-
},
|
| 64 |
-
},
|
| 65 |
-
"CCPA": {
|
| 66 |
-
"description": "California Consumer Privacy Act (Cal. Civ. Code Β§ 1798.100 et seq.)",
|
| 67 |
-
"requirements": {
|
| 68 |
-
"consumer_rights": {
|
| 69 |
-
"keywords": ["right to know", "right to delete", "right to opt out", "right to non-discrimination", "consumer rights"],
|
| 70 |
-
"description": "Must acknowledge California consumer rights",
|
| 71 |
-
"severity": "HIGH",
|
| 72 |
-
},
|
| 73 |
-
"data_categories": {
|
| 74 |
-
"keywords": ["categories of personal information", "personal information categories", "identifiers", "commercial information"],
|
| 75 |
-
"description": "Must disclose categories of personal information collected",
|
| 76 |
-
"severity": "HIGH",
|
| 77 |
-
},
|
| 78 |
-
"sale_of_data": {
|
| 79 |
-
"keywords": ["do not sell my personal information", "opt-out of sale", "sale of personal information"],
|
| 80 |
-
"description": "Must provide opt-out mechanism for data sales",
|
| 81 |
-
"severity": "HIGH",
|
| 82 |
-
},
|
| 83 |
-
"service_providers": {
|
| 84 |
-
"keywords": ["service provider", "third party", "contractor", "business purpose"],
|
| 85 |
-
"description": "Should limit data use to business/service provider purposes",
|
| 86 |
-
"severity": "MEDIUM",
|
| 87 |
-
},
|
| 88 |
-
},
|
| 89 |
-
},
|
| 90 |
-
"SOX": {
|
| 91 |
-
"description": "Sarbanes-Oxley Act (US, 2002)",
|
| 92 |
-
"requirements": {
|
| 93 |
-
"internal_controls": {
|
| 94 |
-
"keywords": ["internal controls", "internal control over financial reporting", "ICFR"],
|
| 95 |
-
"description": "Must reference internal controls over financial reporting (Β§ 404)",
|
| 96 |
-
"severity": "HIGH",
|
| 97 |
-
},
|
| 98 |
-
"audit_committee": {
|
| 99 |
-
"keywords": ["audit committee", "independent auditor", "PCAOB"],
|
| 100 |
-
"description": "Should reference audit committee oversight",
|
| 101 |
-
"severity": "MEDIUM",
|
| 102 |
-
},
|
| 103 |
-
"whistleblower": {
|
| 104 |
-
"keywords": ["whistleblower", "anonymous reporting", "reporting hotline", "retaliation"],
|
| 105 |
-
"description": "Should protect whistleblower provisions (Β§ 806)",
|
| 106 |
-
"severity": "HIGH",
|
| 107 |
-
},
|
| 108 |
-
"document_retention": {
|
| 109 |
-
"keywords": ["document retention", "record retention", "retention policy", "preserve records"],
|
| 110 |
-
"description": "Must include document retention obligations (Β§ 802)",
|
| 111 |
-
"severity": "HIGH",
|
| 112 |
-
},
|
| 113 |
-
},
|
| 114 |
-
},
|
| 115 |
-
"HIPAA": {
|
| 116 |
-
"description": "Health Insurance Portability and Accountability Act (US, 1996)",
|
| 117 |
-
"requirements": {
|
| 118 |
-
"phi_protection": {
|
| 119 |
-
"keywords": ["protected health information", "PHI", "health information", "ePHI"],
|
| 120 |
-
"description": "Must protect PHI and limit uses/disclosures",
|
| 121 |
-
"severity": "CRITICAL",
|
| 122 |
-
},
|
| 123 |
-
"business_associate": {
|
| 124 |
-
"keywords": ["business associate agreement", "BAA", "business associate", "covered entity"],
|
| 125 |
-
"description": "Should reference Business Associate Agreement (Β§ 164.504(e))",
|
| 126 |
-
"severity": "HIGH",
|
| 127 |
-
},
|
| 128 |
-
"security_safeguards": {
|
| 129 |
-
"keywords": ["administrative safeguards", "technical safeguards", "physical safeguards", "encryption", "access controls"],
|
| 130 |
-
"description": "Must implement security safeguards (Β§ 164.308-312)",
|
| 131 |
-
"severity": "HIGH",
|
| 132 |
-
},
|
| 133 |
-
"breach_notification": {
|
| 134 |
-
"keywords": ["breach notification", "notification of breach", "unauthorized access"],
|
| 135 |
-
"description": "Must include breach notification obligations (Β§ 164.400-414)",
|
| 136 |
-
"severity": "HIGH",
|
| 137 |
-
},
|
| 138 |
-
},
|
| 139 |
-
},
|
| 140 |
-
"FINRA": {
|
| 141 |
-
"description": "Financial Industry Regulatory Authority (US)",
|
| 142 |
-
"requirements": {
|
| 143 |
-
"recordkeeping": {
|
| 144 |
-
"keywords": ["recordkeeping", "books and records", "retain records", "SEC Rule 17a-4"],
|
| 145 |
-
"description": "Must comply with recordkeeping rules (FINRA Rule 4511)",
|
| 146 |
-
"severity": "HIGH",
|
| 147 |
-
},
|
| 148 |
-
"supervision": {
|
| 149 |
-
"keywords": ["supervision", "supervisory system", "review and approval"],
|
| 150 |
-
"description": "Should reference supervisory obligations (FINRA Rule 3110)",
|
| 151 |
-
"severity": "MEDIUM",
|
| 152 |
-
},
|
| 153 |
-
"anti_money_laundering": {
|
| 154 |
-
"keywords": ["anti-money laundering", "AML", "suspicious activity", "SAR", "OFAC"],
|
| 155 |
-
"description": "Must reference AML compliance (FINRA Rule 3310)",
|
| 156 |
-
"severity": "HIGH",
|
| 157 |
-
},
|
| 158 |
-
"privacy": {
|
| 159 |
-
"keywords": ["privacy policy", "customer information", "Regulation S-P", "nonpublic personal information"],
|
| 160 |
-
"description": "Must protect customer information (Regulation S-P)",
|
| 161 |
-
"severity": "HIGH",
|
| 162 |
-
},
|
| 163 |
-
},
|
| 164 |
-
},
|
| 165 |
-
}
|
| 166 |
-
|
| 167 |
-
RISK_STYLES = {
|
| 168 |
-
"CRITICAL": ("#dc2626", "#fef2f2"),
|
| 169 |
-
"HIGH": ("#ea580c", "#fff7ed"),
|
| 170 |
-
"MEDIUM": ("#ca8a04", "#fefce8"),
|
| 171 |
-
"LOW": ("#16a34a", "#f0fdf4"),
|
| 172 |
-
}
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
def _check_negation(text_lower, keyword, window=100):
|
| 176 |
-
"""Check if a keyword match is negated by nearby negation words."""
|
| 177 |
-
idx = text_lower.find(keyword.lower())
|
| 178 |
-
if idx == -1:
|
| 179 |
-
return False
|
| 180 |
-
# Get context window around the match
|
| 181 |
-
start = max(0, idx - window)
|
| 182 |
-
end = min(len(text_lower), idx + len(keyword) + window)
|
| 183 |
-
context = text_lower[start:end]
|
| 184 |
-
|
| 185 |
-
for neg_pat in _NEGATION_PATTERNS:
|
| 186 |
-
if re.search(neg_pat, context, re.IGNORECASE):
|
| 187 |
-
return True
|
| 188 |
-
return False
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
def _get_context(text, keyword, window=80):
|
| 192 |
-
"""Extract context around a keyword match."""
|
| 193 |
-
text_lower = text.lower()
|
| 194 |
-
idx = text_lower.find(keyword.lower())
|
| 195 |
-
if idx == -1:
|
| 196 |
-
return ""
|
| 197 |
-
start = max(0, idx - window)
|
| 198 |
-
end = min(len(text), idx + len(keyword) + window)
|
| 199 |
-
context = text[start:end].strip()
|
| 200 |
-
if start > 0:
|
| 201 |
-
context = "..." + context
|
| 202 |
-
if end < len(text):
|
| 203 |
-
context = context + "..."
|
| 204 |
-
return context
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
def check_compliance(text):
|
| 208 |
-
"""Check contract text against all regulatory frameworks with negation handling."""
|
| 209 |
-
text_lower = text.lower()
|
| 210 |
-
results = {}
|
| 211 |
-
|
| 212 |
-
for reg_name, reg_data in REGULATIONS.items():
|
| 213 |
-
checks = []
|
| 214 |
-
for req_name, req_data in reg_data["requirements"].items():
|
| 215 |
-
matched = False
|
| 216 |
-
negated = False
|
| 217 |
-
matched_keywords = []
|
| 218 |
-
context_snippets = []
|
| 219 |
-
|
| 220 |
-
for kw in req_data["keywords"]:
|
| 221 |
-
if kw.lower() in text_lower:
|
| 222 |
-
matched_keywords.append(kw)
|
| 223 |
-
# Check if the match is negated
|
| 224 |
-
if _check_negation(text_lower, kw):
|
| 225 |
-
negated = True
|
| 226 |
-
else:
|
| 227 |
-
matched = True
|
| 228 |
-
# Get context
|
| 229 |
-
ctx = _get_context(text, kw)
|
| 230 |
-
if ctx:
|
| 231 |
-
context_snippets.append(ctx)
|
| 232 |
-
|
| 233 |
-
if matched and not negated:
|
| 234 |
-
status = "PASS"
|
| 235 |
-
elif negated and not matched:
|
| 236 |
-
status = "NEGATED"
|
| 237 |
-
elif matched and negated:
|
| 238 |
-
status = "AMBIGUOUS"
|
| 239 |
-
else:
|
| 240 |
-
status = "MISSING"
|
| 241 |
-
|
| 242 |
-
checks.append({
|
| 243 |
-
"requirement": req_name,
|
| 244 |
-
"description": req_data["description"],
|
| 245 |
-
"severity": req_data["severity"],
|
| 246 |
-
"status": status,
|
| 247 |
-
"matched_keywords": matched_keywords,
|
| 248 |
-
"context": context_snippets[:2], # Keep top 2 context snippets
|
| 249 |
-
})
|
| 250 |
-
|
| 251 |
-
passed = sum(1 for c in checks if c["status"] == "PASS")
|
| 252 |
-
total = len(checks)
|
| 253 |
-
compliance_rate = round(passed / total * 100) if total > 0 else 0
|
| 254 |
-
|
| 255 |
-
negated_count = sum(1 for c in checks if c["status"] == "NEGATED")
|
| 256 |
-
ambiguous_count = sum(1 for c in checks if c["status"] == "AMBIGUOUS")
|
| 257 |
-
|
| 258 |
-
if compliance_rate >= 80:
|
| 259 |
-
overall = "COMPLIANT"
|
| 260 |
-
elif compliance_rate >= 40:
|
| 261 |
-
overall = "PARTIAL"
|
| 262 |
-
else:
|
| 263 |
-
overall = "NON-COMPLIANT"
|
| 264 |
-
|
| 265 |
-
# Override if there are negated critical requirements
|
| 266 |
-
if any(c["status"] == "NEGATED" and c["severity"] in ("CRITICAL", "HIGH") for c in checks):
|
| 267 |
-
overall = "WARNING"
|
| 268 |
-
|
| 269 |
-
results[reg_name] = {
|
| 270 |
-
"description": reg_data["description"],
|
| 271 |
-
"compliance_rate": compliance_rate,
|
| 272 |
-
"checks": checks,
|
| 273 |
-
"overall_status": overall,
|
| 274 |
-
"negated_count": negated_count,
|
| 275 |
-
"ambiguous_count": ambiguous_count,
|
| 276 |
-
}
|
| 277 |
-
|
| 278 |
-
return results
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
def render_compliance_html(results):
|
| 282 |
-
"""Render compliance results as HTML for Gradio."""
|
| 283 |
-
html = '<div style="font-family:system-ui,sans-serif;">'
|
| 284 |
-
|
| 285 |
-
for reg_name, reg_result in results.items():
|
| 286 |
-
rate = reg_result["compliance_rate"]
|
| 287 |
-
status = reg_result["overall_status"]
|
| 288 |
-
|
| 289 |
-
status_colors = {
|
| 290 |
-
"COMPLIANT": ("#16a34a", "#f0fdf4"),
|
| 291 |
-
"PARTIAL": ("#ca8a04", "#fefce8"),
|
| 292 |
-
"NON-COMPLIANT": ("#dc2626", "#fef2f2"),
|
| 293 |
-
"WARNING": ("#ea580c", "#fff7ed"),
|
| 294 |
-
}
|
| 295 |
-
status_color, status_bg = status_colors.get(status, ("#6b7280", "#f9fafb"))
|
| 296 |
-
|
| 297 |
-
neg = reg_result.get("negated_count", 0)
|
| 298 |
-
amb = reg_result.get("ambiguous_count", 0)
|
| 299 |
-
warnings = ""
|
| 300 |
-
if neg > 0:
|
| 301 |
-
warnings += f'<span style="font-size:10px;color:#ea580c;margin-left:8px;">β οΈ {neg} negated</span>'
|
| 302 |
-
if amb > 0:
|
| 303 |
-
warnings += f'<span style="font-size:10px;color:#ca8a04;margin-left:8px;">β {amb} ambiguous</span>'
|
| 304 |
-
|
| 305 |
-
html += f'''
|
| 306 |
-
<div style="border:1px solid #e5e7eb;border-radius:10px;margin-bottom:16px;overflow:hidden;">
|
| 307 |
-
<div style="display:flex;justify-content:space-between;align-items:center;padding:12px 16px;background:{status_bg};border-bottom:1px solid #e5e7eb;">
|
| 308 |
-
<div>
|
| 309 |
-
<span style="font-size:16px;font-weight:700;color:#1f2937;">{reg_name}</span>
|
| 310 |
-
{warnings}
|
| 311 |
-
<p style="font-size:11px;color:#6b7280;margin:2px 0 0 0;">{reg_result["description"]}</p>
|
| 312 |
-
</div>
|
| 313 |
-
<div style="text-align:right;">
|
| 314 |
-
<div style="font-size:24px;font-weight:700;color:{status_color};">{rate}%</div>
|
| 315 |
-
<div style="font-size:11px;color:{status_color};font-weight:500;">{status}</div>
|
| 316 |
-
</div>
|
| 317 |
-
</div>
|
| 318 |
-
<div style="padding:8px 16px;">
|
| 319 |
-
'''
|
| 320 |
-
|
| 321 |
-
for check in reg_result["checks"]:
|
| 322 |
-
color, bg = RISK_STYLES[check["severity"]]
|
| 323 |
-
status_icons = {"PASS": "β
", "MISSING": "β", "NEGATED": "π«", "AMBIGUOUS": "β"}
|
| 324 |
-
status_icon = status_icons.get(check["status"], "β")
|
| 325 |
-
status_text_map = {"PASS": "Found", "MISSING": "Missing", "NEGATED": "Negated", "AMBIGUOUS": "Ambiguous"}
|
| 326 |
-
status_text = status_text_map.get(check["status"], "Unknown")
|
| 327 |
-
keywords = ", ".join(check["matched_keywords"][:3]) if check["matched_keywords"] else "β"
|
| 328 |
-
|
| 329 |
-
context_html = ""
|
| 330 |
-
if check.get("context"):
|
| 331 |
-
ctx = check["context"][0][:120].replace("<", "<").replace(">", ">")
|
| 332 |
-
context_html = f'<div style="font-size:10px;color:#6b7280;margin-top:2px;font-style:italic;">"{ctx}"</div>'
|
| 333 |
-
|
| 334 |
-
html += f'''
|
| 335 |
-
<div style="display:flex;justify-content:space-between;align-items:flex-start;padding:8px 0;border-bottom:1px solid #f3f4f6;">
|
| 336 |
-
<div style="flex:1;">
|
| 337 |
-
<div style="font-size:12px;font-weight:500;color:#374151;">{check["description"]}</div>
|
| 338 |
-
<div style="font-size:10px;color:#9ca3af;margin-top:2px;">Keywords: {keywords}</div>
|
| 339 |
-
{context_html}
|
| 340 |
-
</div>
|
| 341 |
-
<div style="display:flex;align-items:center;gap:6px;margin-left:8px;">
|
| 342 |
-
<span style="font-size:10px;color:{color};font-weight:600;background:{bg};padding:2px 8px;border-radius:4px;">{check["severity"]}</span>
|
| 343 |
-
<span style="font-size:13px;" title="{status_text}">{status_icon}</span>
|
| 344 |
-
</div>
|
| 345 |
-
</div>
|
| 346 |
-
'''
|
| 347 |
-
|
| 348 |
-
html += '</div></div>'
|
| 349 |
-
|
| 350 |
-
html += '</div>'
|
| 351 |
-
return html
|
|
|
|
| 1 |
+
file:/app/compliance.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1,203 +1 @@
|
|
| 1 |
-
|
| 2 |
-
import { createClient } from "@/lib/supabase/server";
|
| 3 |
-
|
| 4 |
-
const GRADIO_URL = process.env.CLAUSEGUARD_GRADIO_URL || "https://gaurv007-clauseguard.hf.space";
|
| 5 |
-
|
| 6 |
-
export async function POST(req: NextRequest) {
|
| 7 |
-
try {
|
| 8 |
-
const supabase = await createClient();
|
| 9 |
-
const { data: { user } } = await supabase.auth.getUser();
|
| 10 |
-
|
| 11 |
-
if (!user) {
|
| 12 |
-
return NextResponse.json({ error: "Unauthorized. Please log in to analyze texts." }, { status: 401 });
|
| 13 |
-
}
|
| 14 |
-
|
| 15 |
-
const body = await req.json();
|
| 16 |
-
let { text } = body;
|
| 17 |
-
|
| 18 |
-
if (!text || typeof text !== "string" || text.trim().length < 50) {
|
| 19 |
-
return NextResponse.json(
|
| 20 |
-
{ error: "Please provide at least 50 characters of text to analyze." },
|
| 21 |
-
{ status: 400 }
|
| 22 |
-
);
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
// Check scan limits
|
| 26 |
-
const { data: profile } = await supabase
|
| 27 |
-
.from("profiles")
|
| 28 |
-
.select("plan, role")
|
| 29 |
-
.eq("id", user.id)
|
| 30 |
-
.single();
|
| 31 |
-
|
| 32 |
-
const isAdmin = profile?.role === "admin";
|
| 33 |
-
const plan = profile?.plan || "free";
|
| 34 |
-
|
| 35 |
-
const { count: scanCount } = await supabase
|
| 36 |
-
.from("analysis_history")
|
| 37 |
-
.select("*", { count: "exact", head: true })
|
| 38 |
-
.gte("created_at", new Date(new Date().getFullYear(), new Date().getMonth(), 1).toISOString())
|
| 39 |
-
.eq("user_id", user.id);
|
| 40 |
-
|
| 41 |
-
const limit = isAdmin ? 999999 : plan === "free" ? 10 : 999999;
|
| 42 |
-
if ((scanCount ?? 0) >= limit) {
|
| 43 |
-
return NextResponse.json({ error: "Monthly scan limit reached. Please upgrade to premium." }, { status: 403 });
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
// Sanitize basic HTML tags if any to prevent XSS down the line
|
| 47 |
-
text = text.replace(/</g, "<").replace(/>/g, ">");
|
| 48 |
-
|
| 49 |
-
// Step 1: Submit to Gradio Space
|
| 50 |
-
const submitRes = await fetch(`${GRADIO_URL}/gradio_api/call/_analysis_and_index`, {
|
| 51 |
-
method: "POST",
|
| 52 |
-
headers: { "Content-Type": "application/json" },
|
| 53 |
-
body: JSON.stringify({ data: [text] }),
|
| 54 |
-
});
|
| 55 |
-
|
| 56 |
-
if (!submitRes.ok) {
|
| 57 |
-
throw new Error(`Gradio submit failed: ${submitRes.status}`);
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
-
const { event_id } = await submitRes.json();
|
| 61 |
-
if (!event_id) throw new Error("No event_id from Gradio");
|
| 62 |
-
|
| 63 |
-
// Step 2: Poll for result (SSE)
|
| 64 |
-
// The Gradio API streams but we need the full response
|
| 65 |
-
let resultText = "";
|
| 66 |
-
let attempts = 0;
|
| 67 |
-
const maxAttempts = 60; // 60 seconds max
|
| 68 |
-
|
| 69 |
-
while (attempts < maxAttempts) {
|
| 70 |
-
const resultRes = await fetch(
|
| 71 |
-
`${GRADIO_URL}/gradio_api/call/_analysis_and_index/${event_id}`,
|
| 72 |
-
{ headers: { Accept: "text/event-stream" } }
|
| 73 |
-
);
|
| 74 |
-
|
| 75 |
-
resultText = await resultRes.text();
|
| 76 |
-
|
| 77 |
-
if (resultText.includes("event: complete")) break;
|
| 78 |
-
if (resultText.includes("event: error")) {
|
| 79 |
-
const errMatch = resultText.match(/data:\s*(.+)/);
|
| 80 |
-
throw new Error(errMatch ? errMatch[1] : "Analysis failed in backend");
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
// Wait 1 second and retry
|
| 84 |
-
await new Promise(r => setTimeout(r, 1000));
|
| 85 |
-
attempts++;
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
if (!resultText.includes("event: complete")) {
|
| 89 |
-
throw new Error("Analysis timed out");
|
| 90 |
-
}
|
| 91 |
-
|
| 92 |
-
// Step 3: Parse the SSE data
|
| 93 |
-
// Format: "event: complete\ndata: [...]"
|
| 94 |
-
// The data contains HTML with literal newlines, so we need to find 'data: ' after 'event: complete'
|
| 95 |
-
const completeIdx = resultText.indexOf("event: complete");
|
| 96 |
-
const dataIdx = resultText.indexOf("data: ", completeIdx);
|
| 97 |
-
if (dataIdx === -1) throw new Error("No data in response");
|
| 98 |
-
|
| 99 |
-
const dataStr = resultText.substring(dataIdx + 6).trim();
|
| 100 |
-
|
| 101 |
-
// Parse JSON β the HTML strings contain control characters so we need to handle that
|
| 102 |
-
// In JS, JSON.parse is more lenient with control chars in strings than Python's strict mode
|
| 103 |
-
let gradioData: any[];
|
| 104 |
-
try {
|
| 105 |
-
gradioData = JSON.parse(dataStr);
|
| 106 |
-
} catch {
|
| 107 |
-
// If direct parse fails, try replacing problematic control characters
|
| 108 |
-
const cleaned = dataStr.replace(/[\x00-\x1f]/g, (ch: string) => {
|
| 109 |
-
if (ch === "\n") return "\\n";
|
| 110 |
-
if (ch === "\r") return "\\r";
|
| 111 |
-
if (ch === "\t") return "\\t";
|
| 112 |
-
return "";
|
| 113 |
-
});
|
| 114 |
-
gradioData = JSON.parse(cleaned);
|
| 115 |
-
}
|
| 116 |
-
|
| 117 |
-
// Step 4: Download the JSON report file (structured data)
|
| 118 |
-
// gradioData[8] is the JSON file object with { url, path, ... }
|
| 119 |
-
const jsonFileObj = gradioData[8];
|
| 120 |
-
if (!jsonFileObj?.url) {
|
| 121 |
-
throw new Error("No JSON report generated");
|
| 122 |
-
}
|
| 123 |
-
|
| 124 |
-
// Download immediately (temp files expire quickly)
|
| 125 |
-
const jsonRes = await fetch(jsonFileObj.url);
|
| 126 |
-
if (!jsonRes.ok) throw new Error("Failed to download analysis JSON");
|
| 127 |
-
const analysisData = await jsonRes.json();
|
| 128 |
-
|
| 129 |
-
// Step 5: Transform to frontend format
|
| 130 |
-
const riskScore = analysisData.risk?.score ?? 0;
|
| 131 |
-
const grade = analysisData.risk?.grade ?? "A";
|
| 132 |
-
const totalClauses = analysisData.metadata?.total_clauses ?? 0;
|
| 133 |
-
const flaggedCount = analysisData.metadata?.flagged_clauses ?? 0;
|
| 134 |
-
|
| 135 |
-
// Group clauses by text (multiple labels per clause)
|
| 136 |
-
const clauseMap = new Map<string, any>();
|
| 137 |
-
for (const cr of (analysisData.clauses || [])) {
|
| 138 |
-
if (!clauseMap.has(cr.text)) {
|
| 139 |
-
clauseMap.set(cr.text, { text: cr.text, categories: [] });
|
| 140 |
-
}
|
| 141 |
-
clauseMap.get(cr.text)!.categories.push({
|
| 142 |
-
name: cr.label,
|
| 143 |
-
severity: cr.risk,
|
| 144 |
-
confidence: cr.confidence,
|
| 145 |
-
description: cr.description,
|
| 146 |
-
});
|
| 147 |
-
}
|
| 148 |
-
const results = Array.from(clauseMap.values());
|
| 149 |
-
|
| 150 |
-
// Parse redlines from HTML (gradioData[7])
|
| 151 |
-
const redlines: any[] = [];
|
| 152 |
-
const redlineHtml = typeof gradioData[7] === "string" ? gradioData[7] : "";
|
| 153 |
-
if (redlineHtml.includes("Clause Redlining")) {
|
| 154 |
-
// Split by redline card borders
|
| 155 |
-
const blocks = redlineHtml.split(/border-left:4px solid #/);
|
| 156 |
-
for (let i = 1; i < blocks.length; i++) {
|
| 157 |
-
const block = blocks[i];
|
| 158 |
-
const labelMatch = block.match(/font-weight:600[^>]*>([^<]+)<\/span>\s*<span[^>]*font-weight:600[^>]*>([^<]+)/);
|
| 159 |
-
const origMatch = block.match(/<del>([^<]*)<\/del>/);
|
| 160 |
-
const safeBlock = block.match(/Suggested Alternative[\s\S]*?<div[^>]*color:#166534[^>]*>([\s\S]*?)<\/div>/);
|
| 161 |
-
const legalMatch = block.match(/Legal Basis<\/div>\s*<div[^>]*>([^<]+)/);
|
| 162 |
-
const consumerMatch = block.match(/Consumer Standard<\/div>\s*<div[^>]*>([^<]+)/);
|
| 163 |
-
const isLLM = block.includes("LLM Refined");
|
| 164 |
-
|
| 165 |
-
if (labelMatch) {
|
| 166 |
-
redlines.push({
|
| 167 |
-
clause_label: labelMatch[1].trim(),
|
| 168 |
-
risk_level: labelMatch[2].trim(),
|
| 169 |
-
original_text: origMatch ? origMatch[1].trim() : "",
|
| 170 |
-
safe_alternative: safeBlock ? safeBlock[1].replace(/<[^>]+>/g, "").trim() : "",
|
| 171 |
-
legal_basis: legalMatch ? legalMatch[1].trim() : "",
|
| 172 |
-
consumer_standard: consumerMatch ? consumerMatch[1].trim() : "",
|
| 173 |
-
tier: isLLM ? "llm_refined" : "template",
|
| 174 |
-
});
|
| 175 |
-
}
|
| 176 |
-
}
|
| 177 |
-
}
|
| 178 |
-
|
| 179 |
-
const modelStatus = analysisData.metadata?.model || "";
|
| 180 |
-
|
| 181 |
-
return NextResponse.json({
|
| 182 |
-
risk_score: riskScore,
|
| 183 |
-
grade,
|
| 184 |
-
total_clauses: totalClauses,
|
| 185 |
-
flagged_count: flaggedCount,
|
| 186 |
-
results,
|
| 187 |
-
entities: analysisData.entities || [],
|
| 188 |
-
contradictions: analysisData.contradictions || [],
|
| 189 |
-
obligations: analysisData.obligations || [],
|
| 190 |
-
compliance: analysisData.compliance || {},
|
| 191 |
-
redlines,
|
| 192 |
-
model: modelStatus.includes("loaded") ? "ml" : "regex",
|
| 193 |
-
latency_ms: 0,
|
| 194 |
-
session_id: null,
|
| 195 |
-
});
|
| 196 |
-
} catch (error: any) {
|
| 197 |
-
console.error("Analyze error:", error.message);
|
| 198 |
-
return NextResponse.json(
|
| 199 |
-
{ error: "Analysis failed: " + (error.message || "Try again in 30 seconds.") },
|
| 200 |
-
{ status: 500 }
|
| 201 |
-
);
|
| 202 |
-
}
|
| 203 |
-
}
|
|
|
|
| 1 |
+
file:/app/web_api_analyze_route.ts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1,77 +1 @@
|
|
| 1 |
-
|
| 2 |
-
import { createClient } from "@/lib/supabase/server";
|
| 3 |
-
|
| 4 |
-
const GRADIO_URL = process.env.CLAUSEGUARD_GRADIO_URL || "https://gaurv007-clauseguard.hf.space";
|
| 5 |
-
|
| 6 |
-
export async function POST(req: NextRequest) {
|
| 7 |
-
try {
|
| 8 |
-
const supabase = await createClient();
|
| 9 |
-
const { data: { user } } = await supabase.auth.getUser();
|
| 10 |
-
|
| 11 |
-
if (!user) {
|
| 12 |
-
return NextResponse.json({ error: "Unauthorized. Please log in." }, { status: 401 });
|
| 13 |
-
}
|
| 14 |
-
|
| 15 |
-
const body = await req.json();
|
| 16 |
-
const { message, history } = body;
|
| 17 |
-
|
| 18 |
-
if (!message) {
|
| 19 |
-
return NextResponse.json(
|
| 20 |
-
{ error: "message is required" },
|
| 21 |
-
{ status: 400 }
|
| 22 |
-
);
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
// The Gradio ChatInterface endpoint is /chat
|
| 26 |
-
// It accepts: message (str), then the additional_inputs are handled by Gradio state
|
| 27 |
-
// We need to call the Gradio API with the message
|
| 28 |
-
const submitRes = await fetch(`${GRADIO_URL}/gradio_api/call/chat`, {
|
| 29 |
-
method: "POST",
|
| 30 |
-
headers: { "Content-Type": "application/json" },
|
| 31 |
-
body: JSON.stringify({ data: [message] }),
|
| 32 |
-
});
|
| 33 |
-
|
| 34 |
-
if (!submitRes.ok) {
|
| 35 |
-
const errText = await submitRes.text().catch(() => "");
|
| 36 |
-
throw new Error(`Chat submit failed (${submitRes.status}): ${errText}`);
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
const { event_id } = await submitRes.json();
|
| 40 |
-
if (!event_id) throw new Error("No event_id from Gradio chat");
|
| 41 |
-
|
| 42 |
-
// Poll for streaming result
|
| 43 |
-
const resultRes = await fetch(
|
| 44 |
-
`${GRADIO_URL}/gradio_api/call/chat/${event_id}`,
|
| 45 |
-
{ headers: { Accept: "text/event-stream" } }
|
| 46 |
-
);
|
| 47 |
-
|
| 48 |
-
if (!resultRes.ok) {
|
| 49 |
-
throw new Error(`Chat result failed: ${resultRes.status}`);
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
const resultText = await resultRes.text();
|
| 53 |
-
|
| 54 |
-
// Find the complete event data
|
| 55 |
-
const dataMatch = resultText.match(/event:\s*complete\s*\ndata:\s*(.+)/);
|
| 56 |
-
if (!dataMatch) {
|
| 57 |
-
// Check for error
|
| 58 |
-
const errMatch = resultText.match(/event:\s*error\s*\ndata:\s*(.+)/);
|
| 59 |
-
if (errMatch) {
|
| 60 |
-
throw new Error(`Chat error: ${errMatch[1]}`);
|
| 61 |
-
}
|
| 62 |
-
throw new Error("No response from chatbot. Analyze a contract first in the Gradio Space, then try chatting.");
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
const responseData = JSON.parse(dataMatch[1]);
|
| 66 |
-
// The ChatInterface returns the response as a string
|
| 67 |
-
const responseText = typeof responseData === "string" ? responseData : responseData[0] || "";
|
| 68 |
-
|
| 69 |
-
return NextResponse.json({ response: responseText });
|
| 70 |
-
} catch (error: any) {
|
| 71 |
-
console.error("Chat error:", error.message);
|
| 72 |
-
return NextResponse.json(
|
| 73 |
-
{ error: error.message || "Chat failed. Make sure you analyzed a contract in the Gradio Space first." },
|
| 74 |
-
{ status: 500 }
|
| 75 |
-
);
|
| 76 |
-
}
|
| 77 |
-
}
|
|
|
|
| 1 |
+
file:/app/web_api_chat_route.ts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
file:/app/web_loading.tsx
|
|
@@ -1,203 +1 @@
|
|
| 1 |
-
|
| 2 |
-
-- Tables ordered by dependency (no forward references)
|
| 3 |
-
|
| 4 |
-
-- βββ 1. Teams (no dependencies) βββ
|
| 5 |
-
CREATE TABLE IF NOT EXISTS public.teams (
|
| 6 |
-
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 7 |
-
name TEXT NOT NULL,
|
| 8 |
-
owner_id UUID REFERENCES auth.users NOT NULL,
|
| 9 |
-
plan TEXT DEFAULT 'team',
|
| 10 |
-
max_seats INT DEFAULT 5,
|
| 11 |
-
razorpay_subscription_id TEXT,
|
| 12 |
-
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 13 |
-
);
|
| 14 |
-
|
| 15 |
-
-- βββ 2. Profiles (depends on teams) βββ
|
| 16 |
-
CREATE TABLE IF NOT EXISTS public.profiles (
|
| 17 |
-
id UUID REFERENCES auth.users ON DELETE CASCADE PRIMARY KEY,
|
| 18 |
-
email TEXT,
|
| 19 |
-
full_name TEXT,
|
| 20 |
-
avatar_url TEXT,
|
| 21 |
-
razorpay_subscription_id TEXT,
|
| 22 |
-
plan TEXT DEFAULT 'free' CHECK (plan IN ('free', 'pro', 'team')),
|
| 23 |
-
role TEXT DEFAULT 'user' CHECK (role IN ('user', 'admin')),
|
| 24 |
-
is_banned BOOLEAN DEFAULT false,
|
| 25 |
-
team_id UUID REFERENCES public.teams(id) ON DELETE SET NULL,
|
| 26 |
-
analyses_this_month INT DEFAULT 0,
|
| 27 |
-
monthly_reset_at TIMESTAMPTZ DEFAULT date_trunc('month', NOW()),
|
| 28 |
-
created_at TIMESTAMPTZ DEFAULT NOW(),
|
| 29 |
-
updated_at TIMESTAMPTZ DEFAULT NOW()
|
| 30 |
-
);
|
| 31 |
-
|
| 32 |
-
-- βββ 3. Team Invites (depends on teams) βββ
|
| 33 |
-
CREATE TABLE IF NOT EXISTS public.team_invites (
|
| 34 |
-
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 35 |
-
team_id UUID REFERENCES public.teams ON DELETE CASCADE NOT NULL,
|
| 36 |
-
email TEXT NOT NULL,
|
| 37 |
-
role TEXT DEFAULT 'member' CHECK (role IN ('admin', 'member')),
|
| 38 |
-
status TEXT DEFAULT 'pending' CHECK (status IN ('pending', 'accepted', 'expired')),
|
| 39 |
-
invited_by UUID REFERENCES auth.users NOT NULL,
|
| 40 |
-
created_at TIMESTAMPTZ DEFAULT NOW(),
|
| 41 |
-
expires_at TIMESTAMPTZ DEFAULT NOW() + INTERVAL '7 days',
|
| 42 |
-
UNIQUE(team_id, email)
|
| 43 |
-
);
|
| 44 |
-
|
| 45 |
-
-- βββ 4. Analyses (depends on profiles, teams) βββ
|
| 46 |
-
CREATE TABLE IF NOT EXISTS public.analyses (
|
| 47 |
-
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 48 |
-
user_id UUID REFERENCES public.profiles ON DELETE CASCADE NOT NULL,
|
| 49 |
-
team_id UUID REFERENCES public.teams ON DELETE SET NULL,
|
| 50 |
-
source_url TEXT,
|
| 51 |
-
source_type TEXT DEFAULT 'tos' CHECK (source_type IN ('tos', 'contract', 'rental', 'other')),
|
| 52 |
-
total_clauses INT NOT NULL,
|
| 53 |
-
flagged_count INT NOT NULL,
|
| 54 |
-
risk_score INT NOT NULL CHECK (risk_score >= 0 AND risk_score <= 100),
|
| 55 |
-
grade CHAR(1) NOT NULL CHECK (grade IN ('A', 'B', 'C', 'D', 'F')),
|
| 56 |
-
clauses JSONB NOT NULL DEFAULT '[]',
|
| 57 |
-
entities JSONB DEFAULT '[]',
|
| 58 |
-
contradictions JSONB DEFAULT '[]',
|
| 59 |
-
obligations JSONB DEFAULT '[]',
|
| 60 |
-
compliance JSONB DEFAULT '{}',
|
| 61 |
-
raw_text TEXT,
|
| 62 |
-
model TEXT DEFAULT 'regex',
|
| 63 |
-
latency_ms INT DEFAULT 0,
|
| 64 |
-
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 65 |
-
);
|
| 66 |
-
|
| 67 |
-
-- βββ 5. API Keys (depends on profiles, teams) βββ
|
| 68 |
-
CREATE TABLE IF NOT EXISTS public.api_keys (
|
| 69 |
-
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 70 |
-
user_id UUID REFERENCES public.profiles ON DELETE CASCADE NOT NULL,
|
| 71 |
-
team_id UUID REFERENCES public.teams ON DELETE CASCADE,
|
| 72 |
-
name TEXT NOT NULL,
|
| 73 |
-
key_hash TEXT NOT NULL UNIQUE,
|
| 74 |
-
key_prefix TEXT NOT NULL,
|
| 75 |
-
calls_this_month INT DEFAULT 0,
|
| 76 |
-
calls_limit INT DEFAULT 1000,
|
| 77 |
-
last_used_at TIMESTAMPTZ,
|
| 78 |
-
is_active BOOLEAN DEFAULT true,
|
| 79 |
-
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 80 |
-
);
|
| 81 |
-
|
| 82 |
-
-- βββ 6. Custom Clause Rules (depends on profiles, teams) βββ
|
| 83 |
-
CREATE TABLE IF NOT EXISTS public.custom_rules (
|
| 84 |
-
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 85 |
-
user_id UUID REFERENCES public.profiles ON DELETE CASCADE,
|
| 86 |
-
team_id UUID REFERENCES public.teams ON DELETE CASCADE,
|
| 87 |
-
name TEXT NOT NULL,
|
| 88 |
-
description TEXT,
|
| 89 |
-
pattern TEXT NOT NULL,
|
| 90 |
-
severity TEXT DEFAULT 'MEDIUM' CHECK (severity IN ('HIGH', 'MEDIUM', 'LOW')),
|
| 91 |
-
category TEXT NOT NULL,
|
| 92 |
-
is_active BOOLEAN DEFAULT true,
|
| 93 |
-
created_at TIMESTAMPTZ DEFAULT NOW(),
|
| 94 |
-
updated_at TIMESTAMPTZ DEFAULT NOW()
|
| 95 |
-
);
|
| 96 |
-
|
| 97 |
-
-- βββ 7. Admin Logs βββ
|
| 98 |
-
CREATE TABLE IF NOT EXISTS public.admin_logs (
|
| 99 |
-
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 100 |
-
admin_id UUID REFERENCES auth.users NOT NULL,
|
| 101 |
-
action TEXT NOT NULL,
|
| 102 |
-
target_type TEXT,
|
| 103 |
-
target_id TEXT,
|
| 104 |
-
details JSONB,
|
| 105 |
-
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 106 |
-
);
|
| 107 |
-
|
| 108 |
-
-- βββ Indexes βββ
|
| 109 |
-
CREATE INDEX IF NOT EXISTS idx_analyses_user_id ON public.analyses(user_id);
|
| 110 |
-
CREATE INDEX IF NOT EXISTS idx_analyses_team_id ON public.analyses(team_id);
|
| 111 |
-
CREATE INDEX IF NOT EXISTS idx_analyses_created_at ON public.analyses(created_at DESC);
|
| 112 |
-
CREATE INDEX IF NOT EXISTS idx_api_keys_key_hash ON public.api_keys(key_hash);
|
| 113 |
-
CREATE INDEX IF NOT EXISTS idx_api_keys_user_id ON public.api_keys(user_id);
|
| 114 |
-
CREATE INDEX IF NOT EXISTS idx_team_invites_email ON public.team_invites(email);
|
| 115 |
-
CREATE INDEX IF NOT EXISTS idx_custom_rules_user_id ON public.custom_rules(user_id);
|
| 116 |
-
CREATE INDEX IF NOT EXISTS idx_custom_rules_team_id ON public.custom_rules(team_id);
|
| 117 |
-
CREATE INDEX IF NOT EXISTS idx_admin_logs_created ON public.admin_logs(created_at DESC);
|
| 118 |
-
CREATE INDEX IF NOT EXISTS idx_profiles_role ON public.profiles(role);
|
| 119 |
-
CREATE INDEX IF NOT EXISTS idx_profiles_email ON public.profiles(email);
|
| 120 |
-
|
| 121 |
-
-- βββ Row Level Security βββ
|
| 122 |
-
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
|
| 123 |
-
ALTER TABLE public.analyses ENABLE ROW LEVEL SECURITY;
|
| 124 |
-
ALTER TABLE public.teams ENABLE ROW LEVEL SECURITY;
|
| 125 |
-
ALTER TABLE public.team_invites ENABLE ROW LEVEL SECURITY;
|
| 126 |
-
ALTER TABLE public.api_keys ENABLE ROW LEVEL SECURITY;
|
| 127 |
-
ALTER TABLE public.custom_rules ENABLE ROW LEVEL SECURITY;
|
| 128 |
-
ALTER TABLE public.admin_logs ENABLE ROW LEVEL SECURITY;
|
| 129 |
-
|
| 130 |
-
-- Profiles
|
| 131 |
-
CREATE POLICY "Users see own profile" ON public.profiles FOR SELECT USING (auth.uid() = id);
|
| 132 |
-
CREATE POLICY "Users update own profile" ON public.profiles FOR UPDATE USING (auth.uid() = id);
|
| 133 |
-
CREATE POLICY "Admins read all profiles" ON public.profiles FOR SELECT USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 134 |
-
CREATE POLICY "Admins update all profiles" ON public.profiles FOR UPDATE USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 135 |
-
|
| 136 |
-
-- Analyses
|
| 137 |
-
CREATE POLICY "Users see own analyses" ON public.analyses FOR SELECT
|
| 138 |
-
USING (auth.uid() = user_id OR team_id IN (SELECT team_id FROM public.profiles WHERE id = auth.uid()));
|
| 139 |
-
CREATE POLICY "Users insert analyses" ON public.analyses FOR INSERT WITH CHECK (auth.uid() = user_id);
|
| 140 |
-
CREATE POLICY "Users delete own analyses" ON public.analyses FOR DELETE USING (auth.uid() = user_id);
|
| 141 |
-
CREATE POLICY "Admins read all analyses" ON public.analyses FOR SELECT USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 142 |
-
|
| 143 |
-
-- Teams
|
| 144 |
-
CREATE POLICY "Team members can view" ON public.teams FOR SELECT
|
| 145 |
-
USING (id IN (SELECT team_id FROM public.profiles WHERE id = auth.uid()) OR owner_id = auth.uid());
|
| 146 |
-
CREATE POLICY "Owner can update team" ON public.teams FOR UPDATE USING (owner_id = auth.uid());
|
| 147 |
-
CREATE POLICY "Admins read all teams" ON public.teams FOR SELECT USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 148 |
-
|
| 149 |
-
-- Team invites
|
| 150 |
-
CREATE POLICY "Members see team invites" ON public.team_invites FOR SELECT
|
| 151 |
-
USING (team_id IN (SELECT team_id FROM public.profiles WHERE id = auth.uid()));
|
| 152 |
-
CREATE POLICY "Users can invite" ON public.team_invites FOR INSERT WITH CHECK (invited_by = auth.uid());
|
| 153 |
-
|
| 154 |
-
-- API Keys
|
| 155 |
-
CREATE POLICY "Users see own API keys" ON public.api_keys FOR SELECT
|
| 156 |
-
USING (user_id = auth.uid() OR team_id IN (SELECT team_id FROM public.profiles WHERE id = auth.uid()));
|
| 157 |
-
CREATE POLICY "Users manage own API keys" ON public.api_keys FOR ALL USING (user_id = auth.uid());
|
| 158 |
-
CREATE POLICY "Admins read all api_keys" ON public.api_keys FOR SELECT USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 159 |
-
|
| 160 |
-
-- Custom Rules
|
| 161 |
-
CREATE POLICY "Users see own rules" ON public.custom_rules FOR SELECT
|
| 162 |
-
USING (user_id = auth.uid() OR team_id IN (SELECT team_id FROM public.profiles WHERE id = auth.uid()));
|
| 163 |
-
CREATE POLICY "Users manage own rules" ON public.custom_rules FOR ALL USING (user_id = auth.uid());
|
| 164 |
-
CREATE POLICY "Admins read all rules" ON public.custom_rules FOR SELECT USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 165 |
-
|
| 166 |
-
-- Admin Logs
|
| 167 |
-
CREATE POLICY "Admins manage logs" ON public.admin_logs FOR ALL
|
| 168 |
-
USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 169 |
-
|
| 170 |
-
-- βββ Auto-create profile on signup βββ
|
| 171 |
-
CREATE OR REPLACE FUNCTION public.handle_new_user()
|
| 172 |
-
RETURNS TRIGGER AS $$
|
| 173 |
-
BEGIN
|
| 174 |
-
INSERT INTO public.profiles (id, email, full_name, avatar_url)
|
| 175 |
-
VALUES (
|
| 176 |
-
NEW.id, NEW.email,
|
| 177 |
-
COALESCE(NEW.raw_user_meta_data ->> 'full_name', NEW.raw_user_meta_data ->> 'name', ''),
|
| 178 |
-
COALESCE(NEW.raw_user_meta_data ->> 'avatar_url', NEW.raw_user_meta_data ->> 'picture', '')
|
| 179 |
-
);
|
| 180 |
-
RETURN NEW;
|
| 181 |
-
END;
|
| 182 |
-
$$ LANGUAGE plpgsql SECURITY DEFINER;
|
| 183 |
-
|
| 184 |
-
DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
|
| 185 |
-
CREATE TRIGGER on_auth_user_created
|
| 186 |
-
AFTER INSERT ON auth.users
|
| 187 |
-
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
|
| 188 |
-
|
| 189 |
-
-- βββ Set owner as admin with full access βββ
|
| 190 |
-
-- Run this AFTER your first signup with your email:
|
| 191 |
-
UPDATE public.profiles
|
| 192 |
-
SET role = 'admin', plan = 'pro'
|
| 193 |
-
WHERE email = 'ankygaur9972@gmail.com';
|
| 194 |
-
|
| 195 |
-
-- βββ Monthly reset function βββ
|
| 196 |
-
CREATE OR REPLACE FUNCTION public.reset_monthly_usage()
|
| 197 |
-
RETURNS void AS $$
|
| 198 |
-
BEGIN
|
| 199 |
-
UPDATE public.profiles SET analyses_this_month = 0, monthly_reset_at = date_trunc('month', NOW())
|
| 200 |
-
WHERE monthly_reset_at < date_trunc('month', NOW());
|
| 201 |
-
UPDATE public.api_keys SET calls_this_month = 0;
|
| 202 |
-
END;
|
| 203 |
-
$$ LANGUAGE plpgsql SECURITY DEFINER;
|
|
|
|
| 1 |
+
file:/app/web_schema.sql
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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