Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,303 +1,422 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import re
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
if
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
#
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
def
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
]
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
def
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import math
|
| 4 |
+
import re
|
| 5 |
+
import io
|
| 6 |
+
import asyncio
|
| 7 |
+
from typing import List, Dict, Tuple, Optional, Any
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 11 |
+
from pypdf import PdfReader
|
| 12 |
+
import docx
|
| 13 |
+
import spacy
|
| 14 |
+
import gradio as gr
|
| 15 |
+
|
| 16 |
+
# -----------------------------
|
| 17 |
+
# Model: IBM Granite 3.2 2B Instruct
|
| 18 |
+
# -----------------------------
|
| 19 |
+
MODEL_ID = "ibm-granite/granite-3.2-2b-instruct"
|
| 20 |
+
|
| 21 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
+
DTYPE = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 23 |
+
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=True)
|
| 25 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 26 |
+
MODEL_ID,
|
| 27 |
+
torch_dtype=DTYPE,
|
| 28 |
+
device_map="auto" if DEVICE == "cuda" else None
|
| 29 |
+
)
|
| 30 |
+
if DEVICE != "cuda":
|
| 31 |
+
model.to(DEVICE)
|
| 32 |
+
|
| 33 |
+
# -----------------------------
|
| 34 |
+
# spaCy for NER
|
| 35 |
+
# -----------------------------
|
| 36 |
+
nlp = spacy.load("en_core_web_sm")
|
| 37 |
+
|
| 38 |
+
# -----------------------------
|
| 39 |
+
# Helper: chat templating for Granite or fallback
|
| 40 |
+
# -----------------------------
|
| 41 |
+
def build_chat_prompt(system_prompt: str, user_prompt: str) -> str:
|
| 42 |
+
messages = []
|
| 43 |
+
if system_prompt:
|
| 44 |
+
messages.append({"role": "system", "content": system_prompt})
|
| 45 |
+
messages.append({"role": "user", "content": user_prompt})
|
| 46 |
+
try:
|
| 47 |
+
return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 48 |
+
except Exception:
|
| 49 |
+
# Fallback: simple concatenation
|
| 50 |
+
sys = f"[SYSTEM]\n{system_prompt}\n" if system_prompt else ""
|
| 51 |
+
usr = f"[USER]\n{user_prompt}\n[ASSISTANT]\n"
|
| 52 |
+
return sys + usr
|
| 53 |
+
|
| 54 |
+
# -----------------------------
|
| 55 |
+
# LLM generation
|
| 56 |
+
# -----------------------------
|
| 57 |
+
def llm_generate(
|
| 58 |
+
system_prompt: str,
|
| 59 |
+
user_prompt: str,
|
| 60 |
+
max_new_tokens: int = 512,
|
| 61 |
+
temperature: float = 0.3,
|
| 62 |
+
top_p: float = 0.9
|
| 63 |
+
) -> str:
|
| 64 |
+
prompt = build_chat_prompt(system_prompt, user_prompt)
|
| 65 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
|
| 66 |
+
with torch.inference_mode():
|
| 67 |
+
output_ids = model.generate(
|
| 68 |
+
**inputs,
|
| 69 |
+
max_new_tokens=max_new_tokens,
|
| 70 |
+
temperature=temperature,
|
| 71 |
+
top_p=top_p,
|
| 72 |
+
do_sample=True,
|
| 73 |
+
pad_token_id=tokenizer.eos_token_id
|
| 74 |
+
)
|
| 75 |
+
full_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 76 |
+
# Try to extract only assistant part if chat template used
|
| 77 |
+
if "[ASSISTANT]" in full_text:
|
| 78 |
+
return full_text.split("[ASSISTANT]")[-1].strip()
|
| 79 |
+
# Otherwise, remove the prompt prefix
|
| 80 |
+
if full_text.startswith(prompt):
|
| 81 |
+
return full_text[len(prompt):].strip()
|
| 82 |
+
return full_text.strip()
|
| 83 |
+
|
| 84 |
+
# -----------------------------
|
| 85 |
+
# Document loading (PDF/DOCX/TXT)
|
| 86 |
+
# -----------------------------
|
| 87 |
+
def load_text_from_pdf(file_obj) -> str:
|
| 88 |
+
reader = PdfReader(file_obj)
|
| 89 |
+
pages = []
|
| 90 |
+
for page in reader.pages:
|
| 91 |
+
try:
|
| 92 |
+
pages.append(page.extract_text() or "")
|
| 93 |
+
except Exception:
|
| 94 |
+
pages.append("")
|
| 95 |
+
return "\n".join(pages).strip()
|
| 96 |
+
|
| 97 |
+
def load_text_from_docx(file_obj) -> str:
|
| 98 |
+
# file_obj is a temporary file-like object; need to read into BytesIO for python-docx
|
| 99 |
+
data = file_obj.read()
|
| 100 |
+
file_obj.seek(0)
|
| 101 |
+
f = io.BytesIO(data)
|
| 102 |
+
doc = docx.Document(f)
|
| 103 |
+
paras = [p.text for p in doc.paragraphs]
|
| 104 |
+
return "\n".join(paras).strip()
|
| 105 |
+
|
| 106 |
+
def load_text_from_txt(file_obj) -> str:
|
| 107 |
+
data = file_obj.read()
|
| 108 |
+
if isinstance(data, bytes):
|
| 109 |
+
try:
|
| 110 |
+
data = data.decode("utf-8", errors="ignore")
|
| 111 |
+
except Exception:
|
| 112 |
+
data = data.decode("latin-1", errors="ignore")
|
| 113 |
+
return str(data).strip()
|
| 114 |
+
|
| 115 |
+
def load_document(file: Optional[gr.File]) -> str:
|
| 116 |
+
if not file:
|
| 117 |
+
return ""
|
| 118 |
+
name = (file.name or "").lower()
|
| 119 |
+
if name.endswith(".pdf"):
|
| 120 |
+
return load_text_from_pdf(file)
|
| 121 |
+
elif name.endswith(".docx"):
|
| 122 |
+
return load_text_from_docx(file)
|
| 123 |
+
elif name.endswith(".txt"):
|
| 124 |
+
return load_text_from_txt(file)
|
| 125 |
+
else:
|
| 126 |
+
# Try all in order
|
| 127 |
+
try:
|
| 128 |
+
return load_text_from_pdf(file)
|
| 129 |
+
except Exception:
|
| 130 |
+
pass
|
| 131 |
+
try:
|
| 132 |
+
return load_text_from_docx(file)
|
| 133 |
+
except Exception:
|
| 134 |
+
pass
|
| 135 |
+
try:
|
| 136 |
+
return load_text_from_txt(file)
|
| 137 |
+
except Exception:
|
| 138 |
+
pass
|
| 139 |
+
return ""
|
| 140 |
+
|
| 141 |
+
# -----------------------------
|
| 142 |
+
# Clause extraction heuristics
|
| 143 |
+
# -----------------------------
|
| 144 |
+
CLAUSE_SPLIT_REGEX = re.compile(
|
| 145 |
+
r"(?:(?:^\s*\d+(?:\.\d+)[.)]\s+)|(?:^\s[A-Z]\s*[.)]\s+)|(?:;?\s*\n))",
|
| 146 |
+
re.MULTILINE
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
def split_into_clauses(text: str, min_len: int = 40) -> List[str]:
|
| 150 |
+
if not text:
|
| 151 |
+
return []
|
| 152 |
+
# First try structured numbering/bullets
|
| 153 |
+
parts = re.split(CLAUSE_SPLIT_REGEX, text)
|
| 154 |
+
# Fallback: sentence-like splits if too few
|
| 155 |
+
if len(parts) < 2:
|
| 156 |
+
parts = re.split(r"(?<=[.;])\s+\n?\s*", text)
|
| 157 |
+
clauses = [p.strip() for p in parts if len(p.strip()) >= min_len]
|
| 158 |
+
# Deduplicate near-identical snippets
|
| 159 |
+
seen = set()
|
| 160 |
+
unique = []
|
| 161 |
+
for c in clauses:
|
| 162 |
+
key = re.sub(r"\s+", " ", c.lower())
|
| 163 |
+
if key not in seen:
|
| 164 |
+
seen.add(key)
|
| 165 |
+
unique.append(c)
|
| 166 |
+
return unique
|
| 167 |
+
|
| 168 |
+
# -----------------------------
|
| 169 |
+
# Feature: Clause Simplification / Plain English
|
| 170 |
+
# -----------------------------
|
| 171 |
+
def simplify_clause(clause: str) -> str:
|
| 172 |
+
system = "You are a legal assistant that rewrites clauses into plain, layman-friendly English while preserving legal meaning."
|
| 173 |
+
user = f"Rewrite the following clause in plain English, preserving intent. Highlight any risks with bullet points at the end.\n\nClause:\n{clause}"
|
| 174 |
+
return llm_generate(system, user, max_new_tokens=400)
|
| 175 |
+
|
| 176 |
+
# -----------------------------
|
| 177 |
+
# Feature: Named Entity Recognition (NER)
|
| 178 |
+
# -----------------------------
|
| 179 |
+
def ner_entities(text: str) -> Dict[str, List[str]]:
|
| 180 |
+
if not text:
|
| 181 |
+
return {}
|
| 182 |
+
doc = nlp(text)
|
| 183 |
+
out: Dict[str, List[str]] = {}
|
| 184 |
+
for ent in doc.ents:
|
| 185 |
+
out.setdefault(ent.label_, []).append(ent.text)
|
| 186 |
+
# Deduplicate
|
| 187 |
+
out = {k: sorted(set(v)) for k, v in out.items()}
|
| 188 |
+
return out
|
| 189 |
+
|
| 190 |
+
# -----------------------------
|
| 191 |
+
# Feature: Clause Extraction and Breakdown
|
| 192 |
+
# -----------------------------
|
| 193 |
+
def extract_clauses(text: str) -> List[str]:
|
| 194 |
+
return split_into_clauses(text)
|
| 195 |
+
|
| 196 |
+
# -----------------------------
|
| 197 |
+
# Feature: Document Type Classification (LLM zero-shot)
|
| 198 |
+
# -----------------------------
|
| 199 |
+
DOC_TYPES = [
|
| 200 |
+
"Non-Disclosure Agreement (NDA)",
|
| 201 |
+
"Lease Agreement",
|
| 202 |
+
"Employment Contract",
|
| 203 |
+
"Service Agreement",
|
| 204 |
+
"Sales Agreement",
|
| 205 |
+
"Consulting Agreement",
|
| 206 |
+
"End User License Agreement (EULA)",
|
| 207 |
+
"Terms of Service",
|
| 208 |
+
]
|
| 209 |
+
|
| 210 |
+
def classify_document(text: str) -> str:
|
| 211 |
+
system = "You are a legal document classifier. Choose the single best-matching document type from the provided list."
|
| 212 |
+
labels = "\n".join(f"- {t}" for t in DOC_TYPES)
|
| 213 |
+
user = f"Classify the following document into one of these types:\n{labels}\n\nDocument:\n{text[:5000]}"
|
| 214 |
+
resp = llm_generate(system, user, max_new_tokens=200)
|
| 215 |
+
# Try to pick the closest label
|
| 216 |
+
scores = {t: (1.0 if t.lower() in resp.lower() else 0.0) for t in DOC_TYPES}
|
| 217 |
+
best = max(scores.items(), key=lambda kv: kv[1])[0]
|
| 218 |
+
# Fallback: heuristic keyword match
|
| 219 |
+
if scores[best] == 0.0:
|
| 220 |
+
lower = text.lower()
|
| 221 |
+
if "confidential" in lower or "non-disclosure" in lower or "nda" in lower:
|
| 222 |
+
best = "Non-Disclosure Agreement (NDA)"
|
| 223 |
+
elif "lease" in lower or "tenant" in lower or "landlord" in lower:
|
| 224 |
+
best = "Lease Agreement"
|
| 225 |
+
elif "employment" in lower or "employee" in lower or "employer" in lower:
|
| 226 |
+
best = "Employment Contract"
|
| 227 |
+
elif "services" in lower or "service" in lower or "statement of work" in lower:
|
| 228 |
+
best = "Service Agreement"
|
| 229 |
+
return best
|
| 230 |
+
|
| 231 |
+
# -----------------------------
|
| 232 |
+
# Feature: Negotiation Coach (3 alternatives with acceptance rates)
|
| 233 |
+
# -----------------------------
|
| 234 |
+
def negotiation_coach(clause: str) -> Tuple[str, List[Dict[str, Any]]]:
|
| 235 |
+
system = "You are an AI negotiation coach for contracts."
|
| 236 |
+
user = (
|
| 237 |
+
"Given the clause below, propose 3 alternative versions ranked by expected acceptance rate. "
|
| 238 |
+
"Provide JSON with fields: alternatives: [ {rank, acceptance_rate_percent, title, clause_text, rationale} ]. "
|
| 239 |
+
"Rank 1 is highest acceptance rate. Keep acceptance_rate_percent as integer. "
|
| 240 |
+
f"\n\nClause:\n{clause}"
|
| 241 |
+
)
|
| 242 |
+
resp = llm_generate(system, user, max_new_tokens=700)
|
| 243 |
+
# Try to extract JSON
|
| 244 |
+
data = None
|
| 245 |
+
try:
|
| 246 |
+
json_str = re.search(r"\{[\s\S]*\}", resp).group(0)
|
| 247 |
+
data = json.loads(json_str)
|
| 248 |
+
except Exception:
|
| 249 |
+
# Try to reconstruct minimal structure
|
| 250 |
+
data = {"alternatives": []}
|
| 251 |
+
# heuristic extraction
|
| 252 |
+
alts = re.split(r"\n\s*\d+[.)]\s*", resp)
|
| 253 |
+
for i, chunk in enumerate(alts[1:4], start=1):
|
| 254 |
+
data["alternatives"].append({
|
| 255 |
+
"rank": i,
|
| 256 |
+
"acceptance_rate_percent": max(50, 90 - (i-1)*10),
|
| 257 |
+
"title": f"Alternative {i}",
|
| 258 |
+
"clause_text": chunk.strip()[:800],
|
| 259 |
+
"rationale": "Heuristic parse from model response."
|
| 260 |
+
})
|
| 261 |
+
pretty = json.dumps(data, indent=2)
|
| 262 |
+
return pretty, data.get("alternatives", [])
|
| 263 |
+
|
| 264 |
+
# -----------------------------
|
| 265 |
+
# Feature: Future Risk Predictor (1–5+ years timeline)
|
| 266 |
+
# -----------------------------
|
| 267 |
+
def future_risk_predictor(clause: str) -> Tuple[str, List[Dict[str, Any]]]:
|
| 268 |
+
system = "You analyze contractual clauses and forecast future risks over time."
|
| 269 |
+
user = (
|
| 270 |
+
"Analyze the clause and forecast risks over the next 1 to 5 years. "
|
| 271 |
+
"Return strict JSON: {timeline: [ {year: int, risk_score_0_100: int, key_risks: [str], mitigation: [str]} ]}. "
|
| 272 |
+
"risk_score_0_100 is an integer. Keep the list length between 5 and 6."
|
| 273 |
+
f"\n\nClause:\n{clause}"
|
| 274 |
+
)
|
| 275 |
+
resp = llm_generate(system, user, max_new_tokens=700)
|
| 276 |
+
data = None
|
| 277 |
+
try:
|
| 278 |
+
json_str = re.search(r"\{[\s\S]*\}", resp).group(0)
|
| 279 |
+
data = json.loads(json_str)
|
| 280 |
+
except Exception:
|
| 281 |
+
data = {"timeline": []}
|
| 282 |
+
for y in range(1, 6):
|
| 283 |
+
data["timeline"].append({
|
| 284 |
+
"year": y,
|
| 285 |
+
"risk_score_0_100": min(95, 40 + y*8),
|
| 286 |
+
"key_risks": ["Heuristic timeline due to JSON parse fallback."],
|
| 287 |
+
"mitigation": ["Seek legal review", "Adjust clause terms", "Add notice/cure period"]
|
| 288 |
+
})
|
| 289 |
+
pretty = json.dumps(data, indent=2)
|
| 290 |
+
return pretty, data["timeline"]
|
| 291 |
+
|
| 292 |
+
# -----------------------------
|
| 293 |
+
# Feature: Fairness Balance Meter (power distribution)
|
| 294 |
+
# -----------------------------
|
| 295 |
+
def fairness_balance_meter(clause: str) -> Tuple[str, int, str]:
|
| 296 |
+
system = "You evaluate which party a clause favors on a 0-100 scale (0=Party A heavily favored, 50=balanced, 100=Party B heavily favored)."
|
| 297 |
+
user = (
|
| 298 |
+
"Return strict JSON: {score_0_100: int, rationale: str, notes: [str]}. "
|
| 299 |
+
"Do not include anything else."
|
| 300 |
+
f"\n\nClause:\n{clause}"
|
| 301 |
+
)
|
| 302 |
+
resp = llm_generate(system, user, max_new_tokens=400)
|
| 303 |
+
try:
|
| 304 |
+
data = json.loads(re.search(r"\{[\s\S]*\}", resp).group(0))
|
| 305 |
+
score = int(data.get("score_0_100", 50))
|
| 306 |
+
rationale = data.get("rationale", "")
|
| 307 |
+
except Exception:
|
| 308 |
+
score, rationale = 50, "Fallback balanced score due to JSON parse."
|
| 309 |
+
data = {"score_0_100": score, "rationale": rationale, "notes": []}
|
| 310 |
+
pretty = json.dumps(data, indent=2)
|
| 311 |
+
return pretty, score, rationale
|
| 312 |
+
|
| 313 |
+
# -----------------------------
|
| 314 |
+
# Feature: Clause Battle Arena (head-to-head)
|
| 315 |
+
# -----------------------------
|
| 316 |
+
def clause_battle_arena(text_a: str, text_b: str) -> Tuple[str, str]:
|
| 317 |
+
system = "You compare two contract drafts across objective criteria and declare an overall winner."
|
| 318 |
+
user = (
|
| 319 |
+
"Compare Document A vs Document B across: Liability, Termination, IP, Payment, Confidentiality, Governing Law. "
|
| 320 |
+
"Return JSON: {rounds: [ {category, winner: 'A'|'B'|'Draw', rationale} ], overall_winner: 'A'|'B'|'Draw', summary: str}.\n"
|
| 321 |
+
f"Document A:\n{text_a[:4000]}\n\nDocument B:\n{text_b[:4000]}"
|
| 322 |
+
)
|
| 323 |
+
resp = llm_generate(system, user, max_new_tokens=900)
|
| 324 |
+
try:
|
| 325 |
+
data = json.loads(re.search(r"\{[\s\S]*\}", resp).group(0))
|
| 326 |
+
except Exception:
|
| 327 |
+
data = {
|
| 328 |
+
"rounds": [
|
| 329 |
+
{"category": "Liability", "winner": "Draw", "rationale": "Fallback"},
|
| 330 |
+
{"category": "Termination", "winner": "Draw", "rationale": "Fallback"},
|
| 331 |
+
{"category": "IP", "winner": "Draw", "rationale": "Fallback"},
|
| 332 |
+
{"category": "Payment", "winner": "Draw", "rationale": "Fallback"},
|
| 333 |
+
{"category": "Confidentiality", "winner": "Draw", "rationale": "Fallback"},
|
| 334 |
+
{"category": "Governing Law", "winner": "Draw", "rationale": "Fallback"},
|
| 335 |
+
],
|
| 336 |
+
"overall_winner": "Draw",
|
| 337 |
+
"summary": "JSON parse fallback."
|
| 338 |
+
}
|
| 339 |
+
pretty = json.dumps(data, indent=2)
|
| 340 |
+
rounds_md = "\n".join([f"- {r['category']}: {r['winner']} — {r.get('rationale','')}" for r in data.get("rounds", [])])
|
| 341 |
+
md = f"Overall Winner: {data.get('overall_winner','Draw')}\n\nRounds:\n{rounds_md}\n\nSummary:\n{data.get('summary','')}"
|
| 342 |
+
return pretty, md
|
| 343 |
+
|
| 344 |
+
# -----------------------------
|
| 345 |
+
# Feature: Sensitive Data Sniffer
|
| 346 |
+
# -----------------------------
|
| 347 |
+
PII_REGEXES = {
|
| 348 |
+
"Email": r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}",
|
| 349 |
+
"Phone": r"\+?\d[\d\-\s]{7,}\d",
|
| 350 |
+
"SSN (US)": r"\b\d{3}-\d{2}-\d{4}\b",
|
| 351 |
+
"Credit Card": r"\b(?:\d[ -]*?){13,16}\b",
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
def sensitive_data_sniffer(text: str) -> Tuple[str, Dict[str, List[str]]]:
|
| 355 |
+
# LLM-based identification of privacy traps plus regex PII
|
| 356 |
+
system = "You find hidden privacy traps in legal text and list personal data categories being shared or processed."
|
| 357 |
+
user = (
|
| 358 |
+
"Return strict JSON: {data_categories: [str], sharing_parties: [str], processing_purposes: [str], risks: [str], recommendations: [str]}.\n"
|
| 359 |
+
f"Text:\n{text[:6000]}"
|
| 360 |
+
)
|
| 361 |
+
resp = llm_generate(system, user, max_new_tokens=700)
|
| 362 |
+
data = None
|
| 363 |
+
try:
|
| 364 |
+
data = json.loads(re.search(r"\{[\s\S]*\}", resp).group(0))
|
| 365 |
+
except Exception:
|
| 366 |
+
data = {
|
| 367 |
+
"data_categories": ["Name", "Email"],
|
| 368 |
+
"sharing_parties": ["Service Provider"],
|
| 369 |
+
"processing_purposes": ["Service delivery"],
|
| 370 |
+
"risks": ["Potential over-collection"],
|
| 371 |
+
"recommendations": ["Narrow purpose", "Limit retention"]
|
| 372 |
+
}
|
| 373 |
+
# Regex-based PII findings
|
| 374 |
+
regex_hits: Dict[str, List[str]] = {}
|
| 375 |
+
for label, pattern in PII_REGEXES.items():
|
| 376 |
+
hits = re.findall(pattern, text or "", flags=re.IGNORECASE)
|
| 377 |
+
if hits:
|
| 378 |
+
regex_hits[label] = sorted(set([h.strip() for h in hits]))
|
| 379 |
+
pretty = json.dumps({"llm": data, "regex_hits": regex_hits}, indent=2)
|
| 380 |
+
return pretty, regex_hits
|
| 381 |
+
|
| 382 |
+
# -----------------------------
|
| 383 |
+
# Feature: Litigation Risk Radar
|
| 384 |
+
# -----------------------------
|
| 385 |
+
def litigation_risk_radar(text: str) -> Tuple[str, str]:
|
| 386 |
+
clauses = split_into_clauses(text)
|
| 387 |
+
sample = "\n\n".join(clauses[:8]) if clauses else text[:4000]
|
| 388 |
+
system = "You identify clauses most likely to trigger disputes or litigation and provide sample dispute scenarios."
|
| 389 |
+
user = (
|
| 390 |
+
"Analyze the clauses and return JSON: {hotspots: [ {clause_excerpt, risk_level: 'Low'|'Medium'|'High', why, sample_dispute_scenario} ]}.\n"
|
| 391 |
+
f"Clauses:\n{sample}"
|
| 392 |
+
)
|
| 393 |
+
resp = llm_generate(system, user, max_new_tokens=900)
|
| 394 |
+
try:
|
| 395 |
+
data = json.loads(re.search(r"\{[\s\S]*\}", resp).group(0))
|
| 396 |
+
except Exception:
|
| 397 |
+
data = {
|
| 398 |
+
"hotspots": [
|
| 399 |
+
{
|
| 400 |
+
"clause_excerpt": (clauses[0][:280] if clauses else text[:280]),
|
| 401 |
+
"risk_level": "Medium",
|
| 402 |
+
"why": "Ambiguous obligations.",
|
| 403 |
+
"sample_dispute_scenario": "Party A alleges non-performance due to unclear milestones."
|
| 404 |
+
}
|
| 405 |
+
]
|
| 406 |
+
}
|
| 407 |
+
pretty = json.dumps(data, indent=2)
|
| 408 |
+
md = "\n".join([
|
| 409 |
+
f"- [{h.get('risk_level','Medium')}] {h.get('clause_excerpt','')}\n Why: {h.get('why','')}\n Scenario: {h.get('sample_dispute_scenario','')}"
|
| 410 |
+
for h in data.get("hotspots", [])
|
| 411 |
+
])
|
| 412 |
+
return pretty, md
|
| 413 |
+
|
| 414 |
+
# -----------------------------
|
| 415 |
+
# Glue: Input handling (upload or paste)
|
| 416 |
+
# -----------------------------
|
| 417 |
+
def get_text_from_inputs(file: Optional[gr.File], text: str) -> str:
|
| 418 |
+
file_text = load_document(file) if file else ""
|
| 419 |
+
final = (text or "").strip()
|
| 420 |
+
if len(file_text) > len(final):
|
| 421 |
+
return file_text
|
| 422 |
+
return final
|