Spaces:
Running
Running
Upload app.py
Browse filesinitial upload of app - will need to fill in functions and add requirements.txt
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import streamlit_authenticator as stauth
|
3 |
+
import sqlite3
|
4 |
+
import yaml
|
5 |
+
from yaml.loader import SafeLoader
|
6 |
+
|
7 |
+
# Connect to SQLite database
|
8 |
+
conn = sqlite3.connect('user_data.db')
|
9 |
+
|
10 |
+
# Load the configuration file
|
11 |
+
with open('config.yaml') as file:
|
12 |
+
config = yaml.load(file, Loader=SafeLoader)
|
13 |
+
|
14 |
+
# Create an authenticator
|
15 |
+
authenticator = stauth.Authenticate(
|
16 |
+
config['credentials'],
|
17 |
+
config['cookie']['name'],
|
18 |
+
config['cookie']['key'],
|
19 |
+
config['cookie']['expiry_days'],
|
20 |
+
config['preauthorized']
|
21 |
+
)
|
22 |
+
|
23 |
+
# Render the login module
|
24 |
+
name, authentication_status, username = authenticator.login('Login', 'main')
|
25 |
+
|
26 |
+
# Create table for user data if it doesn't exist
|
27 |
+
conn.execute('''CREATE TABLE IF NOT EXISTS user_data
|
28 |
+
(username TEXT PRIMARY KEY,
|
29 |
+
principles TEXT,
|
30 |
+
writing_style TEXT,
|
31 |
+
sources TEXT)''')
|
32 |
+
|
33 |
+
def get_user_data(user):
|
34 |
+
cursor = conn.cursor()
|
35 |
+
cursor.execute("SELECT * FROM user_data WHERE username = ?", (user,))
|
36 |
+
data = cursor.fetchone()
|
37 |
+
return data
|
38 |
+
|
39 |
+
def update_user_data(user, principles, writing_style, sources):
|
40 |
+
conn.execute("INSERT OR REPLACE INTO user_data VALUES (?, ?, ?, ?)", (user, principles, writing_style, sources))
|
41 |
+
conn.commit()
|
42 |
+
|
43 |
+
# If the user is authenticated
|
44 |
+
if authentication_status:
|
45 |
+
authenticator.logout('Logout', 'main', key='unique_key')
|
46 |
+
st.write(f'Welcome *{name}*')
|
47 |
+
|
48 |
+
# Sidebar for navigation
|
49 |
+
page = st.sidebar.selectbox("Choose a page", ["Main screen", "Principles and sources"])
|
50 |
+
|
51 |
+
# Fetch user data from the database
|
52 |
+
user_data = get_user_data(username)
|
53 |
+
|
54 |
+
if page == "Main screen":
|
55 |
+
st.title("Main Screen")
|
56 |
+
|
57 |
+
# Input boxes
|
58 |
+
original_post = st.text_input("Paste Original Post Here")
|
59 |
+
background_info = st.text_input("Background information on original post (references, relevant information, best practices for responding)")
|
60 |
+
|
61 |
+
# Function to process inputs from box 1 and 2. You will need to replace this with your actual function.
|
62 |
+
def process_inputs(original_post, background_info, principles, writing_style, sources):
|
63 |
+
# Use principles, writing_style, and sources in your function
|
64 |
+
return f"Draft Response: {original_post}, {background_info}, {principles}, {writing_style}, {sources}"
|
65 |
+
|
66 |
+
if user_data is None:
|
67 |
+
draft_response = process_inputs(original_post, background_info, "", "", "")
|
68 |
+
else:
|
69 |
+
draft_response = process_inputs(original_post, background_info, user_data[1], user_data[2], user_data[3])
|
70 |
+
|
71 |
+
# Output from function
|
72 |
+
draft_response = process_inputs(original_post, background_info, user_data[1], user_data[2], user_data[3])
|
73 |
+
st.text_area(label="Draft Response. Please edit here or prompt suggestions in the box below.", value=draft_response, height=350)
|
74 |
+
|
75 |
+
regenerate_prompt = st.text_input("Additional prompting for regenerating draft response")
|
76 |
+
|
77 |
+
elif page == "Principles and sources":
|
78 |
+
st.title("Principles and Sources")
|
79 |
+
|
80 |
+
# Input boxes with existing data
|
81 |
+
principles = st.text_input("My Principles", value=user_data[1] if user_data else "")
|
82 |
+
writing_style = st.text_input("My Writing Style (Paste Examples)", value=user_data[2] if user_data else "")
|
83 |
+
sources = st.text_input("Sources (Provide all sources you would like to use)", value=user_data[3] if user_data else "")
|
84 |
+
|
85 |
+
# Update button
|
86 |
+
if st.button("Update"):
|
87 |
+
update_user_data(username, principles, writing_style, sources)
|
88 |
+
|
89 |
+
elif authentication_status is False:
|
90 |
+
st.error('Username/password is incorrect')
|
91 |
+
|
92 |
+
elif authentication_status is None:
|
93 |
+
st.warning('Please enter your username and password')
|
94 |
+
|
95 |
+
try:
|
96 |
+
if authenticator.register_user('Register user', preauthorization=False):
|
97 |
+
st.success('User registered successfully')
|
98 |
+
except Exception as e:
|
99 |
+
st.error(e)
|
100 |
+
|
101 |
+
with open('config.yaml', 'w') as file:
|
102 |
+
yaml.dump(config, file, default_flow_style=False)
|