Spaces:
Runtime error
Runtime error
Remove api key from app.py
Browse files
app.py
CHANGED
|
@@ -1,245 +1,243 @@
|
|
| 1 |
-
from dotenv import load_dotenv
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import streamlit as st
|
| 4 |
-
import streamlit_authenticator as stauth
|
| 5 |
-
from streamlit_modal import Modal
|
| 6 |
-
|
| 7 |
-
from utils import new_file, clear_memory, append_documentation_to_sidebar, load_authenticator_config, init_qa, \
|
| 8 |
-
append_header
|
| 9 |
-
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
| 10 |
-
from haystack import Document
|
| 11 |
-
|
| 12 |
-
load_dotenv()
|
| 13 |
-
|
| 14 |
-
OPENAI_MODELS = ['gpt-3.5-turbo',
|
| 15 |
-
"gpt-4",
|
| 16 |
-
"gpt-4-1106-preview"]
|
| 17 |
-
|
| 18 |
-
OPEN_MODELS = [
|
| 19 |
-
'mistralai/Mistral-7B-Instruct-v0.1',
|
| 20 |
-
'HuggingFaceH4/zephyr-7b-beta'
|
| 21 |
-
]
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def reset_chat_memory():
|
| 25 |
-
st.button(
|
| 26 |
-
'Reset chat memory',
|
| 27 |
-
key="reset-memory-button",
|
| 28 |
-
on_click=clear_memory,
|
| 29 |
-
help="Clear the conversational memory. Currently implemented to retain the 4 most recent messages.",
|
| 30 |
-
disabled=False)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def manage_files(modal, document_store):
|
| 34 |
-
open_modal = st.sidebar.button("Manage Files", use_container_width=True)
|
| 35 |
-
if open_modal:
|
| 36 |
-
modal.open()
|
| 37 |
-
|
| 38 |
-
if modal.is_open():
|
| 39 |
-
with modal.container():
|
| 40 |
-
uploaded_file = st.file_uploader(
|
| 41 |
-
"Upload a document in PDF format",
|
| 42 |
-
type=("pdf",),
|
| 43 |
-
on_change=new_file(),
|
| 44 |
-
disabled=st.session_state['document_qa_model'] is None,
|
| 45 |
-
label_visibility="collapsed",
|
| 46 |
-
help="The document is used to answer your questions. The system will process the document and store it in a RAG to answer your questions.",
|
| 47 |
-
)
|
| 48 |
-
edited_df = st.data_editor(use_container_width=True, data=st.session_state['files'],
|
| 49 |
-
num_rows='dynamic',
|
| 50 |
-
column_order=['name', 'size', 'is_active'],
|
| 51 |
-
column_config={'name': {'editable': False}, 'size': {'editable': False},
|
| 52 |
-
'is_active': {'editable': True, 'type': 'checkbox',
|
| 53 |
-
'width': 100}}
|
| 54 |
-
)
|
| 55 |
-
st.session_state['files'] = pd.DataFrame(columns=['name', 'content', 'size', 'is_active'])
|
| 56 |
-
|
| 57 |
-
if uploaded_file:
|
| 58 |
-
st.session_state['file_uploaded'] = True
|
| 59 |
-
st.session_state['files'] = pd.concat([st.session_state['files'], edited_df])
|
| 60 |
-
with st.spinner('Processing the document...'):
|
| 61 |
-
store_file_in_table(document_store, uploaded_file)
|
| 62 |
-
ingest_document(uploaded_file)
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def ingest_document(uploaded_file):
|
| 66 |
-
if not st.session_state['document_qa_model']:
|
| 67 |
-
st.warning('Please select a model to start asking questions')
|
| 68 |
-
else:
|
| 69 |
-
try:
|
| 70 |
-
st.session_state['document_qa_model'].ingest_pdf(uploaded_file)
|
| 71 |
-
st.success('Document processed successfully')
|
| 72 |
-
except Exception as e:
|
| 73 |
-
st.error(f"Error processing the document: {e}")
|
| 74 |
-
st.session_state['file_uploaded'] = False
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
def store_file_in_table(document_store, uploaded_file):
|
| 78 |
-
pdf_content = uploaded_file.getvalue()
|
| 79 |
-
st.session_state['pdf_content'] = pdf_content
|
| 80 |
-
st.session_state.messages = []
|
| 81 |
-
document = Document(content=pdf_content, meta={"name": uploaded_file.name})
|
| 82 |
-
df = pd.DataFrame(st.session_state['files'])
|
| 83 |
-
df['is_active'] = False
|
| 84 |
-
st.session_state['files'] = pd.concat([df, pd.DataFrame(
|
| 85 |
-
[{"name": uploaded_file.name, "content": pdf_content, "size": len(pdf_content),
|
| 86 |
-
"is_active": True}])])
|
| 87 |
-
document_store.write_documents([document])
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
def init_session_state():
|
| 91 |
-
st.session_state.setdefault('files', pd.DataFrame(columns=['name', 'content', 'size', 'is_active']))
|
| 92 |
-
st.session_state.setdefault('models', [])
|
| 93 |
-
st.session_state.setdefault('api_keys', {})
|
| 94 |
-
st.session_state.setdefault('current_selected_model', 'gpt-3.5-turbo')
|
| 95 |
-
st.session_state.setdefault('current_api_key', '')
|
| 96 |
-
st.session_state.setdefault('messages', [])
|
| 97 |
-
st.session_state.setdefault('pdf_content', None)
|
| 98 |
-
st.session_state.setdefault('memory', None)
|
| 99 |
-
st.session_state.setdefault('pdf', None)
|
| 100 |
-
st.session_state.setdefault('document_qa_model', None)
|
| 101 |
-
st.session_state.setdefault('file_uploaded', False)
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
def set_page_config():
|
| 105 |
-
st.set_page_config(
|
| 106 |
-
page_title="Document Insights AI Assistant",
|
| 107 |
-
page_icon=":shark:",
|
| 108 |
-
initial_sidebar_state="expanded",
|
| 109 |
-
layout="wide",
|
| 110 |
-
menu_items={
|
| 111 |
-
'Get Help': 'https://www.extremelycoolapp.com/help',
|
| 112 |
-
'Report a bug': "https://www.extremelycoolapp.com/bug",
|
| 113 |
-
'About': "# This is a header. This is an *extremely* cool app!"
|
| 114 |
-
}
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
def update_running_model(api_key, model):
|
| 119 |
-
st.session_state['api_keys'][model] = api_key
|
| 120 |
-
st.session_state['document_qa_model'] = init_qa(model, api_key)
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
def init_api_key_dict():
|
| 124 |
-
# st.session_state['models'] = OPENAI_MODELS + list(OPEN_MODELS) + ['local LLM']
|
| 125 |
-
st.session_state['models'] = OPENAI_MODELS
|
| 126 |
-
for model_name in OPENAI_MODELS:
|
| 127 |
-
st.session_state['api_keys'][model_name] = None
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
def display_chat_messages(chat_box, chat_input):
|
| 131 |
-
with chat_box:
|
| 132 |
-
if chat_input:
|
| 133 |
-
for message in st.session_state.messages:
|
| 134 |
-
with st.chat_message(message["role"]):
|
| 135 |
-
st.markdown(message["content"], unsafe_allow_html=True)
|
| 136 |
-
|
| 137 |
-
st.chat_message("user").markdown(chat_input)
|
| 138 |
-
with st.chat_message("assistant"):
|
| 139 |
-
# process user input and generate response
|
| 140 |
-
response = st.session_state['document_qa_model'].inference(chat_input, st.session_state.messages)
|
| 141 |
-
|
| 142 |
-
st.markdown(response)
|
| 143 |
-
st.session_state.messages.append({"role": "user", "content": chat_input})
|
| 144 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
def setup_model_selection():
|
| 148 |
-
model = st.selectbox(
|
| 149 |
-
"Model:",
|
| 150 |
-
options=st.session_state['models'],
|
| 151 |
-
index=0, # default to the first model in the list gpt-3.5-turbo
|
| 152 |
-
placeholder="Select model",
|
| 153 |
-
help="Select an LLM:"
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
-
if model:
|
| 157 |
-
if model != st.session_state['current_selected_model']:
|
| 158 |
-
st.session_state['current_selected_model'] = model
|
| 159 |
-
if model == 'local LLM':
|
| 160 |
-
st.session_state['document_qa_model'] = init_qa(model)
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
api_key
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
task_options = ['Extractive'
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
self.
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
self.authenticator_config['
|
| 208 |
-
self.authenticator_config['cookie']['
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
elif st.session_state["authentication_status"] is
|
| 234 |
-
st.
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
app = StreamlitApp()
|
| 245 |
-
app.run()
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import streamlit_authenticator as stauth
|
| 5 |
+
from streamlit_modal import Modal
|
| 6 |
+
|
| 7 |
+
from utils import new_file, clear_memory, append_documentation_to_sidebar, load_authenticator_config, init_qa, \
|
| 8 |
+
append_header
|
| 9 |
+
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
| 10 |
+
from haystack import Document
|
| 11 |
+
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
OPENAI_MODELS = ['gpt-3.5-turbo',
|
| 15 |
+
"gpt-4",
|
| 16 |
+
"gpt-4-1106-preview"]
|
| 17 |
+
|
| 18 |
+
OPEN_MODELS = [
|
| 19 |
+
'mistralai/Mistral-7B-Instruct-v0.1',
|
| 20 |
+
'HuggingFaceH4/zephyr-7b-beta'
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def reset_chat_memory():
|
| 25 |
+
st.button(
|
| 26 |
+
'Reset chat memory',
|
| 27 |
+
key="reset-memory-button",
|
| 28 |
+
on_click=clear_memory,
|
| 29 |
+
help="Clear the conversational memory. Currently implemented to retain the 4 most recent messages.",
|
| 30 |
+
disabled=False)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def manage_files(modal, document_store):
|
| 34 |
+
open_modal = st.sidebar.button("Manage Files", use_container_width=True)
|
| 35 |
+
if open_modal:
|
| 36 |
+
modal.open()
|
| 37 |
+
|
| 38 |
+
if modal.is_open():
|
| 39 |
+
with modal.container():
|
| 40 |
+
uploaded_file = st.file_uploader(
|
| 41 |
+
"Upload a document in PDF format",
|
| 42 |
+
type=("pdf",),
|
| 43 |
+
on_change=new_file(),
|
| 44 |
+
disabled=st.session_state['document_qa_model'] is None,
|
| 45 |
+
label_visibility="collapsed",
|
| 46 |
+
help="The document is used to answer your questions. The system will process the document and store it in a RAG to answer your questions.",
|
| 47 |
+
)
|
| 48 |
+
edited_df = st.data_editor(use_container_width=True, data=st.session_state['files'],
|
| 49 |
+
num_rows='dynamic',
|
| 50 |
+
column_order=['name', 'size', 'is_active'],
|
| 51 |
+
column_config={'name': {'editable': False}, 'size': {'editable': False},
|
| 52 |
+
'is_active': {'editable': True, 'type': 'checkbox',
|
| 53 |
+
'width': 100}}
|
| 54 |
+
)
|
| 55 |
+
st.session_state['files'] = pd.DataFrame(columns=['name', 'content', 'size', 'is_active'])
|
| 56 |
+
|
| 57 |
+
if uploaded_file:
|
| 58 |
+
st.session_state['file_uploaded'] = True
|
| 59 |
+
st.session_state['files'] = pd.concat([st.session_state['files'], edited_df])
|
| 60 |
+
with st.spinner('Processing the document...'):
|
| 61 |
+
store_file_in_table(document_store, uploaded_file)
|
| 62 |
+
ingest_document(uploaded_file)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def ingest_document(uploaded_file):
|
| 66 |
+
if not st.session_state['document_qa_model']:
|
| 67 |
+
st.warning('Please select a model to start asking questions')
|
| 68 |
+
else:
|
| 69 |
+
try:
|
| 70 |
+
st.session_state['document_qa_model'].ingest_pdf(uploaded_file)
|
| 71 |
+
st.success('Document processed successfully')
|
| 72 |
+
except Exception as e:
|
| 73 |
+
st.error(f"Error processing the document: {e}")
|
| 74 |
+
st.session_state['file_uploaded'] = False
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def store_file_in_table(document_store, uploaded_file):
|
| 78 |
+
pdf_content = uploaded_file.getvalue()
|
| 79 |
+
st.session_state['pdf_content'] = pdf_content
|
| 80 |
+
st.session_state.messages = []
|
| 81 |
+
document = Document(content=pdf_content, meta={"name": uploaded_file.name})
|
| 82 |
+
df = pd.DataFrame(st.session_state['files'])
|
| 83 |
+
df['is_active'] = False
|
| 84 |
+
st.session_state['files'] = pd.concat([df, pd.DataFrame(
|
| 85 |
+
[{"name": uploaded_file.name, "content": pdf_content, "size": len(pdf_content),
|
| 86 |
+
"is_active": True}])])
|
| 87 |
+
document_store.write_documents([document])
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def init_session_state():
|
| 91 |
+
st.session_state.setdefault('files', pd.DataFrame(columns=['name', 'content', 'size', 'is_active']))
|
| 92 |
+
st.session_state.setdefault('models', [])
|
| 93 |
+
st.session_state.setdefault('api_keys', {})
|
| 94 |
+
st.session_state.setdefault('current_selected_model', 'gpt-3.5-turbo')
|
| 95 |
+
st.session_state.setdefault('current_api_key', '')
|
| 96 |
+
st.session_state.setdefault('messages', [])
|
| 97 |
+
st.session_state.setdefault('pdf_content', None)
|
| 98 |
+
st.session_state.setdefault('memory', None)
|
| 99 |
+
st.session_state.setdefault('pdf', None)
|
| 100 |
+
st.session_state.setdefault('document_qa_model', None)
|
| 101 |
+
st.session_state.setdefault('file_uploaded', False)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def set_page_config():
|
| 105 |
+
st.set_page_config(
|
| 106 |
+
page_title="Document Insights AI Assistant",
|
| 107 |
+
page_icon=":shark:",
|
| 108 |
+
initial_sidebar_state="expanded",
|
| 109 |
+
layout="wide",
|
| 110 |
+
menu_items={
|
| 111 |
+
'Get Help': 'https://www.extremelycoolapp.com/help',
|
| 112 |
+
'Report a bug': "https://www.extremelycoolapp.com/bug",
|
| 113 |
+
'About': "# This is a header. This is an *extremely* cool app!"
|
| 114 |
+
}
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def update_running_model(api_key, model):
|
| 119 |
+
st.session_state['api_keys'][model] = api_key
|
| 120 |
+
st.session_state['document_qa_model'] = init_qa(model, api_key)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def init_api_key_dict():
|
| 124 |
+
# st.session_state['models'] = OPENAI_MODELS + list(OPEN_MODELS) + ['local LLM']
|
| 125 |
+
st.session_state['models'] = OPENAI_MODELS
|
| 126 |
+
for model_name in OPENAI_MODELS:
|
| 127 |
+
st.session_state['api_keys'][model_name] = None
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def display_chat_messages(chat_box, chat_input):
|
| 131 |
+
with chat_box:
|
| 132 |
+
if chat_input:
|
| 133 |
+
for message in st.session_state.messages:
|
| 134 |
+
with st.chat_message(message["role"]):
|
| 135 |
+
st.markdown(message["content"], unsafe_allow_html=True)
|
| 136 |
+
|
| 137 |
+
st.chat_message("user").markdown(chat_input)
|
| 138 |
+
with st.chat_message("assistant"):
|
| 139 |
+
# process user input and generate response
|
| 140 |
+
response = st.session_state['document_qa_model'].inference(chat_input, st.session_state.messages)
|
| 141 |
+
|
| 142 |
+
st.markdown(response)
|
| 143 |
+
st.session_state.messages.append({"role": "user", "content": chat_input})
|
| 144 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def setup_model_selection():
|
| 148 |
+
model = st.selectbox(
|
| 149 |
+
"Model:",
|
| 150 |
+
options=st.session_state['models'],
|
| 151 |
+
index=0, # default to the first model in the list gpt-3.5-turbo
|
| 152 |
+
placeholder="Select model",
|
| 153 |
+
help="Select an LLM:"
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
if model:
|
| 157 |
+
if model != st.session_state['current_selected_model']:
|
| 158 |
+
st.session_state['current_selected_model'] = model
|
| 159 |
+
if model == 'local LLM':
|
| 160 |
+
st.session_state['document_qa_model'] = init_qa(model)
|
| 161 |
+
|
| 162 |
+
api_key = st.sidebar.text_input("Enter LLM-authorization Key:", type="password",
|
| 163 |
+
disabled=st.session_state['current_selected_model'] == 'local LLM')
|
| 164 |
+
|
| 165 |
+
if api_key and api_key != st.session_state['current_api_key']:
|
| 166 |
+
update_running_model(api_key, model)
|
| 167 |
+
st.session_state['current_api_key'] = api_key
|
| 168 |
+
|
| 169 |
+
return model
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def setup_task_selection(model):
|
| 173 |
+
# enable extractive and generative tasks if we're using a local LLM or an OpenAI model with an API key
|
| 174 |
+
if model == 'local LLM' or st.session_state['api_keys'].get(model):
|
| 175 |
+
task_options = ['Extractive', 'Generative']
|
| 176 |
+
else:
|
| 177 |
+
task_options = ['Extractive']
|
| 178 |
+
|
| 179 |
+
task_selection = st.sidebar.radio('Select the task:', task_options)
|
| 180 |
+
|
| 181 |
+
# TODO: Add the task selection logic here (initializing the model based on the task)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def setup_page_body():
|
| 185 |
+
chat_box = st.container(height=350, border=False)
|
| 186 |
+
chat_input = st.chat_input(
|
| 187 |
+
placeholder="Upload a document to start asking questions...",
|
| 188 |
+
disabled=not st.session_state['file_uploaded'],
|
| 189 |
+
)
|
| 190 |
+
if st.session_state['file_uploaded']:
|
| 191 |
+
display_chat_messages(chat_box, chat_input)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
class StreamlitApp:
|
| 195 |
+
def __init__(self):
|
| 196 |
+
self.authenticator_config = load_authenticator_config()
|
| 197 |
+
self.document_store = InMemoryDocumentStore()
|
| 198 |
+
set_page_config()
|
| 199 |
+
self.authenticator = self.init_authenticator()
|
| 200 |
+
init_session_state()
|
| 201 |
+
init_api_key_dict()
|
| 202 |
+
|
| 203 |
+
def init_authenticator(self):
|
| 204 |
+
return stauth.Authenticate(
|
| 205 |
+
self.authenticator_config['credentials'],
|
| 206 |
+
self.authenticator_config['cookie']['name'],
|
| 207 |
+
self.authenticator_config['cookie']['key'],
|
| 208 |
+
self.authenticator_config['cookie']['expiry_days']
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
def setup_sidebar(self):
|
| 212 |
+
with st.sidebar:
|
| 213 |
+
st.sidebar.image("resources/puma.png", use_column_width=True)
|
| 214 |
+
|
| 215 |
+
# Sidebar for Task Selection
|
| 216 |
+
st.sidebar.header('Options:')
|
| 217 |
+
model = setup_model_selection()
|
| 218 |
+
# setup_task_selection(model)
|
| 219 |
+
st.divider()
|
| 220 |
+
self.authenticator.logout()
|
| 221 |
+
reset_chat_memory()
|
| 222 |
+
modal = Modal("Manage Files", key="demo-modal")
|
| 223 |
+
manage_files(modal, self.document_store)
|
| 224 |
+
st.divider()
|
| 225 |
+
append_documentation_to_sidebar()
|
| 226 |
+
|
| 227 |
+
def run(self):
|
| 228 |
+
name, authentication_status, username = self.authenticator.login()
|
| 229 |
+
if authentication_status:
|
| 230 |
+
self.run_authenticated_app()
|
| 231 |
+
elif st.session_state["authentication_status"] is False:
|
| 232 |
+
st.error('Username/password is incorrect')
|
| 233 |
+
elif st.session_state["authentication_status"] is None:
|
| 234 |
+
st.warning('Please enter your username and password')
|
| 235 |
+
|
| 236 |
+
def run_authenticated_app(self):
|
| 237 |
+
self.setup_sidebar()
|
| 238 |
+
append_header()
|
| 239 |
+
setup_page_body()
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
app = StreamlitApp()
|
| 243 |
+
app.run()
|
|
|
|
|
|