Spaces:
Paused
Paused
WaysAhead Global commited on
Update app.py
Browse files
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
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
| 166 |
"""
|
| 167 |
|
| 168 |
agent = create_pandas_dataframe_agent(
|
| 169 |
-
ChatOpenAI(model="gpt-
|
| 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
|
| 184 |
-
if "
|
| 185 |
-
|
| 186 |
-
base64_img = response.split('base64,')[1].split('"')[0]
|
| 187 |
|
| 188 |
-
#
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|