2024-10-13 04:55:54 +00:00
|
|
|
import streamlit as st
|
|
|
|
import requests
|
2024-10-13 14:41:32 +00:00
|
|
|
import datetime
|
|
|
|
from pymongo import MongoClient
|
|
|
|
from streamlit_cookies_manager import CookieManager
|
2024-10-12 19:26:45 +00:00
|
|
|
|
2024-10-13 14:41:32 +00:00
|
|
|
# Inject the scroll control HTML/JS
|
|
|
|
def load_html(file_name):
|
|
|
|
with open(file_name, 'r', encoding='utf-8') as file:
|
|
|
|
return file.read()
|
2024-10-13 04:55:54 +00:00
|
|
|
|
2024-10-13 14:41:32 +00:00
|
|
|
# Initialize the Cookie Manager (no encryption)
|
|
|
|
cookies = CookieManager(prefix="ktosiek/streamlit-cookies-manager/")
|
|
|
|
if not cookies.ready():
|
|
|
|
st.stop()
|
|
|
|
|
|
|
|
# Connect to MongoDB
|
|
|
|
client = MongoClient("mongodb://localhost:27017/") # Adjust your MongoDB connection URI
|
|
|
|
db = client["chatbot_db"] # Database name
|
|
|
|
users_collection = db["users"] # Collection to store user data
|
|
|
|
|
|
|
|
# Generate a user ID and store it in cookies if not already set
|
|
|
|
if 'user_id' not in cookies:
|
|
|
|
cookies['user_id'] = str(datetime.datetime.now().timestamp()) # Use timestamp as a unique ID
|
|
|
|
cookies.save()
|
|
|
|
|
|
|
|
user_id = cookies.get('user_id')
|
|
|
|
# st.write(f"Current User ID: {user_id}")
|
|
|
|
|
|
|
|
# Check if the user exists in the database
|
|
|
|
user_data = users_collection.find_one({"user_id": user_id})
|
|
|
|
|
|
|
|
# If user doesn't exist, create a new entry with the current timestamp and empty chat history
|
|
|
|
if not user_data:
|
|
|
|
timestamp = str(datetime.datetime.now().isoformat()) # Unique timestamp for each user
|
|
|
|
users_collection.insert_one({
|
|
|
|
"user_id": user_id,
|
|
|
|
"timestamp": timestamp,
|
|
|
|
"messages": [] # Empty chat history
|
|
|
|
})
|
|
|
|
st.write("New user detected! Created user entry in the database.")
|
|
|
|
else:
|
|
|
|
# If user exists, load the chat history from the database
|
|
|
|
st.session_state.messages = user_data["messages"]
|
|
|
|
# st.write("Returning user detected! Loaded chat history from the database.")
|
2024-10-13 04:55:54 +00:00
|
|
|
|
2024-10-13 13:26:31 +00:00
|
|
|
# Function to send POST request to a local server
|
2024-10-13 04:55:54 +00:00
|
|
|
def send_post_request(prompt):
|
2024-10-13 13:26:31 +00:00
|
|
|
url = "http://localhost:19194/" # Update this to your actual server URL
|
2024-10-13 04:55:54 +00:00
|
|
|
payload = prompt.title()
|
|
|
|
response = requests.post(url, json=payload)
|
|
|
|
return response.text
|
|
|
|
|
2024-10-13 13:26:31 +00:00
|
|
|
# Custom CSS to fix the input box at the bottom of the page
|
|
|
|
st.markdown(
|
|
|
|
"""
|
|
|
|
<style>
|
|
|
|
.fixed-input-container {
|
|
|
|
position: fixed;
|
|
|
|
bottom: 0;
|
|
|
|
left: 0;
|
|
|
|
width: 100%;
|
|
|
|
background-color: #f9f9f9;
|
|
|
|
padding: 10px;
|
|
|
|
box-shadow: 0px -2px 10px rgba(0, 0, 0, 0.1);
|
|
|
|
z-index: 999;
|
|
|
|
}
|
|
|
|
.input-container input {
|
|
|
|
width: 80%;
|
|
|
|
padding: 10px;
|
|
|
|
font-size: 16px;
|
|
|
|
border-radius: 5px;
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
margin-right: 10px;
|
|
|
|
}
|
|
|
|
.input-container button {
|
|
|
|
padding: 10px 20px;
|
|
|
|
font-size: 16px;
|
|
|
|
border: none;
|
|
|
|
background-color: #4CAF50;
|
|
|
|
color: white;
|
|
|
|
border-radius: 5px;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
.input-container button:hover {
|
|
|
|
background-color: #45a049;
|
|
|
|
}
|
|
|
|
.prompt-history {
|
|
|
|
margin-bottom: 120px; /* Space to ensure prompt history doesn't overlap input box */
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
""",
|
|
|
|
unsafe_allow_html=True
|
|
|
|
)
|
|
|
|
|
2024-10-13 14:41:32 +00:00
|
|
|
st.title('Ask Maargdarshak')
|
2024-10-13 13:26:31 +00:00
|
|
|
|
|
|
|
if 'messages' not in st.session_state:
|
|
|
|
st.session_state.messages = []
|
|
|
|
|
2024-10-13 14:41:32 +00:00
|
|
|
# Load chat history from session state
|
2024-10-13 13:26:31 +00:00
|
|
|
for message in st.session_state.messages:
|
|
|
|
st.chat_message(message['role']).markdown(message['content'])
|
|
|
|
|
2024-10-13 14:41:32 +00:00
|
|
|
# Prompt input
|
2024-10-13 13:26:31 +00:00
|
|
|
prompt = st.chat_input("Ask your question here")
|
2024-10-13 04:55:54 +00:00
|
|
|
|
2024-10-13 13:26:31 +00:00
|
|
|
if prompt:
|
2024-10-13 14:41:32 +00:00
|
|
|
# Display the user prompt in the chat UI
|
2024-10-13 13:26:31 +00:00
|
|
|
st.chat_message('user').markdown(prompt)
|
|
|
|
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
2024-10-13 04:55:54 +00:00
|
|
|
|
2024-10-13 14:41:32 +00:00
|
|
|
# Get the bot's response via a POST request
|
2024-10-13 13:26:31 +00:00
|
|
|
response = send_post_request(prompt)
|
2024-10-13 14:41:32 +00:00
|
|
|
|
|
|
|
# Display the bot's response in the chat UI
|
2024-10-13 13:26:31 +00:00
|
|
|
st.chat_message('bot').markdown(response)
|
|
|
|
st.session_state.messages.append({'role': 'bot', 'content': response})
|
2024-10-13 04:55:54 +00:00
|
|
|
|
2024-10-13 14:41:32 +00:00
|
|
|
# Update the user's chat history in the database
|
|
|
|
users_collection.update_one(
|
|
|
|
{"user_id": user_id},
|
|
|
|
{"$push": {"messages": {'role': 'user', 'content': prompt}}},
|
|
|
|
)
|
|
|
|
users_collection.update_one(
|
|
|
|
{"user_id": user_id},
|
|
|
|
{"$push": {"messages": {'role': 'bot', 'content': response}}},
|
|
|
|
)
|
2024-10-13 04:55:54 +00:00
|
|
|
|
2024-10-13 14:41:32 +00:00
|
|
|
# Save updated history to cookies (optional, MongoDB handles persistence)
|
|
|
|
cookies['history'] = st.session_state.messages
|
|
|
|
cookies.save()
|