Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,35 +3,6 @@ from utils import extract_text_from_pdf, simple_clause_split
|
|
| 3 |
from model import classify_clauses
|
| 4 |
from report import generate_pdf
|
| 5 |
from salesforce_stub import send_to_salesforce
|
| 6 |
-
from plotly.graph_objects import go
|
| 7 |
-
|
| 8 |
-
# Heatmap function
|
| 9 |
-
def generate_heatmap(results):
|
| 10 |
-
risk_levels = {'High': 3, 'Medium': 2, 'Low': 1}
|
| 11 |
-
x = []
|
| 12 |
-
y = []
|
| 13 |
-
z = []
|
| 14 |
-
|
| 15 |
-
for idx, r in enumerate(results):
|
| 16 |
-
x.append(r['clause']) # Add clause text as x-axis
|
| 17 |
-
y.append(r['risk_level']) # Add risk level as y-axis
|
| 18 |
-
z.append(risk_levels.get(r['risk_level'], 1)) # Map risk level to numeric value
|
| 19 |
-
|
| 20 |
-
# Create a heatmap
|
| 21 |
-
fig = go.Figure(data=go.Heatmap(
|
| 22 |
-
z=[z],
|
| 23 |
-
x=x,
|
| 24 |
-
y=y,
|
| 25 |
-
colorscale='YlOrRd'
|
| 26 |
-
))
|
| 27 |
-
|
| 28 |
-
fig.update_layout(
|
| 29 |
-
title="Contract Risk Heatmap",
|
| 30 |
-
xaxis_title="Clause",
|
| 31 |
-
yaxis_title="Risk Level",
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
return fig.to_html(full_html=False)
|
| 35 |
|
| 36 |
# Initialize Gradio interface
|
| 37 |
def analyze_contract(file):
|
|
@@ -43,21 +14,29 @@ def analyze_contract(file):
|
|
| 43 |
overall_score = sum(r['risk_score'] for r in results) / len(results) if results else 0
|
| 44 |
report_path = generate_pdf(results, overall_score)
|
| 45 |
|
| 46 |
-
#
|
|
|
|
| 47 |
high_risk_clauses = ""
|
|
|
|
| 48 |
for r in results:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
if r['risk_level'] == "High":
|
| 50 |
-
high_risk_clauses += f"<div style='color:
|
| 51 |
|
| 52 |
-
|
| 53 |
-
heatmap_html = generate_heatmap(results)
|
| 54 |
|
| 55 |
send_to_salesforce({
|
| 56 |
"clauses": results,
|
| 57 |
"overall_score": overall_score
|
| 58 |
})
|
| 59 |
|
| 60 |
-
return f"Overall Risk Score: {overall_score:.2f}", high_risk_clauses,
|
| 61 |
|
| 62 |
# Create Gradio Interface
|
| 63 |
iface = gr.Interface(
|
|
@@ -66,7 +45,7 @@ iface = gr.Interface(
|
|
| 66 |
outputs=[
|
| 67 |
gr.Textbox(label="Overall Risk Score"),
|
| 68 |
gr.HTML(label="High-Risk Clauses"), # Display high-risk clauses here
|
| 69 |
-
gr.HTML(label="
|
| 70 |
gr.File(label="Download Risk Report (PDF)")
|
| 71 |
],
|
| 72 |
title="📜 Contract Risk Heatmap Generator",
|
|
|
|
| 3 |
from model import classify_clauses
|
| 4 |
from report import generate_pdf
|
| 5 |
from salesforce_stub import send_to_salesforce
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Initialize Gradio interface
|
| 8 |
def analyze_contract(file):
|
|
|
|
| 14 |
overall_score = sum(r['risk_score'] for r in results) / len(results) if results else 0
|
| 15 |
report_path = generate_pdf(results, overall_score)
|
| 16 |
|
| 17 |
+
# Create clause-level risk highlights with heatmap-style intensity
|
| 18 |
+
highlight_output = ""
|
| 19 |
high_risk_clauses = ""
|
| 20 |
+
|
| 21 |
for r in results:
|
| 22 |
+
color = (
|
| 23 |
+
"red" if r['risk_level'] == "High" else
|
| 24 |
+
"orange" if r['risk_level'] == "Medium" else
|
| 25 |
+
"green"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# High-risk clauses separately
|
| 29 |
if r['risk_level'] == "High":
|
| 30 |
+
high_risk_clauses += f"<div style='color:{color}'><b>{r['risk_level']}</b>: {r['clause']}</div><br>"
|
| 31 |
|
| 32 |
+
highlight_output += f"<div style='color:{color}'><b>{r['risk_level']}</b>: {r['clause']}</div><br>"
|
|
|
|
| 33 |
|
| 34 |
send_to_salesforce({
|
| 35 |
"clauses": results,
|
| 36 |
"overall_score": overall_score
|
| 37 |
})
|
| 38 |
|
| 39 |
+
return f"Overall Risk Score: {overall_score:.2f}", high_risk_clauses, highlight_output, report_path
|
| 40 |
|
| 41 |
# Create Gradio Interface
|
| 42 |
iface = gr.Interface(
|
|
|
|
| 45 |
outputs=[
|
| 46 |
gr.Textbox(label="Overall Risk Score"),
|
| 47 |
gr.HTML(label="High-Risk Clauses"), # Display high-risk clauses here
|
| 48 |
+
gr.HTML(label="Clause Risk Highlight"), # Heatmap-style highlighting
|
| 49 |
gr.File(label="Download Risk Report (PDF)")
|
| 50 |
],
|
| 51 |
title="📜 Contract Risk Heatmap Generator",
|