hopefully stops input while generation
This commit is contained in:
parent
1b842ff917
commit
2c5115b70d
22
main.py
22
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,14 +112,23 @@ 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'])
|
||||||
|
|
||||||
|
# Disable the input box if processing a prompt
|
||||||
|
if st.session_state.processing:
|
||||||
|
st.chat_input("Processing...", disabled=True)
|
||||||
|
else:
|
||||||
# Prompt input
|
# Prompt input
|
||||||
prompt = st.chat_input("Ask your question here")
|
prompt = st.chat_input("Ask your question here")
|
||||||
|
|
||||||
if prompt:
|
if prompt:
|
||||||
|
# Set the processing flag to True to disable input
|
||||||
|
st.session_state.processing = True
|
||||||
|
|
||||||
# Display the user prompt in the chat UI
|
# 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})
|
||||||
|
|
||||||
|
# Show spinner while generating the response
|
||||||
|
with st.spinner("Generating response..."):
|
||||||
# Get the bot's response via a POST request
|
# Get the bot's response via a POST request
|
||||||
response = send_post_request(prompt)
|
response = send_post_request(prompt)
|
||||||
|
|
||||||
@ -134,3 +149,6 @@ if prompt:
|
|||||||
# Save updated history to cookies (optional, MongoDB handles persistence)
|
# Save updated history to cookies (optional, MongoDB handles persistence)
|
||||||
cookies['history'] = st.session_state.messages
|
cookies['history'] = st.session_state.messages
|
||||||
cookies.save()
|
cookies.save()
|
||||||
|
|
||||||
|
# Reset the processing flag to enable input again
|
||||||
|
st.session_state.processing = False
|
||||||
|
Loading…
Reference in New Issue
Block a user