Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Load zero-shot classifier
|
| 8 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 9 |
+
|
| 10 |
+
# Candidate labels
|
| 11 |
+
labels = ["high risk", "medium risk", "low risk"]
|
| 12 |
+
|
| 13 |
+
def classify_clauses(text_input):
|
| 14 |
+
# Split input into clauses
|
| 15 |
+
clauses = [clause.strip() for clause in text_input.strip().split('\n') if clause.strip()]
|
| 16 |
+
|
| 17 |
+
scores = []
|
| 18 |
+
for clause in clauses:
|
| 19 |
+
result = classifier(clause, labels)
|
| 20 |
+
scores.append(result['scores'])
|
| 21 |
+
|
| 22 |
+
scores_array = np.array(scores)
|
| 23 |
+
|
| 24 |
+
# Plot heatmap
|
| 25 |
+
plt.figure(figsize=(10, 6))
|
| 26 |
+
sns.heatmap(
|
| 27 |
+
scores_array,
|
| 28 |
+
annot=True,
|
| 29 |
+
xticklabels=labels,
|
| 30 |
+
yticklabels=[f"Clause {i+1}" for i in range(len(clauses))],
|
| 31 |
+
cmap="Reds"
|
| 32 |
+
)
|
| 33 |
+
plt.title("Contract Clause Risk Heatmap")
|
| 34 |
+
plt.xlabel("Risk Level")
|
| 35 |
+
plt.ylabel("Clauses")
|
| 36 |
+
plt.tight_layout()
|
| 37 |
+
|
| 38 |
+
# Save and return the plot
|
| 39 |
+
plot_path = "heatmap.png"
|
| 40 |
+
plt.savefig(plot_path)
|
| 41 |
+
plt.close()
|
| 42 |
+
return plot_path
|
| 43 |
+
|
| 44 |
+
# Gradio UI
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
+
fn=classify_clauses,
|
| 47 |
+
inputs=gr.Textbox(lines=10, label="Enter Contract Clauses (one per line)"),
|
| 48 |
+
outputs=gr.Image(type="filepath"),
|
| 49 |
+
title="Contract Risk Heatmap Generator",
|
| 50 |
+
description="Enter clauses line by line. Uses zero-shot classification to visualize risk levels."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|