Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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?",
|
|
|
|
| 51 |
|
| 52 |
if st.button("Run Agent") and query:
|
| 53 |
-
# Initialize LLM
|
| 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 |
-
#
|
| 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
|
| 70 |
-
|
| 71 |
-
STRICT
|
| 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.
|
| 75 |
-
4. Before
|
| 76 |
-
5.
|
| 77 |
-
6.
|
| 78 |
-
7.
|
| 79 |
"""
|
| 80 |
|
| 81 |
-
#
|
| 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 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 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.
|
| 130 |
else:
|
| 131 |
-
st.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()
|