Spaces:
Running
Running
arithescientist
commited on
Commit
•
c6acd31
1
Parent(s):
d9d0b05
Update app.py
Browse files
app.py
CHANGED
@@ -56,8 +56,45 @@ sql_chain = SQLDatabaseChain(llm=llm, database=engine, verbose=True)
|
|
56 |
|
57 |
# Step 4: Define the callback function
|
58 |
def process_input():
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
# Step 5: Display conversation history
|
63 |
for message in st.session_state.history:
|
|
|
56 |
|
57 |
# Step 4: Define the callback function
|
58 |
def process_input():
|
59 |
+
user_prompt = st.session_state['user_input']
|
60 |
+
|
61 |
+
if user_prompt:
|
62 |
+
try:
|
63 |
+
# Append user message to history
|
64 |
+
st.session_state.history.append({"role": "user", "content": user_prompt})
|
65 |
+
|
66 |
+
# Use the SQLDatabaseChain to get the response
|
67 |
+
with st.spinner("Processing..."):
|
68 |
+
response = sql_chain.run(user_prompt)
|
69 |
+
|
70 |
+
# Append the assistant's response to the history
|
71 |
+
st.session_state.history.append({"role": "assistant", "content": response})
|
72 |
+
|
73 |
+
# Generate insights based on the response
|
74 |
+
insights_template = """
|
75 |
+
You are an expert data analyst. Based on the user's question and the response provided below, generate a concise analysis that includes key data insights and actionable recommendations. Limit the response to a maximum of 150 words.
|
76 |
+
|
77 |
+
User's Question: {question}
|
78 |
+
|
79 |
+
Response:
|
80 |
+
{response}
|
81 |
+
|
82 |
+
Concise Analysis:
|
83 |
+
"""
|
84 |
+
insights_prompt = PromptTemplate(template=insights_template, input_variables=['question', 'response'])
|
85 |
+
insights_chain = LLMChain(llm=llm, prompt=insights_prompt)
|
86 |
+
|
87 |
+
insights = insights_chain.run({'question': user_prompt, 'response': response})
|
88 |
+
|
89 |
+
# Append the assistant's insights to the history
|
90 |
+
st.session_state.history.append({"role": "assistant", "content": insights})
|
91 |
+
except Exception as e:
|
92 |
+
logging.error(f"An error occurred: {e}")
|
93 |
+
assistant_response = f"Error: {e}"
|
94 |
+
st.session_state.history.append({"role": "assistant", "content": assistant_response})
|
95 |
+
|
96 |
+
# Reset user input
|
97 |
+
st.session_state['user_input'] = ''
|
98 |
|
99 |
# Step 5: Display conversation history
|
100 |
for message in st.session_state.history:
|