Spaces:
Running
Running
Charreau Bell, Ph.D
commited on
Commit
•
ae18f92
1
Parent(s):
5299f28
Update deployUItoHF.yaml
Browse files- app.py +57 -172
- requirements.txt +11 -12
app.py
CHANGED
@@ -1,29 +1,20 @@
|
|
1 |
import streamlit as st
|
2 |
import streamlit_authenticator as stauth
|
3 |
-
|
4 |
import yaml
|
5 |
from yaml.loader import SafeLoader
|
6 |
import os
|
7 |
-
from cryptography.fernet import Fernet
|
8 |
|
9 |
from free_speech_app.DataLoadDb import *
|
10 |
from free_speech_app.FreeSpeechPromptsResponses import *
|
11 |
from langchain.chat_models import ChatOpenAI
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
db = deta.Base("user_data")
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
#activeloop_token
|
21 |
-
os.environ["ACTIVELOOP_TOKEN"] = st.secrets["deeplake_key"]
|
22 |
-
|
23 |
-
config_drive = deta.Drive("passwords")
|
24 |
-
config = config_drive.get("config.yaml").read()
|
25 |
-
|
26 |
-
config = yaml.load(config, Loader=SafeLoader)
|
27 |
|
28 |
# Create an authenticator
|
29 |
authenticator = stauth.Authenticate(
|
@@ -34,23 +25,25 @@ authenticator = stauth.Authenticate(
|
|
34 |
config['preauthorized']
|
35 |
)
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
for person in data:
|
40 |
-
if person['key'] == user:
|
41 |
-
return person
|
42 |
-
return None
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
|
48 |
-
def
|
49 |
-
|
50 |
-
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
|
|
54 |
|
55 |
# If the user is authenticated
|
56 |
if authentication_status:
|
@@ -58,157 +51,51 @@ if authentication_status:
|
|
58 |
st.write(f'Welcome *{name}*')
|
59 |
|
60 |
# Sidebar for navigation
|
61 |
-
page = st.sidebar.
|
62 |
|
63 |
# Fetch user data from the database
|
64 |
user_data = get_user_data(username)
|
65 |
|
66 |
-
if page == "
|
67 |
-
|
68 |
-
st.title("Account Setup")
|
69 |
-
st.markdown("Please use this page to provide your OpenAI API Key, Principles and Writing Style. **Please make sure to press the Save Changes button after providing the information.**")
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
st.session_state.api_key = ""
|
76 |
-
api_input = st.text_input("OpenAI API Key", value=decrypt(user_data["api_key"].encode()[2:-1], fernet) if user_data and "api_key" in user_data else "", type="password")
|
77 |
-
encrypted_api_key = str(encrypt(api_input, fernet))
|
78 |
-
st.session_state.api_key = api_input
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
st.
|
92 |
-
st.write('An API (Application Programming Interface) key is like a password that allows you to access certain functions or data from a website or service. Many sites use API keys to identify you and control access to their APIs.')
|
93 |
-
|
94 |
-
st.header('Why do you need an API key?')
|
95 |
-
st.write('API keys allow sites to track usage and prevent abuse of their services. They help keep things secure. When you request an API key, the site knows the calls are coming from you.')
|
96 |
-
|
97 |
-
image = 'apikeyex.png'
|
98 |
-
st.header('How to get an OpenAI API key:')
|
99 |
-
st.write('1. Go to https://platform.openai.com/account/api-keys')
|
100 |
-
st.write('2. Log in or create an OpenAI account if you do not have one')
|
101 |
-
st.write('3. Click "Create new secret key" and give your key a name')
|
102 |
-
st.image(image, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
|
103 |
-
st.write('4. Copy the generated API key and keep it private like a password')
|
104 |
-
|
105 |
-
st.header('Using your API key')
|
106 |
-
st.write('When making calls to the OpenAI API, include your API key in the request headers or parameters to authenticate.')
|
107 |
-
st.code('headers = {"Authorization": f"Bearer {YOUR_API_KEY}"}')
|
108 |
|
109 |
-
st.
|
110 |
|
|
|
|
|
111 |
|
112 |
-
|
113 |
-
st.
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
# Input boxes
|
118 |
|
119 |
-
|
120 |
-
|
|
|
121 |
|
122 |
-
with left_col:
|
123 |
-
original_post = st.text_area("Paste Original Post Here \n", height=100)
|
124 |
-
|
125 |
-
chat_mdl = None
|
126 |
-
draft_response = ''
|
127 |
-
|
128 |
-
# Check if the "Submit" button is clicked
|
129 |
-
if st.button("Submit"):
|
130 |
-
if st.session_state.api_key:
|
131 |
-
os.environ["OPENAI_API_KEY"] = st.session_state.api_key
|
132 |
-
# add condition to check for passphrase to allow use of DSI api key stored in secrets
|
133 |
-
if (os.environ["OPENAI_API_KEY"] == st.secrets["secret_passphrase"]):
|
134 |
-
os.environ["OPENAI_API_KEY"] = st.secrets["dsi_openai_key"]
|
135 |
-
chat_mdl = ChatOpenAI(model_name='gpt-4', temperature=0.1)
|
136 |
-
|
137 |
-
if chat_mdl is not None:
|
138 |
-
if user_data is None:
|
139 |
-
draft_response, background_text, sources_text = generate_custom_response(original_post, chat_mdl, "", "")
|
140 |
-
st.session_state.draft_response = draft_response.content
|
141 |
-
st.session_state.background_text = background_text
|
142 |
-
st.session_state.sources_text = sources_text
|
143 |
-
st.session_state.background_info = background_text
|
144 |
-
st.session_state.sources = sources_text
|
145 |
-
st.rerun()
|
146 |
-
else:
|
147 |
-
draft_response, background_text, sources_text = generate_custom_response(original_post, chat_mdl, user_data['principles'], user_data['writing_style'])
|
148 |
-
st.session_state.draft_response = draft_response.content
|
149 |
-
st.session_state.background_text = background_text
|
150 |
-
st.session_state.sources_text = sources_text
|
151 |
-
st.session_state.background_info = background_text
|
152 |
-
st.session_state.sources = sources_text
|
153 |
-
st.rerun()
|
154 |
-
|
155 |
-
|
156 |
-
# Ensure session state variables are initialized
|
157 |
-
if 'draft_response' not in st.session_state:
|
158 |
-
st.session_state.draft_response = ''
|
159 |
-
if 'regenerate_prompt' not in st.session_state:
|
160 |
-
st.session_state.regenerate_prompt = ''
|
161 |
-
|
162 |
-
# Output from function
|
163 |
-
response_textarea = st.text_area(
|
164 |
-
label="Draft Response. Please edit here or prompt suggestions in the box below.",
|
165 |
-
value=st.session_state.draft_response if 'draft_response' in st.session_state else '',
|
166 |
-
height=350,
|
167 |
-
key='draft_response_key'
|
168 |
-
)
|
169 |
-
|
170 |
-
# Initialization of the regeneration flag
|
171 |
-
if 'is_regenerating' not in st.session_state:
|
172 |
-
st.session_state.is_regenerating = False
|
173 |
-
|
174 |
-
# Check if the app is in the "regeneration" phase
|
175 |
-
if st.session_state.is_regenerating:
|
176 |
-
# Display the regenerated response explicitly
|
177 |
-
regenerate_prompt = st.text_area(
|
178 |
-
"Request a new draft",
|
179 |
-
value=st.session_state.regenerate_prompt,
|
180 |
-
placeholder="You may edit the regenerated draft directly above, or request further changes here.",
|
181 |
-
height=100,
|
182 |
-
key='regenerate_prompt_key'
|
183 |
-
)
|
184 |
-
# Reset the regeneration flag
|
185 |
-
st.session_state.is_regenerating = False
|
186 |
-
else:
|
187 |
-
# Normal behavior: display the text area for manual input
|
188 |
-
regenerate_prompt = st.text_area(
|
189 |
-
"Request a new draft",
|
190 |
-
placeholder="You may edit the draft directly above, or request a new draft with additional guidance here.",
|
191 |
-
height=100,
|
192 |
-
key='regenerate_prompt_key'
|
193 |
-
)
|
194 |
-
|
195 |
-
if (draft_response is not None) and (regenerate_prompt is not None):
|
196 |
-
if st.button("Regenerate"):
|
197 |
-
if st.session_state.api_key:
|
198 |
-
os.environ['OPENAI_API_KEY'] = st.session_state.api_key
|
199 |
-
# add condition to check for passphrase to allow use of DSI api key stored in secrets
|
200 |
-
if (os.environ["OPENAI_API_KEY"] == st.secrets["secret_passphrase"]):
|
201 |
-
os.environ["OPENAI_API_KEY"] = st.secrets["dsi_openai_key"]
|
202 |
-
chat_mdl = ChatOpenAI(model_name='gpt-4', temperature=0.1)
|
203 |
-
|
204 |
-
if chat_mdl is not None:
|
205 |
-
updated_response = regenerate_custom_response(chat_mdl, regenerate_prompt, st.session_state.draft_response).content
|
206 |
-
st.session_state.regenerate_prompt = updated_response
|
207 |
-
st.session_state.is_regenerating = True
|
208 |
-
|
209 |
-
st.rerun()
|
210 |
-
|
211 |
-
|
212 |
elif authentication_status is False:
|
213 |
st.error('Username/password is incorrect')
|
214 |
|
@@ -216,12 +103,10 @@ elif authentication_status is None:
|
|
216 |
st.warning('Please enter your username and password')
|
217 |
|
218 |
try:
|
219 |
-
if authenticator.register_user('
|
220 |
-
st.success('User
|
221 |
except Exception as e:
|
222 |
st.error(e)
|
223 |
|
224 |
with open('config.yaml', 'w') as file:
|
225 |
yaml.dump(config, file, default_flow_style=False)
|
226 |
-
|
227 |
-
config_drive.put("config.yaml", path = "config.yaml")
|
|
|
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(
|
|
|
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:
|
|
|
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 |
|
|
|
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)
|
|
|
|
requirements.txt
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
-
streamlit
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
nltk
|
|
|
1 |
+
streamlit
|
2 |
+
sqlite3
|
3 |
+
yaml
|
4 |
+
langchain
|
5 |
+
openai
|
6 |
+
chromadb
|
7 |
+
tiktoken
|
8 |
+
unstructured
|
9 |
+
getpass
|
10 |
+
nltk
|
11 |
+
free_speech_app @ git+https://github.com/vanderbilt-data-science/free-speech-app.git
|
|