Spaces:
Configuration error
Configuration error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import base64
|
| 3 |
+
import tempfile
|
| 4 |
+
from utils import extract_text_from_pdf, simple_clause_split
|
| 5 |
+
from model import classify_clauses
|
| 6 |
+
from report import generate_pdf
|
| 7 |
+
from salesforce_stub import send_to_salesforce
|
| 8 |
+
|
| 9 |
+
def analyze_contract(file):
|
| 10 |
+
# Hugging Face provides `file.name` (a path to the uploaded file)
|
| 11 |
+
tmp_path = file.name
|
| 12 |
+
|
| 13 |
+
text = extract_text_from_pdf(tmp_path)
|
| 14 |
+
clauses = simple_clause_split(text)
|
| 15 |
+
results = classify_clauses(clauses)
|
| 16 |
+
|
| 17 |
+
overall_score = sum(r['risk_score'] for r in results) / len(results) if results else 0
|
| 18 |
+
|
| 19 |
+
report_path = generate_pdf(results, overall_score)
|
| 20 |
+
|
| 21 |
+
highlight_output = ""
|
| 22 |
+
for r in results:
|
| 23 |
+
color = (
|
| 24 |
+
"red" if r['risk_level'] == "High" else
|
| 25 |
+
"orange" if r['risk_level'] == "Medium" else
|
| 26 |
+
"green"
|
| 27 |
+
)
|
| 28 |
+
highlight_output += f"<div style='color:{color}'><b>{r['risk_level']}</b>: {r['clause']}</div><br>"
|
| 29 |
+
|
| 30 |
+
send_to_salesforce({
|
| 31 |
+
"clauses": results,
|
| 32 |
+
"overall_score": overall_score
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
return f"Overall Risk Score: {overall_score:.2f}", highlight_output, report_path
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
iface = gr.Interface(
|
| 39 |
+
fn=analyze_contract,
|
| 40 |
+
inputs=gr.File(label="Upload Contract PDF"),
|
| 41 |
+
outputs=[
|
| 42 |
+
gr.Textbox(label="Overall Risk Score"),
|
| 43 |
+
gr.HTML(label="Clause Risk Highlight"),
|
| 44 |
+
gr.File(label="Download Risk Report (PDF)")
|
| 45 |
+
],
|
| 46 |
+
title="📜 Contract Risk Heatmap Generator",
|
| 47 |
+
description="Upload a contract and get clause-level risk scores with heatmap-style highlights. Powered by Hugging Face Transformers."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
iface.launch()
|