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