Anne31415 commited on
Commit
91dfcdf
·
verified ·
1 Parent(s): 8f10f34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -17,7 +17,7 @@ uploaded_file = st.sidebar.file_uploader("Choose a CSV file", type="csv")
17
  if uploaded_file is not None:
18
  st.session_state["df"] = pd.read_csv(uploaded_file)
19
 
20
- # Example function to generate a response from OpenAI's chat model
21
  def ask_openai(prompt):
22
  try:
23
  response = openai.ChatCompletion.create(
@@ -27,17 +27,18 @@ def ask_openai(prompt):
27
  )
28
  return response.choices[0].message["content"]
29
  except Exception as e:
30
- print(f"Error in generating response: {e}")
31
- return "Sorry, I couldn't generate a response. Please try again."
32
-
33
- # Example chat interaction in Streamlit
34
- if "chat_history" not in st.session_state:
35
- st.session_state["chat_history"] = []
36
-
37
- if prompt := st.text_input("Ask me anything about the data:"):
38
- st.session_state["chat_history"].append({"role": "user", "content": prompt})
39
- response = ask_openai(prompt)
40
- st.session_state["chat_history"].append({"role": "assistant", "content": response})
41
-
42
- for chat in st.session_state["chat_history"]:
43
- st_message(chat["content"], is_user=True if chat["role"] == "user" else False)
 
 
17
  if uploaded_file is not None:
18
  st.session_state["df"] = pd.read_csv(uploaded_file)
19
 
20
+ # Function to generate a response from OpenAI's chat model
21
  def ask_openai(prompt):
22
  try:
23
  response = openai.ChatCompletion.create(
 
27
  )
28
  return response.choices[0].message["content"]
29
  except Exception as e:
30
+ st.error(f"Error in generating response: {e}")
31
+ return "I encountered an error. Please try again."
32
+
33
+ # Input for new messages
34
+ user_input = st.text_input("Ask me anything:", key="chat_input")
35
+
36
+ # Process input on submission
37
+ if user_input:
38
+ with st.chat_message("user"):
39
+ st.write(user_input)
40
+
41
+ # Generate and display response
42
+ ai_response = ask_openai(user_input)
43
+ with st.chat_message("assistant"):
44
+ st.write(ai_response)