-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
144 lines (115 loc) · 4.29 KB
/
streamlit_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import streamlit as st
import streamlit_authenticator as stauth
from streamlit_autorefresh import st_autorefresh
import sqlite3
import os
import yaml
from yaml.loader import SafeLoader
import time
db_path = "./chat_messages.sqlite3"
# Streamlit app configuration
st.set_page_config(
page_title="Anonymous remarks",
page_icon="🎭",
layout="wide",
initial_sidebar_state="auto",
menu_items=None
)
count = st_autorefresh(interval=10000)
# Database setup
def init_db() -> sqlite3.Connection:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
conn.commit()
return conn
# Insert a new message
def save_message(conn: sqlite3.Connection, content: str) -> None:
cursor = conn.cursor()
cursor.execute("INSERT INTO messages (content) VALUES (?)", (content,))
conn.commit()
# Retrieve all messages
def get_messages(conn):
cursor = conn.cursor()
cursor.execute("SELECT content, timestamp FROM messages ORDER BY id")
return cursor.fetchall()
# Delete all messages
def clear_messages(conn: sqlite3.Connection) -> None:
cursor = conn.cursor()
cursor.execute("DELETE FROM messages")
conn.commit()
# Check database file size
def is_db_size_exceeded(file_path: str, size_limit_mb: int = 1024) -> bool:
assert os.path.exists(file_path)
file_size_mb = os.path.getsize(file_path) / (1024 * 1024)
return file_size_mb > size_limit_mb
# Initialize database
connection = init_db()
# Load authenticator config
# This is bad practice as it exposes the hash in GitHub.
# This info should be stored in streamlit secrets.
with open('./config.yml') as file:
config = yaml.load(file, Loader=SafeLoader)
authenticator = stauth.authenticate.Authenticate(
config['credentials'],
config['cookie']['name'],
config['cookie']['key'],
config['cookie']['expiry_days'],
)
# Initialize session state for authentication and message refreshing
if "authentication_status" not in st.session_state:
st.session_state["authentication_status"] = None
st.session_state["username"] = None
st.session_state["name"] = None
if "clear_messages_trigger" not in st.session_state:
st.session_state["clear_messages_trigger"] = False
if "last_refresh_time" not in st.session_state:
st.session_state["last_refresh_time"] = time.time()
# Login form
if st.session_state["authentication_status"] is None:
name, authentication_status, username = authenticator.login()
if authentication_status:
st.session_state["authentication_status"] = True
st.session_state["username"] = username
st.session_state["name"] = name
elif authentication_status is False:
st.error("Username/password is incorrect")
else:
name = st.session_state["name"]
username = st.session_state["username"]
# Main application
if st.session_state["authentication_status"]:
st.title("Anonymous remarks 🎭")
# Admin-specific functionality
if username.lower() == "admin":
if st.button("Clear all messages"):
clear_messages(connection)
st.success("All messages have been cleared.")
st.session_state["clear_messages_trigger"] = True
# Chat input
user_message = st.chat_input("Type your message here...")
# Check file size and save message if within limit
if user_message:
if is_db_size_exceeded(db_path):
st.warning("Database size has exceeded 1 GB. New messages cannot be added.")
else:
save_message(connection, user_message)
# Refresh messages if triggered or after a set time interval
if (
st.session_state["clear_messages_trigger"]
or time.time() - st.session_state["last_refresh_time"] > 5
):
st.session_state["clear_messages_trigger"] = False
st.session_state["last_refresh_time"] = time.time()
# Display all messages
messages = get_messages(connection)
for message_content, timestamp in messages:
st.chat_message("user").markdown(f"**{message_content}** \n*{timestamp}*")
elif st.session_state["authentication_status"] is False:
st.error("Username/password is incorrect")