File size: 1,499 Bytes
4056e49
f55126b
 
 
2b4a7cb
 
f55126b
 
2b4a7cb
f55126b
 
 
 
 
 
9fbc013
f55126b
 
 
 
fb99b8d
f55126b
6d45671
f55126b
 
 
 
 
 
 
 
9fbc013
f55126b
 
 
 
 
 
 
 
 
 
2b4a7cb
f55126b
 
 
 
 
5cb2315
f55126b
5cb2315
 
dcce784
 
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
import gradio as gr
from transformers import pipeline
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Load zero-shot classifier
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")

# Candidate labels
labels = ["high risk", "medium risk", "low risk"]

def classify_clauses(text_input):
    # Split input into clauses
    clauses = [clause.strip() for clause in text_input.strip().split('\n') if clause.strip()]
    
    scores = []
    for clause in clauses:
        result = classifier(clause, labels)
        scores.append(result['scores'])

    scores_array = np.array(scores)

    # Plot heatmap
    plt.figure(figsize=(10, 6))
    sns.heatmap(
        scores_array,
        annot=True,
        xticklabels=labels,
        yticklabels=[f"Clause {i+1}" for i in range(len(clauses))],
        cmap="Reds"
    )
    plt.title("Contract Clause Risk Heatmap")
    plt.xlabel("Risk Level")
    plt.ylabel("Clauses")
    plt.tight_layout()
    
    # Save and return the plot
    plot_path = "heatmap.png"
    plt.savefig(plot_path)
    plt.close()
    return plot_path

# Gradio UI
demo = gr.Interface(
    fn=classify_clauses,
    inputs=gr.Textbox(lines=10, label="Enter Contract Clauses (one per line)"),
    outputs=gr.Image(type="filepath"),
    title="Contract Risk Heatmap Generator",
    description="Enter clauses line by line. Uses zero-shot classification to visualize risk levels."
)

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