CosmickVisions commited on
Commit
b36a48a
·
verified ·
1 Parent(s): ed852a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import pandas as pd
3
  import numpy as np
4
  import plotly.express as px
 
5
  from ydata_profiling import ProfileReport
6
  from streamlit_pandas_profiling import st_profile_report
7
  import os
@@ -172,7 +173,7 @@ custom_html = """
172
  </script>
173
  """
174
 
175
- # Helper Functions (mostly unchanged)
176
  def update_cleaned_data(df):
177
  st.session_state.cleaned_data = df
178
  if 'data_versions' not in st.session_state:
@@ -303,8 +304,8 @@ def main():
303
  # Hidden Inputs for JS Interaction
304
  if 'app_mode' not in st.session_state:
305
  st.session_state.app_mode = "Data Upload"
306
- app_mode = st.markdown('<input id="app-mode" type="hidden" value="Data Upload">', unsafe_allow_html=True)
307
- chat_trigger = st.markdown('<input id="chat-trigger" type="hidden">', unsafe_allow_html=True)
308
 
309
  # Sidebar
310
  with st.sidebar:
@@ -392,13 +393,15 @@ def main():
392
  st.session_state.last_plot = {"type": "Histogram", "x": x_axis, "data": df[[x_axis]].to_json()}
393
  st.plotly_chart(fig)
394
 
395
- # Chatbot Logic
396
- chat_input = st.session_state.get('chat_input', '')
 
 
397
  if chat_input:
398
  st.session_state.chat_history.append({"role": "user", "content": chat_input})
399
  df = st.session_state.cleaned_data if 'cleaned_data' in st.session_state else pd.DataFrame()
400
  new_df, result, plot_info = parse_command(chat_input, df)
401
- if isinstance(result, px.scatter._chart_types.Scatter) or isinstance(result, px.histogram._chart_types.Histogram):
402
  st.plotly_chart(result)
403
  st.session_state.last_plot = plot_info
404
  st.session_state.vector_store = update_vector_store_with_plot(extract_plot_data(plot_info, new_df), st.session_state.vector_store)
@@ -417,7 +420,7 @@ def main():
417
  document.dispatchEvent(new CustomEvent('bot_response', {{ detail: {json.dumps(response)} }}));
418
  </script>
419
  """, unsafe_allow_html=True)
420
- st.session_state.chat_input = st.text_input("Chat with AI", key="chat_input", on_change=lambda: None)
421
 
422
  if __name__ == "__main__":
423
  main()
 
2
  import pandas as pd
3
  import numpy as np
4
  import plotly.express as px
5
+ from plotly.graph_objects import Figure
6
  from ydata_profiling import ProfileReport
7
  from streamlit_pandas_profiling import st_profile_report
8
  import os
 
173
  </script>
174
  """
175
 
176
+ # Helper Functions (unchanged)
177
  def update_cleaned_data(df):
178
  st.session_state.cleaned_data = df
179
  if 'data_versions' not in st.session_state:
 
304
  # Hidden Inputs for JS Interaction
305
  if 'app_mode' not in st.session_state:
306
  st.session_state.app_mode = "Data Upload"
307
+ st.markdown(f'<input id="app-mode" type="hidden" value="{st.session_state.app_mode}">', unsafe_allow_html=True)
308
+ st.markdown('<input id="chat-trigger" type="hidden">', unsafe_allow_html=True)
309
 
310
  # Sidebar
311
  with st.sidebar:
 
393
  st.session_state.last_plot = {"type": "Histogram", "x": x_axis, "data": df[[x_axis]].to_json()}
394
  st.plotly_chart(fig)
395
 
396
+ # Chatbot Logic (Moved to Bottom, Fixed Session State Issue)
397
+ if "chat_trigger" not in st.session_state:
398
+ st.session_state.chat_trigger = ""
399
+ chat_input = st.session_state.chat_trigger
400
  if chat_input:
401
  st.session_state.chat_history.append({"role": "user", "content": chat_input})
402
  df = st.session_state.cleaned_data if 'cleaned_data' in st.session_state else pd.DataFrame()
403
  new_df, result, plot_info = parse_command(chat_input, df)
404
+ if isinstance(result, Figure):
405
  st.plotly_chart(result)
406
  st.session_state.last_plot = plot_info
407
  st.session_state.vector_store = update_vector_store_with_plot(extract_plot_data(plot_info, new_df), st.session_state.vector_store)
 
420
  document.dispatchEvent(new CustomEvent('bot_response', {{ detail: {json.dumps(response)} }}));
421
  </script>
422
  """, unsafe_allow_html=True)
423
+ st.session_state.chat_trigger = "" # Reset after processing
424
 
425
  if __name__ == "__main__":
426
  main()