Skip to content

Commit 0bb0454

Browse files
authored
Update Chatbot.py Add this Under Hacktoberfest 2025 (#3540)
2 parents 56a7a38 + d044b46 commit 0bb0454

File tree

1 file changed

+65
-27
lines changed

1 file changed

+65
-27
lines changed

Python/Chatbot.py

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,93 @@
11
import re
22
import streamlit as st
3+
from datetime import datetime
34

5+
# ------------------------------
6+
# 💬 Chatbot Response Function
7+
# ------------------------------
48
def chatbot_response(user_input):
5-
user_input = user_input.lower()
9+
"""Generate a chatbot response using regex pattern matching."""
10+
user_input = user_input.lower().strip()
611

712
patterns = {
813
r"\b(hi|hello|hey|hola)\b": "Hello there! 👋 How can I help you today?",
9-
r"\b(how (are|r) you|how's it going)\b": "I'm doing great! How about you?",
10-
r"\b(what is your name|who are you)\b": "I'm ChatBot — your non-friendly chatbot 🤖",
11-
r"\b(help|support|assist|problem)\b": "Sure! Please tell me what issue you’re facing.",
12-
r"\b(thank you|thanks|thx)\b": "You're welcome! 😊",
13-
r"\b(weather|temperature)\b": "I can’t check the weather right now, but it’s always sunny in my code!",
14-
r"\b(time)\b": "Check your system clock!",
15-
r"\b(ok)\b": "Ok,anything else",
16-
r"\b(bye|exit|quit|goodbye|see you)\b": "Bye! Have a great day! 👋",
17-
r"\b(joke|funny)\b": "Why did the computer show up at work late? It had a hard drive! 😂",
18-
r"\b(why)\b": "cause I am a bot",
19-
r"b(no|nah|nope)\b": "Okay",
20-
r"\b(yes|yeah|yep|sure)\b": "Great!"
14+
r"\b(how (are|r) you|how's it going)\b": "I'm doing great! Thanks for asking 😊 How about you?",
15+
r"\b(what is your name|who are you)\b": "I'm PyBot 🤖 — your friendly chatbot!",
16+
r"\b(help|support|assist|problem)\b": "Sure! Please tell me what issue you’re facing. 🛠️",
17+
r"\b(thank you|thanks|thx)\b": "You're most welcome! 🙏",
18+
r"\b(weather|temperature)\b": "I can’t check the weather right now, but it’s always sunny in my code! ☀️",
19+
r"\b(time)\b": f"The current time is {datetime.now().strftime('%I:%M %p')} 🕒",
20+
r"\b(ok|okay|fine)\b": "Okay! 👍 Anything else?",
21+
r"\b(bye|exit|quit|goodbye|see you)\b": "Bye! 👋 Have a fantastic day!",
22+
r"\b(joke|funny)\b": "😂 Why did the computer show up at work late? It had a hard drive!",
23+
r"\b(why)\b": "Because that's how my developer coded me 🤖",
24+
r"\b(no|nah|nope)\b": "Alright, no worries 🙂",
25+
r"\b(yes|yeah|yep|sure)\b": "Great! Let's continue 🚀"
2126
}
2227

28+
# Match input with regex patterns
2329
for pattern, response in patterns.items():
2430
if re.search(pattern, user_input):
2531
return response
2632

27-
return "Hmm, I didn’t quite understand that. Could you try rephrasing?"
33+
# Default response
34+
return "Hmm 🤔, I didn’t quite get that. Could you please rephrase?"
2835

29-
st.set_page_config(page_title="Chatbot", page_icon="🤖", layout="centered")
36+
# ------------------------------
37+
# ⚙️ Streamlit Page Config
38+
# ------------------------------
39+
st.set_page_config(page_title="PyBot Chat", page_icon="🤖", layout="centered")
3040

31-
st.title("Chatbot")
32-
# st.markdown("Chat with a simple **rule-based chatbot** built using **Python + Streamlit + Regex**.")
33-
34-
# Initialize chat history in Streamlit session state
41+
# ------------------------------
42+
# 🧠 Chat History Initialization
43+
# ------------------------------
3544
if "chat_history" not in st.session_state:
3645
st.session_state.chat_history = []
3746

38-
# Input box
39-
user_input = st.chat_input("Type your message...")
47+
# ------------------------------
48+
# 🧾 App Title
49+
# ------------------------------
50+
st.markdown("<h1 style='text-align:center;'>🤖 PyBot - Your Mini Chat Assistant</h1>", unsafe_allow_html=True)
51+
st.caption("A simple rule-based chatbot built using Python + Streamlit + Regex")
52+
53+
st.divider()
54+
55+
# ------------------------------
56+
# 💬 Chat Input Area
57+
# ------------------------------
58+
user_input = st.chat_input("Type your message here...")
4059

60+
# If user sends a message
4161
if user_input:
4262
response = chatbot_response(user_input)
4363
st.session_state.chat_history.append(("You", user_input))
4464
st.session_state.chat_history.append(("PyBot", response))
4565

46-
# Display chat history
66+
# ------------------------------
67+
# 🗨️ Display Chat History
68+
# ------------------------------
4769
for sender, message in st.session_state.chat_history:
4870
if sender == "You":
49-
st.markdown(f"🧑 **You:** {message}")
71+
st.markdown(
72+
f"<div style='background-color:#DCF8C6;padding:10px;border-radius:10px;margin:5px 0;'><b>🧑 You:</b> {message}</div>",
73+
unsafe_allow_html=True
74+
)
5075
else:
51-
st.markdown(f"🤖 **Bot:** {message}")
76+
st.markdown(
77+
f"<div style='background-color:#E6E6FA;padding:10px;border-radius:10px;margin:5px 0;'><b>🤖 PyBot:</b> {message}</div>",
78+
unsafe_allow_html=True
79+
)
80+
81+
# ------------------------------
82+
# 🧹 Clear Chat Button
83+
# ------------------------------
84+
if st.button("🗑️ Clear Chat"):
85+
st.session_state.chat_history = []
86+
st.experimental_rerun()
87+
88+
st.divider()
5289

53-
# Footer note
54-
st.markdown("---")
55-
st.caption("A simple Chatbot ")
90+
# ------------------------------
91+
# 📜 Footer
92+
# ------------------------------
93+
st.markdown("<center>Built with ❤️ using <b>Streamlit</b></center>", unsafe_allow_html=True)

0 commit comments

Comments
 (0)