49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
|
import streamlit as st
|
||
|
import time
|
||
|
from cookies_manager import EncryptedCookieManager
|
||
|
|
||
|
# This should be your unique encryption key, which should be kept secure.
|
||
|
cookie_manager = EncryptedCookieManager(
|
||
|
prefix="myapp_", # Define a prefix for the cookie to avoid conflicts
|
||
|
password="your_secure_cookie_password" # Replace with your secure password
|
||
|
)
|
||
|
|
||
|
# Initialize cookies
|
||
|
if not cookie_manager.ready():
|
||
|
st.stop()
|
||
|
|
||
|
|
||
|
def get_user_timestamp():
|
||
|
# Check if timestamp already exists in cookies
|
||
|
timestamp = cookie_manager.get("user_timestamp")
|
||
|
|
||
|
if timestamp:
|
||
|
st.write(f"Welcome back! Your session started at: {timestamp}")
|
||
|
return timestamp
|
||
|
else:
|
||
|
# Generate a new timestamp in milliseconds
|
||
|
new_timestamp = str(int(time.time() * 1000))
|
||
|
cookie_manager.set("user_timestamp", new_timestamp, max_age=3600 * 24 * 7) # Store the timestamp for 7 days
|
||
|
st.write(f"New session started! Your timestamp is: {new_timestamp}")
|
||
|
return new_timestamp
|
||
|
|
||
|
|
||
|
# Chatbot function
|
||
|
def chatbot():
|
||
|
st.title("Chatbot with Timestamps in Cookies")
|
||
|
|
||
|
# Retrieve or create timestamp for the user
|
||
|
timestamp = get_user_timestamp()
|
||
|
|
||
|
# Display the chat prompt
|
||
|
user_input = st.text_input("You: ", "")
|
||
|
|
||
|
if user_input:
|
||
|
# Here, instead of querying Ollama, we simulate a chatbot response
|
||
|
st.write(f"**Bot:** You said '{user_input}' at {timestamp}")
|
||
|
|
||
|
|
||
|
# Main
|
||
|
if __name__ == "__main__":
|
||
|
chatbot()
|