Adityabhaskar commited on
Commit
14a889e
·
verified ·
1 Parent(s): 411ac2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -107,7 +107,7 @@ class ExcelAIQuerySystem:
107
  return list(self.excel_data.keys())
108
 
109
  def query_data(self, query: str, target_sheet: str = "Auto-Select") -> Dict[str, Any]:
110
- """--- MODIFIED: Processes a query, either against a specific sheet or by auto-selecting the most relevant ones. ---"""
111
  results = {'query': query, 'relevant_sheets': [], 'sheet_results': {}, 'summary': ''}
112
 
113
  try:
@@ -126,7 +126,14 @@ class ExcelAIQuerySystem:
126
  if sheet_name not in self.excel_data: continue
127
 
128
  df = self.excel_data[sheet_name]
129
- pandas_agent = create_pandas_dataframe_agent(self.llm, df, agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
 
 
 
 
 
 
 
130
  response = pandas_agent.invoke(query)
131
  results['sheet_results'][sheet_name] = {'response': response['output']}
132
 
@@ -148,7 +155,7 @@ class ExcelAIQuerySystem:
148
  # --- Gradio Interface ---
149
 
150
  def process_file(api_key, file_obj):
151
- """--- MODIFIED: Also returns the list of sheet names to populate the dropdown. ---"""
152
  if not api_key: raise gr.Error("OpenAI API Key is required.")
153
  if file_obj is None: raise gr.Error("Please upload an Excel file.")
154
  try:
@@ -168,7 +175,7 @@ def process_file(api_key, file_obj):
168
  raise gr.Error(f"Failed to process file: {e}")
169
 
170
  def generate_response(query, selected_sheet, system_state):
171
- """--- MODIFIED: Passes the selected sheet to the query function. ---"""
172
  if not query: raise gr.Error("Please enter a query.")
173
  if system_state is None: raise gr.Error("File not loaded. Please upload and load a file first.")
174
 
@@ -197,7 +204,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Excel AI Query System") as demo:
197
 
198
  with gr.Column(scale=2):
199
  gr.Markdown("### 2. Ask a Question")
200
- # --- NEW: Dropdown for sheet selection ---
201
  sheet_selector = gr.Dropdown(
202
  label="Select a sheet to query",
203
  info="Choose 'Auto-Select' to let the AI find the best sheet.",
 
107
  return list(self.excel_data.keys())
108
 
109
  def query_data(self, query: str, target_sheet: str = "Auto-Select") -> Dict[str, Any]:
110
+ """Processes a query, either against a specific sheet or by auto-selecting the most relevant ones."""
111
  results = {'query': query, 'relevant_sheets': [], 'sheet_results': {}, 'summary': ''}
112
 
113
  try:
 
126
  if sheet_name not in self.excel_data: continue
127
 
128
  df = self.excel_data[sheet_name]
129
+ # --- THIS IS THE FIXED LINE ---
130
+ pandas_agent = create_pandas_dataframe_agent(
131
+ self.llm,
132
+ df,
133
+ agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
134
+ verbose=True,
135
+ allow_dangerous_code=True
136
+ )
137
  response = pandas_agent.invoke(query)
138
  results['sheet_results'][sheet_name] = {'response': response['output']}
139
 
 
155
  # --- Gradio Interface ---
156
 
157
  def process_file(api_key, file_obj):
158
+ """Also returns the list of sheet names to populate the dropdown."""
159
  if not api_key: raise gr.Error("OpenAI API Key is required.")
160
  if file_obj is None: raise gr.Error("Please upload an Excel file.")
161
  try:
 
175
  raise gr.Error(f"Failed to process file: {e}")
176
 
177
  def generate_response(query, selected_sheet, system_state):
178
+ """Passes the selected sheet to the query function."""
179
  if not query: raise gr.Error("Please enter a query.")
180
  if system_state is None: raise gr.Error("File not loaded. Please upload and load a file first.")
181
 
 
204
 
205
  with gr.Column(scale=2):
206
  gr.Markdown("### 2. Ask a Question")
 
207
  sheet_selector = gr.Dropdown(
208
  label="Select a sheet to query",
209
  info="Choose 'Auto-Select' to let the AI find the best sheet.",