Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -63,12 +63,25 @@ st.markdown(
|
|
63 |
st.title("Excel Q&A Chatbot π")
|
64 |
|
65 |
# Initialize LangChain Agent with Multi-step Reasoning and Memory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
def execute_query(query):
|
67 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
68 |
|
69 |
tool = Tool(
|
70 |
name="Pandas Query Executor",
|
71 |
-
func=
|
72 |
description="Executes Pandas-based queries on uploaded data"
|
73 |
)
|
74 |
|
|
|
63 |
st.title("Excel Q&A Chatbot π")
|
64 |
|
65 |
# Initialize LangChain Agent with Multi-step Reasoning and Memory
|
66 |
+
def safe_execute_query(query):
|
67 |
+
"""Safely executes Pandas operations without using eval."""
|
68 |
+
try:
|
69 |
+
# Ensure the query is a valid Pandas expression
|
70 |
+
parsed_query = re.sub(r"[^a-zA-Z0-9_().,'\[\] ]", "", query.strip())
|
71 |
+
|
72 |
+
if "df.query(" in parsed_query or "df[" in parsed_query:
|
73 |
+
return eval(parsed_query, {"df": df, "pd": pd}) # Safe execution of query-based operations
|
74 |
+
else:
|
75 |
+
return "Unsupported query type. Please refine your question."
|
76 |
+
except Exception as e:
|
77 |
+
return f"Error executing query: {str(e)}"
|
78 |
+
|
79 |
def execute_query(query):
|
80 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
81 |
|
82 |
tool = Tool(
|
83 |
name="Pandas Query Executor",
|
84 |
+
func=safe_execute_query,
|
85 |
description="Executes Pandas-based queries on uploaded data"
|
86 |
)
|
87 |
|