WaysAhead Global commited on
Commit
7fd3fa8
Β·
verified Β·
1 Parent(s): 7e72b57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -164,11 +164,11 @@ You are an advanced AI specializing in retail analytics. You help users:
164
  1. Find similar stores based on category and sales performance.
165
  2. Generate insights from sales data.
166
  3. Create visualizations like bar charts, line charts, and trends.
167
- 4. If a visualization request is made, generate Python code to create the chart.
168
  """
169
 
170
  agent = create_pandas_dataframe_agent(
171
- ChatOpenAI(model="gpt-3.5-turbo", api_key=openai_api_key),
172
  df_kpi,
173
  verbose=True,
174
  agent_type="openai-tools",
@@ -187,17 +187,18 @@ if user_query:
187
  if "import matplotlib.pyplot" in response:
188
  st.write("πŸ›  AI is generating a chart based on your request...")
189
 
190
- # βœ… Extract and Execute Python Code Safely
 
 
 
191
  try:
192
- exec_globals = {}
193
- exec(response, {}, exec_globals) # Execute AI-generated code
194
 
195
  # βœ… Capture Matplotlib Figure
196
  fig = plt.gcf()
197
  buf = BytesIO()
198
  fig.savefig(buf, format="png")
199
  buf.seek(0)
200
- image_base64 = base64.b64encode(buf.read()).decode()
201
 
202
  # βœ… Display Image in Streamlit
203
  st.image(buf, caption="πŸ“Š AI-Generated Chart", use_column_width=True)
@@ -212,6 +213,4 @@ if user_query:
212
  st.error(f"⚠️ Error: {e}")
213
 
214
  st.markdown("---")
215
- st.markdown("πŸš€ **Powered by WaysAhead Global AI Engine**")
216
-
217
-
 
164
  1. Find similar stores based on category and sales performance.
165
  2. Generate insights from sales data.
166
  3. Create visualizations like bar charts, line charts, and trends.
167
+ 4. If a visualization request is made like a heatmap or chart, give proper and full Python code to create the chart.
168
  """
169
 
170
  agent = create_pandas_dataframe_agent(
171
+ ChatOpenAI(model="gpt-4", api_key=openai_api_key),
172
  df_kpi,
173
  verbose=True,
174
  agent_type="openai-tools",
 
187
  if "import matplotlib.pyplot" in response:
188
  st.write("πŸ›  AI is generating a chart based on your request...")
189
 
190
+ # βœ… Create a dictionary to store execution variables
191
+ exec_globals = {"df": df_kpi}
192
+
193
+ # βœ… Execute the AI-generated code
194
  try:
195
+ exec(response, {}, exec_globals)
 
196
 
197
  # βœ… Capture Matplotlib Figure
198
  fig = plt.gcf()
199
  buf = BytesIO()
200
  fig.savefig(buf, format="png")
201
  buf.seek(0)
 
202
 
203
  # βœ… Display Image in Streamlit
204
  st.image(buf, caption="πŸ“Š AI-Generated Chart", use_column_width=True)
 
213
  st.error(f"⚠️ Error: {e}")
214
 
215
  st.markdown("---")
216
+ st.markdown("πŸš€ **Powered by WaysAhead Global AI Engine**")