hopefully stops input while generation #7

Closed
lakhia13 wants to merge 1 commits from fix/chatstop into main

22
main.py

@ -4,11 +4,13 @@ import datetime
from pymongo import MongoClient
from streamlit_cookies_manager import CookieManager
# Inject the scroll control HTML/JS
def load_html(file_name):
with open(file_name, 'r', encoding='utf-8') as file:
return file.read()
# Initialize the Cookie Manager (no encryption)
cookies = CookieManager(prefix="ktosiek/streamlit-cookies-manager/")
if not cookies.ready():
@ -25,7 +27,6 @@ if 'user_id' not in cookies:
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})
@ -42,7 +43,11 @@ if not user_data:
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.")
# 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
def send_post_request(prompt):
@ -55,6 +60,7 @@ def send_post_request(prompt):
else:
return json
# Custom CSS to fix the input box at the bottom of the page
st.markdown(
"""
@ -106,14 +112,23 @@ if 'messages' not in st.session_state:
for message in st.session_state.messages:
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 = st.chat_input("Ask your question here")
if prompt:
# Set the processing flag to True to disable input
st.session_state.processing = True
# Display the user prompt in the chat UI
st.chat_message('user').markdown(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
response = send_post_request(prompt)
@ -134,3 +149,6 @@ if prompt:
# 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