-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
170 lines (144 loc) · 6.27 KB
/
app.py
File metadata and controls
170 lines (144 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Copyright (c) 2026 Marco De Roni. All rights reserved.
# Licensed under the MIT License — see LICENSE file for details.
import streamlit as st
import os
import tempfile
import yaml
import warnings
warnings.filterwarnings("ignore")
from scanner.extractor import extract_text, split_into_clauses
from scanner.metadata import load_rules, extract_metadata
from scanner.analyzer import analyze
from scanner.reporter import generate_report
# Disable presidio at startup to avoid blocking
os.environ["PRESIDIO_DISABLE_CACHE"] = "1"
st.set_page_config(
page_title="Contract Scanner",
page_icon="🔍",
layout="wide"
)
st.title("🔍 Contract Scanner")
st.caption("AI-powered contract review — R/Y/G risk scoring, missing clause detection and metadata extraction")
# ── Sidebar ──
with st.sidebar:
st.header("⚙️ Configuration")
rules_file = st.file_uploader("Upload rules.yaml (optional)", type=["yaml", "yml"])
if rules_file:
rules = yaml.safe_load(rules_file)
st.success("✅ Custom rules loaded")
else:
default_path = "config/rules.yaml"
if os.path.exists(default_path):
rules = load_rules(default_path)
st.info("Using default config/rules.yaml")
else:
rules = None
st.warning("No rules file found")
sanitize_pii = st.checkbox("🔒 Enable PII sanitization", value=True)
# ── Main ──
uploaded_file = st.file_uploader(
"Upload contract (PDF or DOCX)",
type=["pdf", "docx"]
)
if uploaded_file and rules:
if st.button("▶ Analyse Contract", type="primary"):
with st.spinner("Analysing contract..."):
# Save to temp file
suffix = ".pdf" if uploaded_file.name.endswith(".pdf") else ".docx"
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
tmp.write(uploaded_file.getbuffer())
tmp_path = tmp.name
# Extract text
text = extract_text(tmp_path)
clauses = split_into_clauses(text)
# PII sanitization
pii_mapping = {}
if sanitize_pii:
try:
from scanner.sanitizer import sanitize
text, pii_mapping = sanitize(text)
st.info(f"🔒 PII sanitized: {len(pii_mapping)} entities redacted")
except Exception as e:
st.warning(f"PII sanitization skipped: {e}")
# Analyse
metadata = extract_metadata(text, rules)
analysis = analyze(text, clauses, rules)
# Build PII summary
pii_summary = {"total_entities": len(pii_mapping), "breakdown": {}}
for placeholder in pii_mapping.keys():
entity_type = placeholder.split("_")[0].replace("[", "")
pii_summary["breakdown"][entity_type] = pii_summary["breakdown"].get(entity_type, 0) + 1
os.unlink(tmp_path)
# ── Results ──
overall = analysis.get("overall_score", "UNKNOWN")
color_map = {"RED": "🔴", "YELLOW": "🟡", "GREEN": "🟢"}
st.subheader(f"{color_map.get(overall, '⚪')} Overall Risk: {overall}")
tab1, tab2, tab3, tab4 = st.tabs(["📊 Metadata", "⚠️ Missing Clauses", "🔍 Risk Findings", "📄 Clause Extracts"])
with tab1:
st.subheader("Contract Metadata")
meta_fields = {
"parties": "Parties",
"effective_date": "Effective Date",
"governing_law": "Governing Law",
"jurisdiction": "Jurisdiction",
"notice_period": "Notice Period",
"duration": "Duration",
"auto_renewal": "Auto-Renewal",
}
for key, label in meta_fields.items():
value = metadata.get(key, "Not detected")
st.write(f"**{label}:** {value}")
with tab2:
st.subheader("Missing Clauses")
missing = analysis.get("missing_clauses", [])
if missing:
for clause in missing:
st.error(f"⚠️ {clause} — not found in document")
else:
st.success("✅ All required clauses detected")
with tab3:
st.subheader("Risk Findings")
findings = analysis.get("findings", {})
if not findings:
st.info("No risk patterns detected.")
else:
for category, items in findings.items():
st.write(f"**{category.replace('_', ' ').title()}**")
for item in items:
score = item["score"]
conf = item.get("confidence", "")
comment = item["comment"]
if score == "RED":
st.error(f"🔴 {comment} [{conf} confidence]")
elif score == "YELLOW":
st.warning(f"🟡 {comment} [{conf} confidence]")
else:
st.success(f"🟢 {comment} [{conf} confidence]")
if item.get("excerpt"):
st.caption(item["excerpt"])
with tab4:
st.subheader("Clause Extracts")
found_extracts = analysis.get("found_extracts", {})
if found_extracts:
for clause_name, extract in found_extracts.items():
with st.expander(clause_name):
st.write(extract)
else:
st.info("No clause extracts available.")
# ── Download ──
st.subheader("📥 Download Report")
output_dir = tempfile.mkdtemp()
report_path = generate_report(
uploaded_file.name, metadata, analysis, output_dir, pii_summary=pii_summary
)
with open(report_path, "rb") as f:
st.download_button(
"📝 Download Word Report",
f,
file_name=f"report_{uploaded_file.name}.docx",
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
elif not rules:
st.warning("⚠️ No rules file found. Ensure config/rules.yaml exists.")
else:
st.info("👆 Upload a contract to begin analysis.")