Spaces:
Sleeping
Sleeping
Samarth4023 commited on
Commit ·
7fc2c5f
1
Parent(s): 2ff182c
Modified chatbot.py
Browse files- chatbot.py +35 -11
chatbot.py
CHANGED
|
@@ -7,6 +7,7 @@ import datetime
|
|
| 7 |
import random
|
| 8 |
from streamlit_extras.add_vertical_space import add_vertical_space
|
| 9 |
from streamlit_extras.let_it_rain import rain
|
|
|
|
| 10 |
|
| 11 |
# Load BERT Model & Tokenizer
|
| 12 |
MODEL_PATH = "Trained Model/chatbot_model" # Path where your model & tokenizer are saved
|
|
@@ -59,6 +60,8 @@ def get_response(intent_label):
|
|
| 59 |
# Initialize session state for chat history
|
| 60 |
if "chat_history" not in st.session_state:
|
| 61 |
st.session_state.chat_history = []
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Sidebar menu
|
| 64 |
with st.sidebar:
|
|
@@ -66,15 +69,33 @@ with st.sidebar:
|
|
| 66 |
add_vertical_space(1)
|
| 67 |
if st.button("🆕 New Chat"):
|
| 68 |
st.session_state.chat_history = []
|
|
|
|
| 69 |
st.rerun()
|
| 70 |
if st.button("📜 Chat History"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
st.subheader("🕰 Chat History")
|
| 72 |
for chat in st.session_state.chat_history:
|
| 73 |
st.write(chat)
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
st.info("This is an intent-based chatbot powered by BERT, built using Streamlit with an elegant UI.")
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
# Chat UI
|
| 80 |
st.title("🤖 AI Chatbot")
|
|
@@ -88,26 +109,29 @@ rain(
|
|
| 88 |
)
|
| 89 |
|
| 90 |
# User input
|
| 91 |
-
|
| 92 |
-
if
|
| 93 |
-
intent_label = predict_intent(
|
| 94 |
response = get_response(intent_label)
|
| 95 |
chat_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 96 |
-
st.session_state.chat_history.append(f"[{chat_time}] You: {
|
| 97 |
st.session_state.chat_history.append(f"[{chat_time}] Bot: {response}")
|
| 98 |
|
| 99 |
# Display conversation
|
| 100 |
with st.chat_message("user"):
|
| 101 |
-
st.markdown(f"**You:** {
|
| 102 |
time.sleep(0.5)
|
| 103 |
with st.chat_message("assistant"):
|
| 104 |
st.markdown(f"**Bot:** {response}")
|
| 105 |
|
|
|
|
|
|
|
|
|
|
| 106 |
# Style customization
|
| 107 |
st.markdown("""
|
| 108 |
<style>
|
| 109 |
.stChatMessage {padding: 10px; border-radius: 10px; background-color: #000000;}
|
| 110 |
-
.stChatMessageUser {background-color: #
|
| 111 |
-
.stChatMessageAssistant {background-color: #
|
| 112 |
</style>
|
| 113 |
-
""", unsafe_allow_html=True)
|
|
|
|
| 7 |
import random
|
| 8 |
from streamlit_extras.add_vertical_space import add_vertical_space
|
| 9 |
from streamlit_extras.let_it_rain import rain
|
| 10 |
+
from streamlit_modal import Modal
|
| 11 |
|
| 12 |
# Load BERT Model & Tokenizer
|
| 13 |
MODEL_PATH = "Trained Model/chatbot_model" # Path where your model & tokenizer are saved
|
|
|
|
| 60 |
# Initialize session state for chat history
|
| 61 |
if "chat_history" not in st.session_state:
|
| 62 |
st.session_state.chat_history = []
|
| 63 |
+
if "chat_input" not in st.session_state:
|
| 64 |
+
st.session_state.chat_input = ""
|
| 65 |
|
| 66 |
# Sidebar menu
|
| 67 |
with st.sidebar:
|
|
|
|
| 69 |
add_vertical_space(1)
|
| 70 |
if st.button("🆕 New Chat"):
|
| 71 |
st.session_state.chat_history = []
|
| 72 |
+
st.session_state.chat_input = ""
|
| 73 |
st.rerun()
|
| 74 |
if st.button("📜 Chat History"):
|
| 75 |
+
st.session_state.show_history = True
|
| 76 |
+
if st.button("ℹ️ About"):
|
| 77 |
+
st.session_state.show_about = True
|
| 78 |
+
|
| 79 |
+
# Modals for Chat History and About
|
| 80 |
+
history_modal = Modal("Chat History", key="history_modal")
|
| 81 |
+
about_modal = Modal("About", key="about_modal")
|
| 82 |
+
|
| 83 |
+
if "show_history" in st.session_state and st.session_state.show_history:
|
| 84 |
+
with history_modal.container():
|
| 85 |
st.subheader("🕰 Chat History")
|
| 86 |
for chat in st.session_state.chat_history:
|
| 87 |
st.write(chat)
|
| 88 |
+
if st.button("Close", key="close_history"):
|
| 89 |
+
st.session_state.show_history = False
|
| 90 |
+
st.rerun()
|
| 91 |
+
|
| 92 |
+
if "show_about" in st.session_state and st.session_state.show_about:
|
| 93 |
+
with about_modal.container():
|
| 94 |
+
st.subheader("ℹ️ About")
|
| 95 |
st.info("This is an intent-based chatbot powered by BERT, built using Streamlit with an elegant UI.")
|
| 96 |
+
if st.button("Close", key="close_about"):
|
| 97 |
+
st.session_state.show_about = False
|
| 98 |
+
st.rerun()
|
| 99 |
|
| 100 |
# Chat UI
|
| 101 |
st.title("🤖 AI Chatbot")
|
|
|
|
| 109 |
)
|
| 110 |
|
| 111 |
# User input
|
| 112 |
+
st.session_state.chat_input = st.text_input("You:", st.session_state.chat_input, key="input")
|
| 113 |
+
if st.session_state.chat_input:
|
| 114 |
+
intent_label = predict_intent(st.session_state.chat_input)
|
| 115 |
response = get_response(intent_label)
|
| 116 |
chat_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 117 |
+
st.session_state.chat_history.append(f"[{chat_time}] You: {st.session_state.chat_input}")
|
| 118 |
st.session_state.chat_history.append(f"[{chat_time}] Bot: {response}")
|
| 119 |
|
| 120 |
# Display conversation
|
| 121 |
with st.chat_message("user"):
|
| 122 |
+
st.markdown(f"**You:** {st.session_state.chat_input}")
|
| 123 |
time.sleep(0.5)
|
| 124 |
with st.chat_message("assistant"):
|
| 125 |
st.markdown(f"**Bot:** {response}")
|
| 126 |
|
| 127 |
+
# Clear input box after sending
|
| 128 |
+
st.session_state.chat_input = ""
|
| 129 |
+
|
| 130 |
# Style customization
|
| 131 |
st.markdown("""
|
| 132 |
<style>
|
| 133 |
.stChatMessage {padding: 10px; border-radius: 10px; background-color: #000000;}
|
| 134 |
+
.stChatMessageUser {background-color: #000000;}
|
| 135 |
+
.stChatMessageAssistant {background-color: #000000;}
|
| 136 |
</style>
|
| 137 |
+
""", unsafe_allow_html=True)
|