ABDALLAH31 commited on
Commit
9fbc013
·
verified ·
1 Parent(s): 6d45671

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -30
app.py CHANGED
@@ -5,38 +5,34 @@ import numpy as np
5
  import os
6
 
7
  def generate_heatmap(results):
8
- # Check the structure of the results
9
- print("Results:", results)
 
 
10
 
11
- # If the results are strings (e.g., just the clauses)
12
- if isinstance(results, list) and isinstance(results[0], str):
13
- clauses = results # Directly use clauses
14
- # For simplicity, assume all clauses are "high risk" here for testing purposes
15
- risk_values = [3 for _ in clauses] # Replace with actual risk assessment logic
16
- else:
17
- # Assuming results are in the format [{'clause': ..., 'risk_level': ...}, ...]
18
- clauses = [r['clause'] for r in results] # Extract clause text
19
- risk_levels = {"High": 3, "Medium": 2, "Low": 1}
20
- risk_values = [risk_levels.get(r['risk_level'], 1) for r in results] # Map risk level to value
21
 
22
- # Plot heatmap
23
- fig = plt.figure(figsize=(10, 6))
24
- sns.heatmap([risk_values], annot=True, xticklabels=clauses, yticklabels=["Risk Levels"], cmap="YlOrRd")
 
 
 
 
 
25
 
26
- # Save heatmap image
27
- heatmap_path = os.path.join(os.getcwd(), 'contract_risk_heatmap.png')
28
- plt.savefig(heatmap_path)
 
 
 
29
 
30
- return heatmap_path
31
 
32
- # Gradio interface
33
- iface = gr.Interface(
34
- fn=generate_heatmap,
35
- inputs=gr.File(label="Upload Contract PDF"),
36
- outputs=gr.File(label="Download Heatmap Image"),
37
- title="📜 Contract Risk Heatmap Generator",
38
- description="Upload a contract and get a clause-level risk heatmap."
39
- )
40
-
41
- # Start the app
42
- iface.launch()
 
5
  import os
6
 
7
  def generate_heatmap(results):
8
+ # If results is a string, process it into a list of dicts (or just use the text as is)
9
+ if isinstance(results, str): # Check if results is a string
10
+ # For testing, we will mock the results with dummy values if it's just text
11
+ results = [{"clause": results, "risk_level": "High"}]
12
 
13
+ # Assuming results is now a list of dictionaries with keys 'clause' and 'risk_level'
14
+ clauses = [r['clause'] for r in results] # Extract clause text
15
+ risk_levels = [r['risk_level'] for r in results] # Extract risk levels
16
+
17
+ # Map risk levels to numeric values for plotting
18
+ risk_map = {'High': 3, 'Medium': 2, 'Low': 1}
19
+ risk_values = [risk_map.get(risk, 0) for risk in risk_levels]
 
 
 
20
 
21
+ # Create a heatmap
22
+ fig = go.Figure(data=go.Heatmap(
23
+ z=[risk_values], # The data for the heatmap
24
+ x=clauses, # Clause text as the x-axis labels
25
+ y=["Risk Levels"], # Y-axis representing risk levels
26
+ colorscale='YlOrRd', # You can change the color scale
27
+ colorbar=dict(title="Risk Level")
28
+ ))
29
 
30
+ # Add titles and labels to the heatmap
31
+ fig.update_layout(
32
+ title="Contract Risk Heatmap",
33
+ xaxis_title="Clause",
34
+ yaxis_title="Risk Level"
35
+ )
36
 
37
+ return fig.to_html(full_html=False)
38