ABDALLAH31 commited on
Commit
2b4a7cb
Β·
verified Β·
1 Parent(s): c1a06e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -56
app.py CHANGED
@@ -1,63 +1,58 @@
 
 
 
 
1
  import gradio as gr
2
- import pdfplumber
3
- import plotly.graph_objects as go
4
-
5
- # Function to extract text from PDF
6
- def extract_text_from_pdf(file_path):
7
- with pdfplumber.open(file_path) as pdf:
8
- text = ''
9
- for page in pdf.pages:
10
- text += page.extract_text()
11
- return text
12
-
13
- # Function to generate heatmap from extracted text data (risk scores for clauses)
14
- def generate_heatmap(text):
15
- # Assume you already have a mechanism to classify risk levels per clause
16
- clauses = text.split('\n') # Example: split by newlines to get clauses
17
- risk_levels = ['High', 'Medium', 'Low'] # Placeholder risk levels for simplicity
18
-
19
- # Map risk levels to numeric values for plotting
20
- risk_map = {'High': 3, 'Medium': 2, 'Low': 1}
21
- risk_values = [risk_map.get(level, 1) for level in risk_levels * (len(clauses) // len(risk_levels))]
22
-
23
- # Create the heatmap
24
- fig = go.Figure(data=go.Heatmap(
25
- z=[risk_values],
26
- x=clauses[:len(risk_values)], # Only use the first N clauses
27
- y=["Risk Levels"],
28
- colorscale='YlOrRd', # You can change the color scale
29
- colorbar=dict(title="Risk Level")
30
- ))
31
-
32
- # Customize the layout
33
- fig.update_layout(
34
- title="Contract Risk Heatmap",
35
- xaxis_title="Clause",
36
- yaxis_title="Risk Level"
37
- )
38
-
39
- # Return the HTML representation of the heatmap
40
- return fig.to_html(full_html=False)
41
-
42
- # Function to process the uploaded file and display the heatmap
43
- def process_contract(file):
44
- # Extract text from PDF
45
- text = extract_text_from_pdf(file.name)
46
-
47
- # Generate heatmap
48
- heatmap_html = generate_heatmap(text)
49
-
50
- return heatmap_html
51
-
52
- # Gradio Interface
53
  iface = gr.Interface(
54
- fn=process_contract, # Function that processes the contract
55
- inputs=gr.File(label="Upload Contract PDF"), # Input: file upload
56
- outputs=gr.HTML(label="Contract Risk Heatmap"), # Output: HTML content (heatmap)
57
  title="πŸ“œ Contract Risk Heatmap Generator",
58
- description="Upload a contract and get clause-level risk scores with heatmap-style highlights."
59
  )
60
 
61
- # Run the interface
62
  if __name__ == "__main__":
63
  iface.launch()
 
1
+ from transformers import pipeline
2
+ import matplotlib.pyplot as plt
3
+ import seaborn as sns
4
+ import numpy as np
5
  import gradio as gr
6
+
7
+ # Step 1: Load the classifier model
8
+ classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
9
+
10
+ # Step 2: Function to process the input and generate the heatmap
11
+ def generate_heatmap(file):
12
+ # Example clauses from a contract
13
+ clauses = [
14
+ "The contract may be terminated by either party with a 30-day notice.",
15
+ "The vendor is liable for all damages during service interruptions.",
16
+ "User data may be shared with third parties without notice.",
17
+ "Payment must be made within 15 days of invoice receipt.",
18
+ "No liability is accepted for indirect damages."
19
+ ]
20
+
21
+ # Step 3: Define candidate labels for risk
22
+ labels = ["high risk", "medium risk", "low risk"]
23
+
24
+ # Step 4: Generate scores for each clause
25
+ scores = []
26
+ for clause in clauses:
27
+ result = classifier(clause, labels)
28
+ scores.append(result['scores'])
29
+
30
+ # Convert to NumPy array for heatmap
31
+ scores_array = np.array(scores)
32
+
33
+ # Step 5: Plot heatmap
34
+ plt.figure(figsize=(10, 6))
35
+ sns.heatmap(scores_array, annot=True, xticklabels=labels, yticklabels=[f"Clause {i+1}" for i in range(len(clauses))], cmap="Reds")
36
+ plt.title("Contract Clause Risk Heatmap")
37
+ plt.xlabel("Risk Level")
38
+ plt.ylabel("Clauses")
39
+
40
+ # Save the heatmap as an image file
41
+ heatmap_path = "/mnt/data/contract_risk_heatmap.png"
42
+ plt.savefig(heatmap_path)
43
+ plt.close()
44
+
45
+ return heatmap_path
46
+
47
+ # Step 6: Create Gradio interface
 
 
 
 
 
 
 
 
 
48
  iface = gr.Interface(
49
+ fn=generate_heatmap,
50
+ inputs=gr.File(label="Upload Contract Document"),
51
+ outputs=gr.Image(label="Risk Heatmap"),
52
  title="πŸ“œ Contract Risk Heatmap Generator",
53
+ description="Upload a contract and get a clause-level risk heatmap."
54
  )
55
 
56
+ # Ensure the Gradio app starts properly
57
  if __name__ == "__main__":
58
  iface.launch()