hopefully stops input while generation #7
68
main.py
68
main.py
@ -4,11 +4,13 @@ import datetime
|
|||||||
from pymongo import MongoClient
|
from pymongo import MongoClient
|
||||||
from streamlit_cookies_manager import CookieManager
|
from streamlit_cookies_manager import CookieManager
|
||||||
|
|
||||||
|
|
||||||
# Inject the scroll control HTML/JS
|
# Inject the scroll control HTML/JS
|
||||||
def load_html(file_name):
|
def load_html(file_name):
|
||||||
with open(file_name, 'r', encoding='utf-8') as file:
|
with open(file_name, 'r', encoding='utf-8') as file:
|
||||||
return file.read()
|
return file.read()
|
||||||
|
|
||||||
|
|
||||||
# Initialize the Cookie Manager (no encryption)
|
# Initialize the Cookie Manager (no encryption)
|
||||||
cookies = CookieManager(prefix="ktosiek/streamlit-cookies-manager/")
|
cookies = CookieManager(prefix="ktosiek/streamlit-cookies-manager/")
|
||||||
if not cookies.ready():
|
if not cookies.ready():
|
||||||
@ -25,7 +27,6 @@ if 'user_id' not in cookies:
|
|||||||
cookies.save()
|
cookies.save()
|
||||||
|
|
||||||
user_id = cookies.get('user_id')
|
user_id = cookies.get('user_id')
|
||||||
# st.write(f"Current User ID: {user_id}")
|
|
||||||
|
|
||||||
# Check if the user exists in the database
|
# Check if the user exists in the database
|
||||||
user_data = users_collection.find_one({"user_id": user_id})
|
user_data = users_collection.find_one({"user_id": user_id})
|
||||||
@ -42,7 +43,11 @@ if not user_data:
|
|||||||
else:
|
else:
|
||||||
# If user exists, load the chat history from the database
|
# If user exists, load the chat history from the database
|
||||||
st.session_state.messages = user_data["messages"]
|
st.session_state.messages = user_data["messages"]
|
||||||
# st.write("Returning user detected! Loaded chat history from the database.")
|
|
||||||
|
# Initialize a processing flag in session state
|
||||||
|
if 'processing' not in st.session_state:
|
||||||
|
st.session_state.processing = False
|
||||||
|
|
||||||
|
|
||||||
# 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 +60,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(
|
||||||
"""
|
"""
|
||||||
@ -106,31 +112,43 @@ if 'messages' not in st.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
|
# Disable the input box if processing a prompt
|
||||||
prompt = st.chat_input("Ask your question here")
|
if st.session_state.processing:
|
||||||
|
st.chat_input("Processing...", disabled=True)
|
||||||
|
else:
|
||||||
|
# Prompt input
|
||||||
|
prompt = st.chat_input("Ask your question here")
|
||||||
|
|
||||||
if prompt:
|
if prompt:
|
||||||
# Display the user prompt in the chat UI
|
# Set the processing flag to True to disable input
|
||||||
st.chat_message('user').markdown(prompt)
|
st.session_state.processing = True
|
||||||
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
|
||||||
|
|
||||||
# Get the bot's response via a POST request
|
# Display the user prompt in the chat UI
|
||||||
response = send_post_request(prompt)
|
st.chat_message('user').markdown(prompt)
|
||||||
|
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
||||||
|
|
||||||
# Display the bot's response in the chat UI
|
# Show spinner while generating the response
|
||||||
st.chat_message('bot').markdown(response)
|
with st.spinner("Generating response..."):
|
||||||
st.session_state.messages.append({'role': 'bot', 'content': response})
|
# Get the bot's response via a POST request
|
||||||
|
response = send_post_request(prompt)
|
||||||
|
|
||||||
# Update the user's chat history in the database
|
# Display the bot's response in the chat UI
|
||||||
users_collection.update_one(
|
st.chat_message('bot').markdown(response)
|
||||||
{"user_id": user_id},
|
st.session_state.messages.append({'role': 'bot', 'content': response})
|
||||||
{"$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)
|
# Update the user's chat history in the database
|
||||||
cookies['history'] = st.session_state.messages
|
users_collection.update_one(
|
||||||
cookies.save()
|
{"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()
|
||||||
|
|
||||||
|
# Reset the processing flag to enable input again
|
||||||
|
st.session_state.processing = False
|
||||||
|
Loading…
Reference in New Issue
Block a user