Kushalmanda commited on
Commit
d08ea58
·
verified ·
1 Parent(s): 35089b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import gradio as gr
2
  import pdfplumber
3
  import matplotlib.pyplot as plt
4
  import numpy as np
@@ -47,7 +47,7 @@ def find_penalty_values(text: str) -> List[float]:
47
  for match in matches:
48
  penalty_text = match.group()
49
  try:
50
- if any(word in penalty_text.lower() for word in ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'hundred', 'thousand', 'million']):
51
  penalty_value = w2n.word_to_num(penalty_text.split('dollars')[0].strip())
52
  else:
53
  penalty_value = float(re.sub(r'[^\d.]', '', penalty_text))
@@ -87,7 +87,7 @@ def generate_heatmap(risk_level: str) -> plt.Figure:
87
  """Generate a horizontal heatmap based on detected risk levels"""
88
  fig, ax = plt.subplots(figsize=(8, 2))
89
 
90
- # Set default values for the risks
91
  risk_values = {
92
  "Low": 0,
93
  "Medium": 0,
@@ -102,24 +102,26 @@ def generate_heatmap(risk_level: str) -> plt.Figure:
102
  elif risk_level == "High":
103
  risk_values["High"] = 100
104
 
105
- # Create horizontal heatmap using the risk values
106
- gradient = np.array([risk_values["Low"], risk_values["Medium"], risk_values["High"]]).reshape(1, -1)
107
- ax.imshow(gradient, aspect='auto', cmap='RdYlGn') # RdYlGn for Green to Red
108
 
109
- # Set labels for the heatmap
 
 
 
 
110
  ax.set_yticks([])
111
  ax.set_xticks([0, 1, 2])
112
  ax.set_xticklabels(['Low Risk', 'Medium Risk', 'High Risk'])
113
 
114
- # Add text for the risk level on the heatmap
115
- ax.text(0, 0.5, f"Low Risk: {risk_values['Low']}%", color='black', ha='center', va='center', fontsize=14, fontweight='bold')
116
  ax.text(1, 0.5, f"Medium Risk: {risk_values['Medium']}%", color='black', ha='center', va='center', fontsize=14, fontweight='bold')
117
- ax.text(2, 0.5, f"High Risk: {risk_values['High']}%", color='black', ha='center', va='center', fontsize=14, fontweight='bold')
118
 
119
  ax.set_axis_off()
120
  plt.tight_layout()
121
 
122
- # Return as a file object for Gradio to handle
123
  return fig
124
 
125
  def analyze_pdf(file_obj) -> List:
 
1
+ import gradio as gr
2
  import pdfplumber
3
  import matplotlib.pyplot as plt
4
  import numpy as np
 
47
  for match in matches:
48
  penalty_text = match.group()
49
  try:
50
+ if any(word in penalty_text.lower() for word in ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'hundred', 'thousand', 'million']):
51
  penalty_value = w2n.word_to_num(penalty_text.split('dollars')[0].strip())
52
  else:
53
  penalty_value = float(re.sub(r'[^\d.]', '', penalty_text))
 
87
  """Generate a horizontal heatmap based on detected risk levels"""
88
  fig, ax = plt.subplots(figsize=(8, 2))
89
 
90
+ # Initialize all risks to 0
91
  risk_values = {
92
  "Low": 0,
93
  "Medium": 0,
 
102
  elif risk_level == "High":
103
  risk_values["High"] = 100
104
 
105
+ # Define the colors for each risk level
106
+ colors = ['#28a745', '#ffc107', '#dc3545'] # Green, Orange, Red
 
107
 
108
+ # Plot the heatmap with each color representing a risk
109
+ gradient = np.array([risk_values["Low"], risk_values["Medium"], risk_values["High"]]).reshape(1, -1)
110
+ ax.imshow(gradient, aspect='auto', cmap='RdYlGn') # Color scale: Green to Red
111
+
112
+ # Set the labels for the heatmap
113
  ax.set_yticks([])
114
  ax.set_xticks([0, 1, 2])
115
  ax.set_xticklabels(['Low Risk', 'Medium Risk', 'High Risk'])
116
 
117
+ # Add the corresponding risk labels
118
+ ax.text(0, 0.5, f"Low Risk: {risk_values['Low']}%", color='white', ha='center', va='center', fontsize=14, fontweight='bold')
119
  ax.text(1, 0.5, f"Medium Risk: {risk_values['Medium']}%", color='black', ha='center', va='center', fontsize=14, fontweight='bold')
120
+ ax.text(2, 0.5, f"High Risk: {risk_values['High']}%", color='white', ha='center', va='center', fontsize=14, fontweight='bold')
121
 
122
  ax.set_axis_off()
123
  plt.tight_layout()
124
 
 
125
  return fig
126
 
127
  def analyze_pdf(file_obj) -> List: