sanjaystarc commited on
Commit
6dd3d3c
·
verified ·
1 Parent(s): c1a10eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -40
app.py CHANGED
@@ -47,60 +47,48 @@ def main():
47
  st.info(f"Dataset contains {df.shape[0]} rows and {df.shape[1]} columns.")
48
 
49
  # --- 3. AGENT CONFIGURATION ---
50
- query = st.text_area("What analysis would you like to perform?", placeholder="e.g., 'Plot the distribution of prices.'")
 
51
 
52
  if st.button("Run Agent") and query:
53
- # Initialize LLM with max_retries at the model level
54
  llm = ChatGoogleGenerativeAI(
55
- model="gemini-2.5-flash-preview-09-2025",
56
  google_api_key=GEMINI_API_KEY,
57
  temperature=0,
58
- max_retries=6,
59
  )
60
 
61
- # Updated Prefix: We manually include the DF context here since we are using a custom suffix
62
- # This satisfies the requirement to have DF info without triggering the ValueError
63
  df_context = f"The dataframe 'df' has the following columns: {', '.join(df.columns)}"
64
 
65
  custom_prefix = f"""
66
  You are working with a pandas dataframe in Python. The name of the dataframe is `df`.
67
  {df_context}
68
-
69
- You should use the tools below to answer the question posed of you.
70
-
71
- STRICT FORMATTING RULES:
72
  1. NEVER provide a 'Final Answer' and an 'Action' in the same response.
73
  2. If you need to run code, provide ONLY the 'Action' and 'Action Input'.
74
- 3. Your 'Action Input' must be valid Python code and ONLY Python code. No markdown backticks.
75
- 4. Before creating a visualization for filtered data, always check if the filtered dataframe is empty.
76
- 5. For plots, use `plt.figure()` and end with `st.pyplot(plt.gcf())`.
77
- 6. The visual and code must be clear and beautiful.
78
- 7. Avoide over writing .
79
  """
80
 
81
- # The suffix reinforces the format right before the model generates text
82
- custom_suffix = """
83
- Begin!
84
- Question: {input}
85
- Thought: {agent_scratchpad}
86
- """
87
-
88
- # Create the Pandas Agent
89
- # FIX: Ensure include_df_in_prompt is False when using a custom suffix
90
- # This prevents the ValueError: "If suffix is specified, include_df_in_prompt should not be."
91
  agent = create_pandas_dataframe_agent(
92
- llm,
93
- df,
94
- verbose=True,
95
- agent_type="zero-shot-react", # <-- FIXED
96
- allow_dangerous_code=True,
97
- prefix=custom_prefix,
98
- suffix=custom_suffix,
99
- include_df_in_prompt=False, # <-- now allowed
100
- handle_parsing_errors=True,
101
- agent_executor_kwargs={
102
- "handle_parsing_errors": True
103
- }
104
  )
105
 
106
  # --- 4. EXECUTION ---
@@ -126,14 +114,15 @@ def main():
126
  time.sleep(wait_time)
127
  continue
128
  else:
129
- st.error("Rate limit exceeded consistently. Please try again in a minute.")
130
  else:
131
- st.error(f"Agent encountered a parsing or execution error.")
132
  with st.expander("Show Technical Error"):
133
  st.code(str(e))
134
  break
 
135
  else:
136
  st.info("👆 Upload a CSV to begin.")
137
 
138
  if __name__ == "__main__":
139
- main()
 
47
  st.info(f"Dataset contains {df.shape[0]} rows and {df.shape[1]} columns.")
48
 
49
  # --- 3. AGENT CONFIGURATION ---
50
+ query = st.text_area("What analysis would you like to perform?",
51
+ placeholder="e.g., 'Plot the distribution of prices.'")
52
 
53
  if st.button("Run Agent") and query:
54
+ # Initialize LLM
55
  llm = ChatGoogleGenerativeAI(
56
+ model="gemini-2.5-flash-preview-09-2025",
57
  google_api_key=GEMINI_API_KEY,
58
  temperature=0,
59
+ max_retries=6
60
  )
61
 
62
+ # Add dataframe context manually in prefix
 
63
  df_context = f"The dataframe 'df' has the following columns: {', '.join(df.columns)}"
64
 
65
  custom_prefix = f"""
66
  You are working with a pandas dataframe in Python. The name of the dataframe is `df`.
67
  {df_context}
68
+
69
+ You should use the tools below to answer the question posed.
70
+
71
+ STRICT RULES:
72
  1. NEVER provide a 'Final Answer' and an 'Action' in the same response.
73
  2. If you need to run code, provide ONLY the 'Action' and 'Action Input'.
74
+ 3. 'Action Input' must be valid Python code ONLY no markdown.
75
+ 4. Before plotting filtered data, ALWAYS check `df.empty`.
76
+ 5. Use `plt.figure()` before every plot, and finish with `st.pyplot(plt.gcf())`.
77
+ 6. Code must be clean and readable.
78
+ 7. Avoid overwriting variables unless necessary.
79
  """
80
 
81
+ # IMPORTANT: No suffix avoids ValueError
 
 
 
 
 
 
 
 
 
82
  agent = create_pandas_dataframe_agent(
83
+ llm,
84
+ df,
85
+ verbose=True,
86
+ agent_type="zero-shot-react-description", # required agent type
87
+ allow_dangerous_code=True,
88
+ prefix=custom_prefix,
89
+ include_df_in_prompt=False,
90
+ handle_parsing_errors=True,
91
+ agent_executor_kwargs={"handle_parsing_errors": True}
 
 
 
92
  )
93
 
94
  # --- 4. EXECUTION ---
 
114
  time.sleep(wait_time)
115
  continue
116
  else:
117
+ st.error("Rate limit exceeded consistently. Try again later.")
118
  else:
119
+ st.error("Agent encountered a parsing or execution error.")
120
  with st.expander("Show Technical Error"):
121
  st.code(str(e))
122
  break
123
+
124
  else:
125
  st.info("👆 Upload a CSV to begin.")
126
 
127
  if __name__ == "__main__":
128
+ main()