yatabase1 commited on
Commit
de1638f
·
verified ·
1 Parent(s): f37cf18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -6,6 +6,7 @@ import openai
6
  import matplotlib.pyplot as plt
7
  from dotenv import load_dotenv
8
  from PIL import Image
 
9
 
10
  # Load your OpenAI API key from the environment (Hugging Face Spaces secrets will populate it)
11
  load_dotenv()
@@ -140,17 +141,12 @@ User Query: "{nl_query}"
140
  code = response.choices[0].message.content.strip()
141
  return code
142
 
 
143
  def safe_exec_code(code, df, capture_charts=True, interactive=False, extra_globals=None):
144
  """
145
  Execute the generated code in a restricted namespace.
146
-
147
- - Remove any lines starting with triple backticks.
148
- - First try to evaluate the code as an expression using eval().
149
- - If that fails, execute it via exec() and expect a variable 'result' or 'basic_info'.
150
- - If capture_charts is True, override plt.show() to capture charts as PIL Images.
151
- After executing the code, if interactive is True, open the images.
152
- - The extra_globals parameter can be used to inject additional global variables.
153
- - Returns a tuple (output, charts) where output is the value of 'result' or 'basic_info'.
154
  """
155
  # Remove markdown code fences (lines starting with ```).
156
  code_lines = code.splitlines()
@@ -195,14 +191,16 @@ def safe_exec_code(code, df, capture_charts=True, interactive=False, extra_globa
195
  try:
196
  compiled_expr = compile(clean_code, "<string>", "eval")
197
  output = eval(compiled_expr, safe_globals, safe_locals)
198
- except Exception:
199
  try:
200
  exec(clean_code, safe_globals, safe_locals)
201
  output = safe_locals.get("result", None)
202
  if output is None:
203
  output = safe_locals.get("basic_info", None)
204
  except Exception as ex:
205
- return "An error occurred during code execution. Please check your code or try again.", safe_globals["charts"]
 
 
206
 
207
  if capture_charts and not safe_globals["charts"]:
208
  fig_nums = plt.get_fignums()
@@ -223,6 +221,7 @@ def safe_exec_code(code, df, capture_charts=True, interactive=False, extra_globa
223
  output = "No output variable ('result' or 'basic_info') was set by the code."
224
  return output, safe_globals["charts"]
225
 
 
226
  def generate_interpretation(analysis_result, nl_query):
227
  """
228
  Use OpenAI to generate a detailed interpretation of the analysis result.
 
6
  import matplotlib.pyplot as plt
7
  from dotenv import load_dotenv
8
  from PIL import Image
9
+ import traceback
10
 
11
  # Load your OpenAI API key from the environment (Hugging Face Spaces secrets will populate it)
12
  load_dotenv()
 
141
  code = response.choices[0].message.content.strip()
142
  return code
143
 
144
+
145
  def safe_exec_code(code, df, capture_charts=True, interactive=False, extra_globals=None):
146
  """
147
  Execute the generated code in a restricted namespace.
148
+ Returns a tuple (output, charts) where output is the value of 'result' or 'basic_info'.
149
+ In case of an error, returns a detailed error message.
 
 
 
 
 
 
150
  """
151
  # Remove markdown code fences (lines starting with ```).
152
  code_lines = code.splitlines()
 
191
  try:
192
  compiled_expr = compile(clean_code, "<string>", "eval")
193
  output = eval(compiled_expr, safe_globals, safe_locals)
194
+ except Exception as ex:
195
  try:
196
  exec(clean_code, safe_globals, safe_locals)
197
  output = safe_locals.get("result", None)
198
  if output is None:
199
  output = safe_locals.get("basic_info", None)
200
  except Exception as ex:
201
+ # Capture the full traceback and exception details
202
+ error_details = traceback.format_exc()
203
+ return f"An error occurred during code execution:\n{error_details}", safe_globals["charts"]
204
 
205
  if capture_charts and not safe_globals["charts"]:
206
  fig_nums = plt.get_fignums()
 
221
  output = "No output variable ('result' or 'basic_info') was set by the code."
222
  return output, safe_globals["charts"]
223
 
224
+
225
  def generate_interpretation(analysis_result, nl_query):
226
  """
227
  Use OpenAI to generate a detailed interpretation of the analysis result.