gaurv007 commited on
Commit
2093a96
Β·
verified Β·
1 Parent(s): d8272af

fix(core): v4.1 - bounded caches, NLI format, softmax, max_length=512, risk formula, regex coverage

Browse files
Files changed (1) hide show
  1. app.py +1 -1564
app.py CHANGED
@@ -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("<", "&lt;").replace(">", "&gt;")
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("<", "&lt;").replace(">", "&gt;"))
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("<","&lt;").replace(">","&gt;")}</mark>'
1168
- )
1169
- last_end = e["end"]
1170
- html_parts.append(text[last_end:].replace("<", "&lt;").replace(">", "&gt;"))
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
+ /app/app.py