Anne31415 commited on
Commit
56bab14
·
verified ·
1 Parent(s): 6fb0c3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -5,41 +5,35 @@ from streamlit_chat import message
5
  import pandas as pd
6
  from streamlit_chat import message as st_message # Ensure streamlit_chat is installed
7
 
8
- # Load data function remains the same
9
- def load_data(path):
10
- return pd.read_csv(path)
11
 
12
- # Assuming you've set your OpenAI API key in the environment variables
13
- openai.api_key = os.getenv("OPENAI_API_KEY")
14
 
15
-
16
- # File uploader and data loading logic can remain the same
17
- uploaded_file = st.sidebar.file_uploader("Choose a CSV file", type="csv")
18
- if uploaded_file is not None:
19
- st.session_state["df"] = pd.read_csv(uploaded_file)
20
-
21
- # Function to generate a response from OpenAI's chat model
22
  def ask_openai(prompt):
23
  try:
24
- response = openai.ChatCompletion.create(
25
- model="gpt-3.5-turbo", # Adjust model as needed
26
- messages=[{"role": "system", "content": "You are a helpful assistant."},
27
- {"role": "user", "content": prompt}],
 
 
 
28
  )
29
- return response.choices[0].message["content"]
30
  except Exception as e:
31
  st.error(f"Error in generating response: {e}")
32
  return "I encountered an error. Please try again."
33
 
34
- # Input for new messages
35
  user_input = st.text_input("Ask me anything:", key="chat_input")
36
 
37
- # Process input on submission
38
  if user_input:
 
39
  with st.chat_message("user"):
40
  st.write(user_input)
41
 
42
- # Generate and display response
43
  ai_response = ask_openai(user_input)
44
  with st.chat_message("assistant"):
45
- st.write(ai_response)
 
5
  import pandas as pd
6
  from streamlit_chat import message as st_message # Ensure streamlit_chat is installed
7
 
 
 
 
8
 
9
+ # Ensure you have the OpenAI API key set in your environment variables
10
+ openai.api_key = st.secrets["OPENAI_API_KEY"]
11
 
12
+ # Function to generate a response from OpenAI's GPT model using the updated API
 
 
 
 
 
 
13
  def ask_openai(prompt):
14
  try:
15
+ response = openai.Completion.create(
16
+ model="text-davinci-003", # Example model, adjust as needed
17
+ prompt=prompt,
18
+ temperature=0.7,
19
+ max_tokens=150,
20
+ n=1,
21
+ stop=None,
22
  )
23
+ return response.choices[0].text.strip()
24
  except Exception as e:
25
  st.error(f"Error in generating response: {e}")
26
  return "I encountered an error. Please try again."
27
 
28
+ # UI for input and displaying the chat
29
  user_input = st.text_input("Ask me anything:", key="chat_input")
30
 
 
31
  if user_input:
32
+ # Display user input
33
  with st.chat_message("user"):
34
  st.write(user_input)
35
 
36
+ # Generate and display AI response
37
  ai_response = ask_openai(user_input)
38
  with st.chat_message("assistant"):
39
+ st.write(ai_response)