garyd1 commited on
Commit
9175756
·
verified ·
1 Parent(s): b336262

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -14
app.py CHANGED
@@ -7,6 +7,7 @@ import matplotlib.pyplot as plt
7
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
8
  from dotenv import load_dotenv
9
  import anthropic
 
10
 
11
  # Load environment variables
12
  load_dotenv()
@@ -118,20 +119,32 @@ if uploaded_file is not None:
118
  else:
119
  parsed_query = ask_gpt(f"Convert this question into a Pandas operation: {query}")
120
 
121
- # Execute the query
122
- try:
123
- result = eval(parsed_query, {"df": df, "pd": pd}) # Ensuring df is correctly referenced
124
- st.write("### Result:")
125
- st.write(result if isinstance(result, pd.DataFrame) else str(result))
126
-
127
- # If numerical data, show a visualization
128
- if isinstance(result, pd.Series) and result.dtype in ["int64", "float64"]:
129
- fig, ax = plt.subplots()
130
- result.plot(kind="bar", ax=ax)
131
- st.pyplot(fig)
132
-
133
- except Exception as e:
134
- st.error(f"Error executing query: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
  # Memory for context retention
137
  if "query_history" not in st.session_state:
 
7
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
8
  from dotenv import load_dotenv
9
  import anthropic
10
+ import ast
11
 
12
  # Load environment variables
13
  load_dotenv()
 
119
  else:
120
  parsed_query = ask_gpt(f"Convert this question into a Pandas operation: {query}")
121
 
122
+ # Validate and clean query
123
+ parsed_query = parsed_query.strip().replace("`", "")
124
+ st.write(f"Parsed Query: `{parsed_query}`")
125
+
126
+ # Check for column existence if query involves a column
127
+ for col in column_names:
128
+ if col in parsed_query and col not in df.columns:
129
+ st.error(f"Error: Column '{col}' not found in the uploaded file.")
130
+ break
131
+ else:
132
+ # Execute the query
133
+ try:
134
+ result = eval(parsed_query, {"df": df, "pd": pd}) # Ensuring df is correctly referenced
135
+ st.write("### Result:")
136
+ st.write(result if isinstance(result, pd.DataFrame) else str(result))
137
+
138
+ # If numerical data, show a visualization
139
+ if isinstance(result, pd.Series) and result.dtype in ["int64", "float64"]:
140
+ fig, ax = plt.subplots()
141
+ result.plot(kind="bar", ax=ax)
142
+ st.pyplot(fig)
143
+
144
+ except SyntaxError as e:
145
+ st.error(f"Syntax Error in parsed query: {str(e)}")
146
+ except Exception as e:
147
+ st.error(f"Error executing query: {str(e)}")
148
 
149
  # Memory for context retention
150
  if "query_history" not in st.session_state: