Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,38 +5,34 @@ import numpy as np
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
def generate_heatmap(results):
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 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 |
-
#
|
| 23 |
-
fig =
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
return
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|