|
1 | 1 | import re |
2 | 2 | import streamlit as st |
| 3 | +from datetime import datetime |
3 | 4 |
|
| 5 | +# ------------------------------ |
| 6 | +# 💬 Chatbot Response Function |
| 7 | +# ------------------------------ |
4 | 8 | 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() |
6 | 11 |
|
7 | 12 | patterns = { |
8 | 13 | 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 🚀" |
21 | 26 | } |
22 | 27 |
|
| 28 | + # Match input with regex patterns |
23 | 29 | for pattern, response in patterns.items(): |
24 | 30 | if re.search(pattern, user_input): |
25 | 31 | return response |
26 | 32 |
|
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?" |
28 | 35 |
|
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") |
30 | 40 |
|
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 | +# ------------------------------ |
35 | 44 | if "chat_history" not in st.session_state: |
36 | 45 | st.session_state.chat_history = [] |
37 | 46 |
|
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...") |
40 | 59 |
|
| 60 | +# If user sends a message |
41 | 61 | if user_input: |
42 | 62 | response = chatbot_response(user_input) |
43 | 63 | st.session_state.chat_history.append(("You", user_input)) |
44 | 64 | st.session_state.chat_history.append(("PyBot", response)) |
45 | 65 |
|
46 | | -# Display chat history |
| 66 | +# ------------------------------ |
| 67 | +# 🗨️ Display Chat History |
| 68 | +# ------------------------------ |
47 | 69 | for sender, message in st.session_state.chat_history: |
48 | 70 | 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 | + ) |
50 | 75 | 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() |
52 | 89 |
|
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