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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -12
app.py CHANGED
@@ -160,13 +160,15 @@ if selected_mall:
160
  st.subheader("πŸ’¬ Chat with WaysAhead AI Engine")
161
 
162
  system_prompt = """
163
- You are an advanced AI engine that understands the retail industry in the UAE.
164
- You help users find the best store recommendations, analyze competitors, and generate visualizations.
165
- If the user requests a chart, generate it and return the image properly formatted for display.
 
 
166
  """
167
 
168
  agent = create_pandas_dataframe_agent(
169
- ChatOpenAI(model="gpt-4", api_key=openai_api_key),
170
  df_kpi,
171
  verbose=True,
172
  agent_type="openai-tools",
@@ -175,21 +177,34 @@ agent = create_pandas_dataframe_agent(
175
  system_prompt=system_prompt
176
  )
177
 
 
178
  user_query = st.text_input("πŸ” Ask AI for similar stores or analytics:")
179
  if user_query:
180
  try:
181
  response = agent.run(user_query)
182
 
183
- # βœ… Detect if the response is an image
184
- if "<figure>" in response and "<img" in response and "base64" in response:
185
- # Extract Base64 Image Data
186
- base64_img = response.split('base64,')[1].split('"')[0]
187
 
188
- # Decode and Display Image
189
- image_bytes = BytesIO(base64.b64decode(base64_img))
190
- image = plt.imread(image_bytes, format="png")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
- st.image(image, caption="Generated Visualization", use_column_width=True)
193
  else:
194
  st.markdown(f"πŸ€– **AI Response:** {response}")
195
 
 
160
  st.subheader("πŸ’¬ Chat with WaysAhead AI Engine")
161
 
162
  system_prompt = """
163
+ 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",
 
177
  system_prompt=system_prompt
178
  )
179
 
180
+ # βœ… User Input
181
  user_query = st.text_input("πŸ” Ask AI for similar stores or analytics:")
182
  if user_query:
183
  try:
184
  response = agent.run(user_query)
185
 
186
+ # βœ… Detect if AI Generated Python Code for a Chart
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)
204
+
205
+ except Exception as e:
206
+ st.error(f"⚠️ Error generating chart: {e}")
207
 
 
208
  else:
209
  st.markdown(f"πŸ€– **AI Response:** {response}")
210