Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,30 +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 |
-
import plotly.graph_objects as go
|
| 7 |
-
|
| 8 |
-
# Function to generate heatmap
|
| 9 |
-
def generate_heatmap(results):
|
| 10 |
-
risk_levels = {"High": 3, "Medium": 2, "Low": 1}
|
| 11 |
-
|
| 12 |
-
clauses = [r['clause'] for r in results]
|
| 13 |
-
risk_values = [risk_levels.get(r['risk_level'], 1) for r in results]
|
| 14 |
-
|
| 15 |
-
fig = go.Figure(data=go.Heatmap(
|
| 16 |
-
z=[risk_values],
|
| 17 |
-
x=clauses,
|
| 18 |
-
y=["Risk Levels"],
|
| 19 |
-
colorscale='YlOrRd', # You can change the color scale
|
| 20 |
-
colorbar=dict(title="Risk Level")
|
| 21 |
-
))
|
| 22 |
-
|
| 23 |
-
fig.update_layout(
|
| 24 |
-
title="Contract Risk Heatmap",
|
| 25 |
-
xaxis_title="Clause",
|
| 26 |
-
yaxis_title="Risk Level"
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
return fig.to_html(full_html=False)
|
| 30 |
|
| 31 |
# Initialize Gradio interface
|
| 32 |
def analyze_contract(file):
|
|
@@ -38,33 +14,21 @@ def analyze_contract(file):
|
|
| 38 |
overall_score = sum(r['risk_score'] for r in results) / len(results) if results else 0
|
| 39 |
report_path = generate_pdf(results, overall_score)
|
| 40 |
|
| 41 |
-
# Create clause-level risk highlights
|
| 42 |
highlight_output = ""
|
| 43 |
-
high_risk_clauses = ""
|
| 44 |
-
|
| 45 |
for r in results:
|
| 46 |
color = (
|
| 47 |
"red" if r['risk_level'] == "High" else
|
| 48 |
"orange" if r['risk_level'] == "Medium" else
|
| 49 |
"green"
|
| 50 |
)
|
| 51 |
-
|
| 52 |
-
# High-risk clauses separately
|
| 53 |
-
if r['risk_level'] == "High":
|
| 54 |
-
high_risk_clauses += f"<div style='color:{color}'><b>{r['risk_level']}</b>: {r['clause']}</div><br>"
|
| 55 |
-
|
| 56 |
highlight_output += f"<div style='color:{color}'><b>{r['risk_level']}</b>: {r['clause']}</div><br>"
|
| 57 |
|
| 58 |
-
# Generate heatmap HTML
|
| 59 |
-
heatmap_html = generate_heatmap(results)
|
| 60 |
-
|
| 61 |
-
# Send results to Salesforce (if necessary)
|
| 62 |
send_to_salesforce({
|
| 63 |
"clauses": results,
|
| 64 |
"overall_score": overall_score
|
| 65 |
})
|
| 66 |
|
| 67 |
-
return f"Overall Risk Score: {overall_score:.2f}",
|
| 68 |
|
| 69 |
# Create Gradio Interface
|
| 70 |
iface = gr.Interface(
|
|
@@ -72,9 +36,7 @@ iface = gr.Interface(
|
|
| 72 |
inputs=gr.File(label="Upload Contract PDF"),
|
| 73 |
outputs=[
|
| 74 |
gr.Textbox(label="Overall Risk Score"),
|
| 75 |
-
gr.HTML(label="
|
| 76 |
-
gr.HTML(label="Clause Risk Highlight"), # Heatmap-style highlighting
|
| 77 |
-
gr.HTML(label="Risk Heatmap"), # Display heatmap here
|
| 78 |
gr.File(label="Download Risk Report (PDF)")
|
| 79 |
],
|
| 80 |
title="📜 Contract Risk Heatmap Generator",
|
|
@@ -83,5 +45,4 @@ iface = gr.Interface(
|
|
| 83 |
|
| 84 |
# Ensure the Gradio app starts properly
|
| 85 |
if _name_ == "_main_":
|
| 86 |
-
iface.launch()
|
| 87 |
-
|
|
|
|
| 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 |
highlight_output = ""
|
|
|
|
|
|
|
| 18 |
for r in results:
|
| 19 |
color = (
|
| 20 |
"red" if r['risk_level'] == "High" else
|
| 21 |
"orange" if r['risk_level'] == "Medium" else
|
| 22 |
"green"
|
| 23 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
highlight_output += f"<div style='color:{color}'><b>{r['risk_level']}</b>: {r['clause']}</div><br>"
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
send_to_salesforce({
|
| 27 |
"clauses": results,
|
| 28 |
"overall_score": overall_score
|
| 29 |
})
|
| 30 |
|
| 31 |
+
return f"Overall Risk Score: {overall_score:.2f}", highlight_output, report_path
|
| 32 |
|
| 33 |
# Create Gradio Interface
|
| 34 |
iface = gr.Interface(
|
|
|
|
| 36 |
inputs=gr.File(label="Upload Contract PDF"),
|
| 37 |
outputs=[
|
| 38 |
gr.Textbox(label="Overall Risk Score"),
|
| 39 |
+
gr.HTML(label="Clause Risk Highlight"),
|
|
|
|
|
|
|
| 40 |
gr.File(label="Download Risk Report (PDF)")
|
| 41 |
],
|
| 42 |
title="📜 Contract Risk Heatmap Generator",
|
|
|
|
| 45 |
|
| 46 |
# Ensure the Gradio app starts properly
|
| 47 |
if _name_ == "_main_":
|
| 48 |
+
iface.launch()
|
|
|