exnav29 commited on
Commit
e921d69
·
1 Parent(s): 4f964b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -21
app.py CHANGED
@@ -1,50 +1,67 @@
1
  import os
2
  import openai
3
  import streamlit as st
4
- from streamlit import SessionState
5
 
 
6
  openai.api_key = os.environ["OPENAI_API_KEY"]
7
 
8
- def init_session():
9
- session = SessionState.get(messages=[], counter=0)
10
- if not session.messages:
11
- session.messages = [
12
- {"role": "system", "content": "You are an AI specialized in Real Estate Investing Do not answer anything other than Real Estate-related queries"}
13
- ]
14
- return session
15
 
16
- def chatbot(input, session):
 
 
 
 
 
17
  if input:
18
- session.messages.append({"role": "user", "content": input})
 
 
 
19
  chat = openai.Completion.create(
20
  engine="text-davinci-002",
21
- prompt=f"{session.messages[-2]['content']}\nUser: {session.messages[-1]['content']}\nAI:",
22
  max_tokens=100,
23
  n=1,
24
  stop=None,
25
  temperature=0.2,
26
  )
 
 
27
  reply = chat.choices[0].text
28
- session.messages.append({"role": "assistant", "content": reply})
29
- session.counter += 1
30
- if session.counter >= 3:
 
 
 
31
  st.experimental_rerun()
32
- st.write(
33
- "Thank you for using our chatbot! If you find our service helpful, please consider making a donation to support us: <a href='https://www.paypal.me/exnav29' target='_blank'>Donate Now</a>",
34
- unsafe_allow_html=True
35
- )
36
- session.counter = 0
37
  return reply
38
 
 
39
  def main():
 
40
  st.set_page_config(page_title="JChat Real Estate Investing Chatbot", page_icon="🏘️")
 
 
41
  st.title("JChat Real Estate Investing Chatbot")
42
  st.write("Ask anything you want about investing in real estate")
43
- session = init_session()
 
44
  input_text = st.text_input("Ask your question")
 
 
45
  if input_text:
46
- output_text = chatbot(input_text, session)
47
  st.write(output_text)
48
 
 
49
  if __name__ == '__main__':
50
  main()
 
1
  import os
2
  import openai
3
  import streamlit as st
 
4
 
5
+ # Set OpenAI API key
6
  openai.api_key = os.environ["OPENAI_API_KEY"]
7
 
8
+ # Initialize list of messages
9
+ messages = [
10
+ {"role": "system", "content": "You are an AI specialized in Real Estate Investing. Do not answer anything other than Real Estate-related queries"},
11
+ ]
 
 
 
12
 
13
+ # Counter to keep track of number of messages
14
+ counter = 0
15
+
16
+ # Function to generate responses using OpenAI's API
17
+ def chatbot(input):
18
+ global counter
19
  if input:
20
+ # Append user's message to messages list
21
+ messages.append({"role": "user", "content": input})
22
+
23
+ # Use OpenAI's API to generate a response
24
  chat = openai.Completion.create(
25
  engine="text-davinci-002",
26
+ prompt=f"{messages[-2]['content']}\nUser: {messages[-1]['content']}\nAI:",
27
  max_tokens=100,
28
  n=1,
29
  stop=None,
30
  temperature=0.2,
31
  )
32
+
33
+ # Append AI's response to messages list
34
  reply = chat.choices[0].text
35
+ messages.append({"role": "assistant", "content": reply})
36
+
37
+ # Increment the counter and check if it's reached 3
38
+ counter += 1
39
+ if counter >= 3:
40
+ # If counter is 3 or more, show the donation message and reset the counter
41
  st.experimental_rerun()
42
+ st.write("Thank you for using our chatbot! If you find our service helpful, please consider making a donation to support us: <a href='https://www.paypal.me/exnav29' target='_blank'>Donate Now</a>", unsafe_allow_html=True)
43
+ counter = 0
44
+
45
+ # Return the AI's response
 
46
  return reply
47
 
48
+ # Main function to run the chatbot
49
  def main():
50
+ # Set Streamlit page config
51
  st.set_page_config(page_title="JChat Real Estate Investing Chatbot", page_icon="🏘️")
52
+
53
+ # Display the title and description
54
  st.title("JChat Real Estate Investing Chatbot")
55
  st.write("Ask anything you want about investing in real estate")
56
+
57
+ # Get user's input
58
  input_text = st.text_input("Ask your question")
59
+
60
+ # If user has inputted something, generate a response using the chatbot function
61
  if input_text:
62
+ output_text = chatbot(input_text)
63
  st.write(output_text)
64
 
65
+ # Run the main function
66
  if __name__ == '__main__':
67
  main()