File size: 1,625 Bytes
7af3fdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import base64
import tempfile
from utils import extract_text_from_pdf, simple_clause_split
from model import classify_clauses
from report import generate_pdf
from salesforce_stub import send_to_salesforce

def analyze_contract(file):
    # Hugging Face provides `file.name` (a path to the uploaded file)
    tmp_path = file.name

    text = extract_text_from_pdf(tmp_path)
    clauses = simple_clause_split(text)
    results = classify_clauses(clauses)

    overall_score = sum(r['risk_score'] for r in results) / len(results) if results else 0

    report_path = generate_pdf(results, overall_score)

    highlight_output = ""
    for r in results:
        color = (
            "red" if r['risk_level'] == "High" else
            "orange" if r['risk_level'] == "Medium" else
            "green"
        )
        highlight_output += f"<div style='color:{color}'><b>{r['risk_level']}</b>: {r['clause']}</div><br>"

    send_to_salesforce({
        "clauses": results,
        "overall_score": overall_score
    })

    return f"Overall Risk Score: {overall_score:.2f}", highlight_output, report_path


iface = gr.Interface(
    fn=analyze_contract,
    inputs=gr.File(label="Upload Contract PDF"),
    outputs=[
        gr.Textbox(label="Overall Risk Score"),
        gr.HTML(label="Clause Risk Highlight"),
        gr.File(label="Download Risk Report (PDF)")
    ],
    title="📜 Contract Risk Heatmap Generator",
    description="Upload a contract and get clause-level risk scores with heatmap-style highlights. Powered by Hugging Face Transformers."
)

if __name__ == "__main__":
    iface.launch()