aguyader commited on
Commit
ab5cebc
·
verified ·
1 Parent(s): 803754f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -32
app.py CHANGED
@@ -1,40 +1,28 @@
1
  import streamlit as st
 
2
 
3
- # Initialize session state to store chat history
4
- if "messages" not in st.session_state:
5
- st.session_state["messages"] = []
 
6
 
7
- # Function for chatbot responses
8
- def chatbot_response(user_input):
9
- if user_input.endswith("?"):
10
- return "Yes"
11
- else:
12
- return "Ask me anything!"
13
 
14
- # Streamlit app layout
15
- st.title("Simple Chatbot")
16
- st.markdown("Ask me any yes/no question or anything else!")
17
 
18
- # Display chat history
19
- st.markdown("### Chat History")
20
- for user, bot in st.session_state["messages"]:
21
- st.markdown(f"**You:** {user}")
22
- st.markdown(f"**Bot:** {bot}")
23
 
24
- # User input
25
- user_input = st.text_input("Your message:", key="user_input")
 
 
 
 
 
26
 
27
- # When user submits input
28
- if user_input:
29
- # Get chatbot response
30
- bot_response = chatbot_response(user_input)
31
- # Append the interaction to chat history
32
- st.session_state["messages"].append((user_input, bot_response))
33
- # Clear input box
34
- st.session_state["user_input"] = ""
35
 
36
- # Display chat history
37
- st.markdown("### Chat History")
38
- for user, bot in st.session_state["messages"]:
39
- st.markdown(f"**You:** {user}")
40
- st.markdown(f"**Bot:** {bot}")
 
1
  import streamlit as st
2
+ import random
3
 
4
+ # Simple chatbot logic
5
+ def chatbot_response(question):
6
+ responses = ["Yes", "No", "Maybe", "Absolutely", "Definitely not"]
7
+ return random.choice(responses)
8
 
9
+ # Streamlit App
10
+ st.title("Yes/No Question Chatbot")
11
+ st.write("Ask me any yes/no question, and I'll give you an answer!")
 
 
 
12
 
13
+ # Initialize session state for input clearing
14
+ if "user_input" not in st.session_state:
15
+ st.session_state["user_input"] = ""
16
 
17
+ # Input box
18
+ user_input = st.text_input("Type your question here:", value=st.session_state["user_input"], key="input_box")
 
 
 
19
 
20
+ if st.button("Ask"): # When the user clicks the "Ask" button
21
+ if user_input.strip():
22
+ response = chatbot_response(user_input)
23
+ st.write(f"Chatbot: {response}")
24
+ else:
25
+ st.write("Chatbot: Please ask a valid question!")
26
+ st.session_state["user_input"] = "" # Clear the input box
27
 
 
 
 
 
 
 
 
 
28