jzou19950715 commited on
Commit
dbaa1d5
·
verified ·
1 Parent(s): 2ef0503

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -0
app.py CHANGED
@@ -54,6 +54,27 @@ class DataAnalyzer:
54
  except Exception as e:
55
  return f"API Error: {str(e)}"
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  def analyze_data_types(self, df: pd.DataFrame) -> Dict:
58
  """Analyze data types and basic statistics of the DataFrame"""
59
  analysis = {
 
54
  except Exception as e:
55
  return f"API Error: {str(e)}"
56
 
57
+ def evaluate_code(self, code: str, state: Dict = None) -> Tuple[Any, str]:
58
+ """Safely evaluate Python code with proper state management and security"""
59
+ if state is None:
60
+ state = {"print_outputs": ""}
61
+
62
+ # Create safe environment with allowed imports
63
+ safe_env = {
64
+ "pd": pd,
65
+ "np": np,
66
+ "plt": plt,
67
+ "sns": sns,
68
+ "stats": stats,
69
+ "print": lambda *args: state.update({"print_outputs": state["print_outputs"] + " ".join(map(str, args)) + "\n"}),
70
+ }
71
+
72
+ try:
73
+ exec(code, safe_env, state)
74
+ return state.get("result", None), state["print_outputs"]
75
+ except Exception as e:
76
+ raise RuntimeError(f"Code execution failed: {str(e)}")
77
+
78
  def analyze_data_types(self, df: pd.DataFrame) -> Dict:
79
  """Analyze data types and basic statistics of the DataFrame"""
80
  analysis = {