-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.py
More file actions
113 lines (94 loc) · 3.18 KB
/
Copy pathdatabase.py
File metadata and controls
113 lines (94 loc) · 3.18 KB
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
# database.py
import psycopg2
from datetime import datetime
import streamlit as st
# Database configuration
DATABASE_URL = st.secrets["DATABASE_URL"]
# Function to connect to the PostgreSQL database
def get_db_connection():
conn = psycopg2.connect(DATABASE_URL)
return conn
# Initialize database and create tables if they do not exist
def initialize_database():
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS conversations (
id SERIAL PRIMARY KEY,
user_name VARCHAR(255),
timestamp TIMESTAMP,
conversation_id VARCHAR(255),
role VARCHAR(10),
content TEXT
);
""")
conn.commit()
cursor.close()
conn.close()
# Save a message to the database
def save_message(user_name, conversation_id, role, content):
conn = get_db_connection()
cursor = conn.cursor()
timestamp = datetime.now()
cursor.execute(
"""
INSERT INTO conversations (user_name, timestamp, conversation_id, role, content)
VALUES (%s, %s, %s, %s, %s);
""",
(user_name, timestamp, conversation_id, role, content),
)
conn.commit()
cursor.close()
conn.close()
# Retrieve conversations by date range and optional filters for user name or conversation ID
def retrieve_conversations_by_filters(
start_date, end_date, user_name=None, conversation_id=None
):
conn = get_db_connection()
cursor = conn.cursor()
# Ensure start_date and end_date include time component
start_datetime = datetime.combine(start_date, datetime.min.time())
end_datetime = datetime.combine(end_date, datetime.max.time())
query = """
SELECT user_name, timestamp, conversation_id, role, content
FROM conversations
WHERE timestamp BETWEEN %s AND %s
"""
params = [start_datetime, end_datetime]
if user_name:
query += " AND user_name ILIKE %s"
params.append(f"%{user_name}%")
if conversation_id:
query += " AND conversation_id = %s"
params.append(conversation_id)
query += " ORDER BY conversation_id, timestamp;"
cursor.execute(query, params)
results = cursor.fetchall()
conn.close()
return results
def check_password():
"""Returns `True` if the user has entered the correct password."""
def password_entered():
"""Checks whether the entered password is correct."""
st.session_state["password_correct"] = (
st.session_state["password"] == st.secrets["password"]
)
if "password_correct" not in st.session_state:
# First run, show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
st.write(
"*Please contact David Liebovitz, MD if you need an updated password for access.*"
)
return False
elif not st.session_state["password_correct"]:
# Password not correct, show input + error.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
st.error("😕 Password incorrect")
return False
else:
# Password correct.
return True