masturakinan commited on
Commit
a7240fe
Β·
1 Parent(s): 5d72b12

handle candidate queries

Browse files
Files changed (1) hide show
  1. app.py +48 -38
app.py CHANGED
@@ -99,45 +99,55 @@ if prompt:
99
  st.markdown(prompt)
100
 
101
  response = analyze_sales_data(sales_file, prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
- dummy_response = response['result'][0]
104
-
105
- try:
106
- # Process input
107
- # values = list(map(int, prompt.strip().split(',')))
108
- # labels = [f"Item {i+1}" for i in range(len(values))]
109
- values = [item['y_axis'] for item in dummy_response['visualization']['data']]
110
- labels = [item['x_axis'] for item in dummy_response['visualization']['data']]
111
-
112
- # Create chart
113
- fig, ax = plt.subplots()
114
- if dummy_response['visualization']['type'] == 'line_chart':
115
- ax.plot(labels, values, marker='o', color="lightcoral")
116
- else:
117
- ax.bar(labels, values, color="lightcoral")
118
- plt.xticks(rotation=45, ha='right')
119
- ax.set_title(dummy_response['visualization']['config']['title'])
120
- ax.set_ylabel(dummy_response['visualization']['config']['y_axis'])
121
- ax.set_xlabel(dummy_response['visualization']['config']['x_axis'])
122
-
123
- # Add value labels on top of each bar
124
- for i, v in enumerate(values):
125
- ax.text(i, v, f'{v:,.2f}', ha='center', va='bottom')
126
-
127
- # Generate analysis message
128
- analysis = "πŸ“Š Insights:\n"
129
- for insight in dummy_response['insights']:
130
- analysis += f"- {insight}\n"
131
-
132
- # Add bot replies
133
- st.session_state.messages.append({"role": "assistant", "content": fig})
134
- st.session_state.messages.append({"role": "assistant", "content": analysis})
135
- with st.chat_message("assistant"):
136
- st.pyplot(fig)
137
- st.markdown(analysis)
138
-
139
- except Exception as e:
140
- err_msg = f"❌ Error: {str(e)} β€” please enter only comma-separated numbers."
141
  st.session_state.messages.append({"role": "assistant", "content": err_msg})
142
  with st.chat_message("assistant"):
143
  st.markdown(err_msg)
 
99
  st.markdown(prompt)
100
 
101
  response = analyze_sales_data(sales_file, prompt)
102
+ print("==========>: ", response)
103
+
104
+ if response['status'] == 'success':
105
+ dummy_response = response['result'][0]
106
+
107
+ try:
108
+ # Process input
109
+ # values = list(map(int, prompt.strip().split(',')))
110
+ # labels = [f"Item {i+1}" for i in range(len(values))]
111
+ values = [item['y_axis'] for item in dummy_response['visualization']['data']]
112
+ labels = [item['x_axis'] for item in dummy_response['visualization']['data']]
113
+
114
+ # Create chart
115
+ fig, ax = plt.subplots()
116
+ if dummy_response['visualization']['type'] == 'line_chart':
117
+ ax.plot(labels, values, marker='o', color="lightcoral")
118
+ else:
119
+ ax.bar(labels, values, color="lightcoral")
120
+ plt.xticks(rotation=45, ha='right')
121
+ ax.set_title(dummy_response['visualization']['config']['title'])
122
+ ax.set_ylabel(dummy_response['visualization']['config']['y_axis'])
123
+ ax.set_xlabel(dummy_response['visualization']['config']['x_axis'])
124
+
125
+ # Add value labels on top of each bar
126
+ for i, v in enumerate(values):
127
+ ax.text(i, v, f'{v:,.2f}', ha='center', va='bottom')
128
+
129
+ # Generate analysis message
130
+ analysis = "πŸ“Š Insights:\n"
131
+ for insight in dummy_response['insights']:
132
+ analysis += f"- {insight}\n"
133
+
134
+ # Add bot replies
135
+ st.session_state.messages.append({"role": "assistant", "content": fig})
136
+ st.session_state.messages.append({"role": "assistant", "content": analysis})
137
+ with st.chat_message("assistant"):
138
+ st.pyplot(fig)
139
+ st.markdown(analysis)
140
+
141
+ except Exception as e:
142
+ err_msg = f"❌ Error: {str(e)} β€” please enter only comma-separated numbers."
143
+ st.session_state.messages.append({"role": "assistant", "content": err_msg})
144
+ with st.chat_message("assistant"):
145
+ st.markdown(err_msg)
146
+ else:
147
+ err_msg = "❌ Error: Unable to process the request. Here are some example queries you can try:\n\n"
148
+ for candidate in response['candidate_questions']:
149
+ err_msg += f"- {candidate}\n"
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  st.session_state.messages.append({"role": "assistant", "content": err_msg})
152
  with st.chat_message("assistant"):
153
  st.markdown(err_msg)