Spaces:
Sleeping
Sleeping
File size: 6,363 Bytes
6bc5fb7 5d0c611 6bc5fb7 7fc2c5f 6bc5fb7 3a1d273 880bb75 3a1d273 6bc5fb7 5d0c611 6bc5fb7 5d0c611 6bc5fb7 5d0c611 6bc5fb7 5d0c611 6bc5fb7 7fc2c5f 5d0c611 6bc5fb7 7fc2c5f 5d0c611 7fc2c5f 614f6a8 7fc2c5f 5d0c611 614f6a8 7fc2c5f 5d0c611 7fc2c5f 614f6a8 7fc2c5f 614f6a8 7fc2c5f 5d0c611 7fc2c5f 614f6a8 7fc2c5f 5d0c611 7fc2c5f 6bc5fb7 5d0c611 614f6a8 5d0c611 6bc5fb7 5d0c611 6bc5fb7 5d0c611 6bc5fb7 5d0c611 6bc5fb7 614f6a8 c830f64 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
import streamlit as st
import torch
from transformers import BertTokenizer, BertForSequenceClassification
import json
import os
import time
import datetime
import random
from streamlit_extras.add_vertical_space import add_vertical_space
from streamlit_extras.let_it_rain import rain
from streamlit_modal import Modal
# Streamlit UI
st.set_page_config(page_title="🧠ChatBot🤖", layout="wide")
# Load BERT Model & Tokenizer
MODEL_PATH = "Trained Model/chatbot_model" # Path where your model & tokenizer are saved
# Set device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load model & tokenizer
tokenizer = BertTokenizer.from_pretrained(MODEL_PATH)
model = BertForSequenceClassification.from_pretrained(MODEL_PATH)
model.to(device)
model.eval()
# Load intents
with open("intents.json", "r") as file:
intents = json.load(file)
intent_mapping = {i: intent['tag'] for i, intent in enumerate(intents)}
# Chat history file
CHAT_HISTORY_FILE = "chat_history.json"
# Load Chat History (Ensuring Persistence)
def load_chat_history():
if os.path.exists(CHAT_HISTORY_FILE):
with open(CHAT_HISTORY_FILE, "r") as file:
return json.load(file)
return []
# Save Chat History
def save_chat_history():
with open(CHAT_HISTORY_FILE, "w") as file:
json.dump(st.session_state.chat_history, file)
# Initialize session state variables
if "chat_history" not in st.session_state:
st.session_state.chat_history = load_chat_history()
if "show_history" not in st.session_state:
st.session_state.show_history = False
if "show_about" not in st.session_state:
st.session_state.show_about = False
if "chat_input" not in st.session_state:
st.session_state.chat_input = ""
def predict_intent(text):
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True).to(device)
with torch.no_grad():
outputs = model(**inputs)
predicted_index = torch.argmax(outputs.logits, dim=1).item()
return intent_mapping.get(predicted_index, "unknown")
def get_response(intent_label):
for intent in intents:
if intent_label == intent['tag']:
return random.choice(intent['responses'])
return "I'm not sure how to respond to that."
# Sidebar menu
with st.sidebar:
st.image("Images/Bot Image.jpeg", use_container_width=True)
st.title("💬 Chatbot Menu")
add_vertical_space(1)
if st.button("🆕 New Chat"):
st.session_state.chat_input = ""
save_chat_history()
if st.button("📜 Chat History"):
st.session_state.show_history = True
if st.button("ℹ️ About"):
st.session_state.show_about = True
st.caption("🚀 Built with ❤️ by AI Enthusiast")
# Modal Windows (Chat History & About)
history_modal = Modal("📜Chat History", key="history_modal")
about_modal = Modal("ℹ️About", key="about_modal")
# Display chat history without affecting the chat window
if st.session_state.show_history:
with history_modal.container():
if st.session_state.chat_history:
for chat in st.session_state.chat_history[-10:]: # Show last 10 messages
st.write(chat)
else:
st.info("No chat history available.")
# Clear History Button
if st.button("🗑 Clear History", key="clear_history"):
st.session_state.chat_history = [] # Clear session history
if os.path.exists(CHAT_HISTORY_FILE):
os.remove(CHAT_HISTORY_FILE) # Delete saved file
st.rerun() # Refresh UI to reflect changes
# Close Modal Button
if st.button("Close", key="close_history"):
st.session_state.show_history = False
st.rerun()
if st.session_state.show_about:
with about_modal.container():
st.info("""This chatbot is powered by a deep learning approach using Hugging Face Transformers and a BERT model for intent recognition.
It understands user queries by predicting intent and responding with predefined answers based on a trained dataset.
✅ Natural Language Processing (NLP)
🔹 Transformers (Hugging Face) 🤖 – BERT-based intent classification
✅ Deep Learning
🔹 PyTorch 🔥 – Model training & fine-tuning
""")
if st.button("Close", key="close_about"):
st.session_state.show_about = False # Close modal without resetting UI
st.rerun()
# Chat UI
st.title("🤖 AI Chatbot")
st.markdown("### Talk to me, I'm listening...")
rain(emoji="💬", font_size=10, falling_speed=5, animation_length="infinite")
rain(emoji="❄️", font_size=10, falling_speed=5, animation_length="infinite")
# Chat Input with Enter Button
with st.form("chat_form", clear_on_submit=True):
chat_input = st.text_input("You:", key="chat_input")
submit_button = st.form_submit_button("Enter")
if submit_button and chat_input:
intent_label = predict_intent(chat_input)
response = get_response(intent_label)
chat_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Append messages to chat history (User & Bot)
st.session_state.chat_history.append(f"[{chat_time}] You: {chat_input}")
st.session_state.chat_history.append(f"[{chat_time}] Bot: {response}")
# Save chat history persistently
save_chat_history()
# Display conversation immediately
with st.chat_message("user"):
st.markdown(f"**You:** {chat_input}")
time.sleep(0.5)
with st.chat_message("assistant"):
st.markdown(f"**Bot:** {response}")
# Style customization
st.markdown("""
<style>
/* Fix Modal Height & Enable Scroll */
div[data-modal-container="true"] {
position: fixed !important;
top: 5% !important; /* Adjust this value to position it higher */
color: #000000;
max-height: 1000px !important; /* Set max height */
}
/* Ensure the close button stays visible */
div[data-modal-container="true"] button {
position: sticky;
bottom: 10px;
background-color: #000000;
color: white;
}
.stChatMessage {padding: 10px; border-radius: 10px; background-color: #900C3F;}
.stChatMessageUser {background-color: #900C3F;}
.stChatMessageAssistant {background-color: #900C3F;}
</style>""", unsafe_allow_html=True) |