Spaces:
Running
Running
arithescientist
commited on
Commit
•
580d1d7
1
Parent(s):
91884db
Update app.py
Browse files
app.py
CHANGED
@@ -42,6 +42,10 @@ else:
|
|
42 |
st.write(f"Data Preview ({csv_file.name}):")
|
43 |
st.dataframe(data.head())
|
44 |
|
|
|
|
|
|
|
|
|
45 |
# Step 2: Load CSV data into SQLite database
|
46 |
db_file = 'my_database.db'
|
47 |
conn = sqlite3.connect(db_file)
|
@@ -63,18 +67,34 @@ agent_executor = create_sql_agent(
|
|
63 |
agent_executor_kwargs={"return_intermediate_steps": True}
|
64 |
)
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
# Step 4: Define the callback function
|
67 |
def process_input():
|
68 |
-
user_prompt = st.session_state
|
69 |
|
70 |
if user_prompt:
|
71 |
try:
|
72 |
# Append user message to history
|
73 |
st.session_state.history.append({"role": "user", "content": user_prompt})
|
74 |
|
75 |
-
# Use the agent to get the response
|
76 |
with st.spinner("Processing..."):
|
77 |
-
response = agent_executor(user_prompt)
|
78 |
|
79 |
# Extract the final answer and the data from intermediate steps
|
80 |
final_answer = response['output']
|
@@ -123,16 +143,20 @@ def process_input():
|
|
123 |
st.session_state.history.append({"role": "assistant", "content": insights})
|
124 |
except Exception as e:
|
125 |
logging.error(f"An error occurred: {e}")
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
st.session_state.history.append({"role": "assistant", "content": assistant_response})
|
128 |
|
129 |
# Reset user input
|
130 |
st.session_state['user_input'] = ''
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
# Step 5: Display conversation history
|
|
|
136 |
for message in st.session_state.history:
|
137 |
if message['role'] == 'user':
|
138 |
st.markdown(f"**User:** {message['content']}")
|
|
|
42 |
st.write(f"Data Preview ({csv_file.name}):")
|
43 |
st.dataframe(data.head())
|
44 |
|
45 |
+
# Display column names
|
46 |
+
st.write("**Available Columns:**")
|
47 |
+
st.write(", ".join(data.columns.tolist()))
|
48 |
+
|
49 |
# Step 2: Load CSV data into SQLite database
|
50 |
db_file = 'my_database.db'
|
51 |
conn = sqlite3.connect(db_file)
|
|
|
67 |
agent_executor_kwargs={"return_intermediate_steps": True}
|
68 |
)
|
69 |
|
70 |
+
# Step 3: Sample Questions
|
71 |
+
st.write("**Sample Questions:**")
|
72 |
+
sample_questions = [
|
73 |
+
"Provide an analysis of the data.",
|
74 |
+
"Summarize the data for me.",
|
75 |
+
"Can you offer any recommendations based on the data sets?",
|
76 |
+
"What are the total sales across regions?"
|
77 |
+
]
|
78 |
+
|
79 |
+
def set_sample_question(question):
|
80 |
+
st.session_state['user_input'] = question
|
81 |
+
process_input()
|
82 |
+
|
83 |
+
for question in sample_questions:
|
84 |
+
st.button(question, on_click=set_sample_question, args=(question,))
|
85 |
+
|
86 |
# Step 4: Define the callback function
|
87 |
def process_input():
|
88 |
+
user_prompt = st.session_state.get('user_input', '')
|
89 |
|
90 |
if user_prompt:
|
91 |
try:
|
92 |
# Append user message to history
|
93 |
st.session_state.history.append({"role": "user", "content": user_prompt})
|
94 |
|
95 |
+
# Use the agent to get the response
|
96 |
with st.spinner("Processing..."):
|
97 |
+
response = agent_executor(user_prompt)
|
98 |
|
99 |
# Extract the final answer and the data from intermediate steps
|
100 |
final_answer = response['output']
|
|
|
143 |
st.session_state.history.append({"role": "assistant", "content": insights})
|
144 |
except Exception as e:
|
145 |
logging.error(f"An error occurred: {e}")
|
146 |
+
|
147 |
+
# Check for specific errors related to missing columns
|
148 |
+
if "no such column" in str(e).lower():
|
149 |
+
assistant_response = "Error: One or more columns referenced do not exist in the data."
|
150 |
+
else:
|
151 |
+
assistant_response = f"Error: {e}"
|
152 |
+
|
153 |
st.session_state.history.append({"role": "assistant", "content": assistant_response})
|
154 |
|
155 |
# Reset user input
|
156 |
st.session_state['user_input'] = ''
|
157 |
|
|
|
|
|
|
|
158 |
# Step 5: Display conversation history
|
159 |
+
st.write("## Conversation History")
|
160 |
for message in st.session_state.history:
|
161 |
if message['role'] == 'user':
|
162 |
st.markdown(f"**User:** {message['content']}")
|