Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,10 @@ from dotenv import load_dotenv
|
|
9 |
import anthropic
|
10 |
import ast
|
11 |
import re
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Load environment variables
|
14 |
load_dotenv()
|
@@ -58,37 +62,27 @@ st.markdown(
|
|
58 |
|
59 |
st.title("Excel Q&A Chatbot π")
|
60 |
|
61 |
-
#
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
inputs = tokenizer(query, return_tensors="pt").to("cuda")
|
71 |
-
output = model.generate(**inputs)
|
72 |
-
return tokenizer.decode(output[0])
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
return response.content[0]["text"]
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
def ask_gpt(query):
|
87 |
-
response = client.chat.completions.create(
|
88 |
-
model="gpt-3.5-turbo",
|
89 |
-
messages=[{"role": "user", "content": query}]
|
90 |
-
)
|
91 |
-
return response.choices[0].message.content
|
92 |
|
93 |
# File Upload with validation
|
94 |
uploaded_file = st.file_uploader("Upload a file", type=["csv", "xlsx", "xls", "json", "tsv"])
|
@@ -130,47 +124,10 @@ if uploaded_file is not None:
|
|
130 |
|
131 |
if st.button("Submit Query"):
|
132 |
if query:
|
133 |
-
# Interpret the query using selected LLM
|
134 |
-
if model_choice == "Mistral-7B":
|
135 |
-
parsed_query = ask_mistral(f"Convert this question into a structured data operation: {query}")
|
136 |
-
elif model_choice == "Claude 3 Haiku":
|
137 |
-
parsed_query = ask_claude(f"Convert this question into a structured data operation: {query}")
|
138 |
-
else:
|
139 |
-
parsed_query = ask_gpt(f"Convert this question into a structured data operation: {query}")
|
140 |
-
|
141 |
-
# Validate and clean query
|
142 |
-
#parsed_query = re.sub(r"[^a-zA-Z0-9_()\[\]"'., ]", "", parsed_query.strip())
|
143 |
-
parsed_query = re.sub(r"[^a-zA-Z0-9_()\[\]\'., ]", "", parsed_query.strip())
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
st.write(f"Parsed Query: `{parsed_query}`")
|
148 |
-
|
149 |
-
# Predefined Safe Execution Methods
|
150 |
-
SAFE_OPERATIONS = {
|
151 |
-
"sum": lambda col: df[col].sum(),
|
152 |
-
"mean": lambda col: df[col].mean(),
|
153 |
-
"max": lambda col: df[col].max(),
|
154 |
-
"groupby_sum": lambda col, group_by: df.groupby(group_by)[col].sum()
|
155 |
-
}
|
156 |
-
|
157 |
-
# Safe Execution
|
158 |
try:
|
159 |
-
exec_result =
|
160 |
st.write("### Result:")
|
161 |
-
st.write(exec_result
|
162 |
-
|
163 |
-
# If numerical data, show a visualization dynamically
|
164 |
-
if isinstance(exec_result, pd.Series):
|
165 |
-
fig, ax = plt.subplots()
|
166 |
-
if exec_result.dtype in ["int64", "float64"]:
|
167 |
-
exec_result.plot(kind="bar", ax=ax)
|
168 |
-
elif exec_result.dtype == "object":
|
169 |
-
exec_result.value_counts().plot(kind="bar", ax=ax)
|
170 |
-
st.pyplot(fig)
|
171 |
-
|
172 |
-
except SyntaxError as e:
|
173 |
-
st.error(f"Syntax Error in parsed query: {str(e)}")
|
174 |
except Exception as e:
|
175 |
st.error(f"Error executing query: {str(e)}")
|
176 |
|
|
|
9 |
import anthropic
|
10 |
import ast
|
11 |
import re
|
12 |
+
from langchain.agents import AgentType, initialize_agent
|
13 |
+
from langchain.tools import Tool
|
14 |
+
from langchain.chat_models import ChatOpenAI
|
15 |
+
from langchain.memory import ConversationBufferMemory
|
16 |
|
17 |
# Load environment variables
|
18 |
load_dotenv()
|
|
|
62 |
|
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=lambda q: eval(q, {"df": df, "pd": pd}),
|
72 |
+
description="Executes Pandas-based queries on uploaded data"
|
73 |
+
)
|
|
|
|
|
|
|
74 |
|
75 |
+
agent = initialize_agent(
|
76 |
+
tools=[tool],
|
77 |
+
llm=ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0),
|
78 |
+
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
|
79 |
+
memory=memory,
|
80 |
+
verbose=True
|
81 |
+
)
|
82 |
+
return agent.run(query)
|
|
|
83 |
|
84 |
+
# Model Selection
|
85 |
+
model_choice = st.selectbox("Select LLM Model", ["OpenAI GPT-3.5", "Claude 3 Haiku", "Mistral-7B"])
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
# File Upload with validation
|
88 |
uploaded_file = st.file_uploader("Upload a file", type=["csv", "xlsx", "xls", "json", "tsv"])
|
|
|
124 |
|
125 |
if st.button("Submit Query"):
|
126 |
if query:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
try:
|
128 |
+
exec_result = execute_query(query)
|
129 |
st.write("### Result:")
|
130 |
+
st.write(exec_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
except Exception as e:
|
132 |
st.error(f"Error executing query: {str(e)}")
|
133 |
|