yatabase1 commited on
Commit
03b8d2f
·
verified ·
1 Parent(s): ce17304

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -33,7 +33,7 @@ def load_file(file):
33
  else:
34
  return None
35
  except Exception as e:
36
- print(e)
37
  return None
38
  return df
39
  else:
@@ -48,16 +48,16 @@ def load_file(file):
48
  df = pd.read_excel(file, engine='xlrd')
49
  else:
50
  return None
51
- except Exception:
 
52
  return None
53
  return df
54
 
55
-
56
  def preview_file(file):
57
  """Return the DataFrame for preview."""
58
  df = load_file(file)
59
  if df is None:
60
- # Return a DataFrame with an error message instead of a string
61
  return pd.DataFrame({"Error": ["Error loading file or unsupported file type."]})
62
  return df
63
 
@@ -141,7 +141,6 @@ User Query: "{nl_query}"
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.
@@ -198,8 +197,10 @@ def safe_exec_code(code, df, capture_charts=True, interactive=False, extra_globa
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"]:
@@ -221,7 +222,6 @@ def safe_exec_code(code, df, capture_charts=True, interactive=False, extra_globa
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.
 
33
  else:
34
  return None
35
  except Exception as e:
36
+ print("Error loading file from dict:", e)
37
  return None
38
  return df
39
  else:
 
48
  df = pd.read_excel(file, engine='xlrd')
49
  else:
50
  return None
51
+ except Exception as e:
52
+ print("Error loading file from file-like object:", e)
53
  return None
54
  return df
55
 
 
56
  def preview_file(file):
57
  """Return the DataFrame for preview."""
58
  df = load_file(file)
59
  if df is None:
60
+ # Return a DataFrame with an error message instead of a plain string
61
  return pd.DataFrame({"Error": ["Error loading file or unsupported file type."]})
62
  return df
63
 
 
141
  code = response.choices[0].message.content.strip()
142
  return code
143
 
 
144
  def safe_exec_code(code, df, capture_charts=True, interactive=False, extra_globals=None):
145
  """
146
  Execute the generated code in a restricted namespace.
 
197
  if output is None:
198
  output = safe_locals.get("basic_info", None)
199
  except Exception as ex:
 
200
  error_details = traceback.format_exc()
201
+ # Append a hint for KeyError related issues.
202
+ if "KeyError" in error_details:
203
+ error_details += "\nHint: The generated code might be referencing columns that do not exist in your DataFrame."
204
  return f"An error occurred during code execution:\n{error_details}", safe_globals["charts"]
205
 
206
  if capture_charts and not safe_globals["charts"]:
 
222
  output = "No output variable ('result' or 'basic_info') was set by the code."
223
  return output, safe_globals["charts"]
224
 
 
225
  def generate_interpretation(analysis_result, nl_query):
226
  """
227
  Use OpenAI to generate a detailed interpretation of the analysis result.