Compare commits
No commits in common. "1b842ff917b1f64f56c7b929f2d94756fa5cc684" and "3cb6b717165d135eb8033f5c6a67854c2661bb03" have entirely different histories.
1b842ff917
...
3cb6b71716
68
main.py
68
main.py
@ -1,48 +1,10 @@
|
|||||||
import streamlit as st
|
import streamlit as st
|
||||||
import requests
|
import requests
|
||||||
import datetime
|
|
||||||
from pymongo import MongoClient
|
|
||||||
from streamlit_cookies_manager import CookieManager
|
|
||||||
|
|
||||||
# Inject the scroll control HTML/JS
|
# Initialize session state for prompt history
|
||||||
def load_html(file_name):
|
if "history" not in st.session_state:
|
||||||
with open(file_name, 'r', encoding='utf-8') as file:
|
st.session_state.history = []
|
||||||
return file.read()
|
|
||||||
|
|
||||||
# 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.")
|
|
||||||
|
|
||||||
# Function to send POST request to a local server
|
# Function to send POST request to a local server
|
||||||
def send_post_request(prompt):
|
def send_post_request(prompt):
|
||||||
@ -55,6 +17,7 @@ def send_post_request(prompt):
|
|||||||
else:
|
else:
|
||||||
return json
|
return json
|
||||||
|
|
||||||
|
|
||||||
# Custom CSS to fix the input box at the bottom of the page
|
# Custom CSS to fix the input box at the bottom of the page
|
||||||
st.markdown(
|
st.markdown(
|
||||||
"""
|
"""
|
||||||
@ -97,40 +60,25 @@ st.markdown(
|
|||||||
unsafe_allow_html=True
|
unsafe_allow_html=True
|
||||||
)
|
)
|
||||||
|
|
||||||
st.title('Ask Maargdarshak')
|
st.title('Ask maargdarshak')
|
||||||
|
|
||||||
if 'messages' not in st.session_state:
|
if 'messages' not in st.session_state:
|
||||||
st.session_state.messages = []
|
st.session_state.messages = []
|
||||||
|
|
||||||
# Load chat history from session state
|
|
||||||
for message in st.session_state.messages:
|
for message in st.session_state.messages:
|
||||||
st.chat_message(message['role']).markdown(message['content'])
|
st.chat_message(message['role']).markdown(message['content'])
|
||||||
|
|
||||||
# Prompt input
|
|
||||||
prompt = st.chat_input("Ask your question here")
|
prompt = st.chat_input("Ask your question here")
|
||||||
|
|
||||||
if prompt:
|
if prompt:
|
||||||
# Display the user prompt in the chat UI
|
|
||||||
st.chat_message('user').markdown(prompt)
|
st.chat_message('user').markdown(prompt)
|
||||||
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
||||||
|
|
||||||
# Get the bot's response via a POST request
|
|
||||||
response = send_post_request(prompt)
|
response = send_post_request(prompt)
|
||||||
|
print(response)
|
||||||
# Display the bot's response in the chat UI
|
|
||||||
st.chat_message('bot').markdown(response)
|
st.chat_message('bot').markdown(response)
|
||||||
st.session_state.messages.append({'role': 'bot', 'content': response})
|
st.session_state.messages.append({'role': 'bot', 'content': response})
|
||||||
|
# st.session_state.history.append((prompt, response))
|
||||||
|
# st.experimental_rerun()
|
||||||
|
|
||||||
# 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}}},
|
|
||||||
)
|
|
||||||
|
|
||||||
# Save updated history to cookies (optional, MongoDB handles persistence)
|
|
||||||
cookies['history'] = st.session_state.messages
|
|
||||||
cookies.save()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user