VEDAGI1 commited on
Commit
7fa9c2d
·
verified ·
1 Parent(s): 318a48c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py CHANGED
@@ -65,6 +65,20 @@ PHI_PATTERNS = [
65
  (re.compile(r"\b\d{5}(-\d{4})?\b"), "[REDACTED_ZIP]"),
66
  ]
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  def redact_phi(text: str) -> str:
69
  if not isinstance(text, str):
70
  return text
@@ -231,6 +245,27 @@ def handle(user_msg: str, files: list, yield_update) -> str:
231
  with redirect_stdout(output_buffer):
232
  exec(analysis_script, execution_namespace)
233
  raw_data_output = output_buffer.getvalue()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  except Exception as e:
235
  return (
236
  f"An error occurred executing the script: {e}\n\nGenerated Script:\n"
 
65
  (re.compile(r"\b\d{5}(-\d{4})?\b"), "[REDACTED_ZIP]"),
66
  ]
67
 
68
+ # ------------------------------------------------------------------
69
+ # Helper to safely convert pandas scalars → native Python types
70
+ # ------------------------------------------------------------------
71
+ def to_python(val):
72
+ """Convert pandas/numpy scalars to native Python types for JSON serialization"""
73
+ import numpy as np
74
+ if isinstance(val, (np.integer, np.int64)):
75
+ return int(val)
76
+ if isinstance(val, (np.floating, np.float64)):
77
+ return float(val)
78
+ if hasattr(val, 'item'):
79
+ return val.item()
80
+ return val
81
+
82
  def redact_phi(text: str) -> str:
83
  if not isinstance(text, str):
84
  return text
 
245
  with redirect_stdout(output_buffer):
246
  exec(analysis_script, execution_namespace)
247
  raw_data_output = output_buffer.getvalue()
248
+ # ←←← ADD THIS SAFETY WRAPPER
249
+ try:
250
+ raw_data = json.loads(raw_data_output)
251
+ except json.JSONDecodeError:
252
+ # Sometimes the model prints extra text → try to extract JSON
253
+ import re
254
+ json_match = re.search(r'\{.*\}', raw_data_output, re.DOTALL)
255
+ if json_match:
256
+ raw_data = json.loads(json_match.group(0))
257
+ else:
258
+ raise ValueError("No valid JSON found in script output")
259
+ # Convert any remaining pandas types safely
260
+ def convert_pandas(obj):
261
+ if isinstance(obj, dict):
262
+ return {k: convert_pandas(v) for k, v in obj.items()}
263
+ elif isinstance(obj, list):
264
+ return [convert_pandas(v) for v in obj]
265
+ else:
266
+ return to_python(obj)
267
+ raw_data = convert_pandas(raw_data)
268
+ raw_data_json = json.dumps(raw_data)
269
  except Exception as e:
270
  return (
271
  f"An error occurred executing the script: {e}\n\nGenerated Script:\n"