vinhnx90 commited on
Commit
259cbe8
β€’
1 Parent(s): 5756365

Handle secrets management

Browse files
Files changed (2) hide show
  1. .gitignore +2 -0
  2. app.py +29 -32
.gitignore CHANGED
@@ -158,3 +158,5 @@ cython_debug/
158
  # and can be added to the global gitignore or merged into this file. For a more nuclear
159
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
  #.idea/
 
 
 
158
  # and can be added to the global gitignore or merged into this file. For a more nuclear
159
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
  #.idea/
161
+
162
+ .streamlit/secrets.toml
app.py CHANGED
@@ -47,7 +47,7 @@ def load_and_process_file(file_data):
47
  chunk_overlap=200,
48
  )
49
  chunks = text_splitter.split_documents(documents)
50
- embeddings = OpenAIEmbeddings(openai_api_key=st.session_state.api_key)
51
  vector_store = Chroma.from_documents(chunks, embeddings)
52
  return vector_store
53
 
@@ -60,7 +60,7 @@ def initialize_chat_model(vector_store):
60
  llm = ChatOpenAI(
61
  model="gpt-3.5-turbo",
62
  temperature=0,
63
- openai_api_key=st.session_state.api_key,
64
  )
65
  retriever = vector_store.as_retriever()
66
  return ConversationalRetrievalChain.from_llm(llm, retriever)
@@ -71,6 +71,15 @@ def main():
71
  The main function that runs the Streamlit app.
72
  """
73
 
 
 
 
 
 
 
 
 
 
74
  assistant_message = "Hello, you can upload a document and chat with me to ask questions related to its content. Start by adding OpenAI API Key in the sidebar."
75
  st.session_state["messages"] = [
76
  Assistant(message=assistant_message).build_message()
@@ -80,7 +89,7 @@ def main():
80
 
81
  if prompt := st.chat_input(
82
  placeholder="Chat with your document",
83
- disabled=(not st.session_state.api_key),
84
  ):
85
  st.session_state.messages.append(User(message=prompt).build_message())
86
  st.chat_message(ChatProfileRoleEnum.User).write(prompt)
@@ -112,7 +121,7 @@ def handle_question(question):
112
  with st.chat_message(ChatProfileRoleEnum.Assistant):
113
  stream_handler = StreamHandler(st.empty())
114
  llm = ChatOpenAI(
115
- openai_api_key=st.session_state.api_key,
116
  streaming=True,
117
  callbacks=[stream_handler],
118
  )
@@ -146,36 +155,24 @@ def clear_history():
146
  def build_sidebar():
147
  with st.sidebar:
148
  st.title("πŸ“š InkChatGPT")
149
-
150
- openai_api_key = st.text_input(
151
- "OpenAI API Key",
152
- type="password",
153
- placeholder="Enter your OpenAI API key",
154
  )
155
 
156
- st.session_state.api_key = openai_api_key
157
-
158
- if not openai_api_key:
159
- st.info("Please add your OpenAI API key to continue.")
160
- else:
161
- uploaded_file = st.file_uploader(
162
- "Select a file", type=["pdf", "docx", "txt"], key="file_uploader"
163
- )
164
-
165
- add_file = st.button(
166
- "Process File",
167
- disabled=(not uploaded_file and not openai_api_key),
168
- )
169
- if add_file and uploaded_file and openai_api_key.startswith("sk-"):
170
- with st.spinner("πŸ’­ Thinking..."):
171
- vector_store = load_and_process_file(uploaded_file)
172
-
173
- if vector_store:
174
- crc = initialize_chat_model(vector_store)
175
- st.session_state.crc = crc
176
- st.chat_message(ChatProfileRoleEnum.Assistant).write(
177
- f"File: `{uploaded_file.name}`, processed successfully!"
178
- )
179
 
180
 
181
  if __name__ == "__main__":
 
47
  chunk_overlap=200,
48
  )
49
  chunks = text_splitter.split_documents(documents)
50
+ embeddings = OpenAIEmbeddings(openai_api_key=st.secrets.openai_api_key)
51
  vector_store = Chroma.from_documents(chunks, embeddings)
52
  return vector_store
53
 
 
60
  llm = ChatOpenAI(
61
  model="gpt-3.5-turbo",
62
  temperature=0,
63
+ openai_api_key=st.secrets.openai_api_key,
64
  )
65
  retriever = vector_store.as_retriever()
66
  return ConversationalRetrievalChain.from_llm(llm, retriever)
 
71
  The main function that runs the Streamlit app.
72
  """
73
 
74
+ if "openai_api_key" in st.secrets:
75
+ openai_api_key = st.secrets.openai_api_key
76
+ else:
77
+ openai_api_key = st.sidebar.text_input("OpenAI API Key", type="password")
78
+ st.secrets.openai_api_key = openai_api_key
79
+
80
+ if not st.secrets.openai_api_key:
81
+ st.info("Please add your OpenAI API key to continue.")
82
+
83
  assistant_message = "Hello, you can upload a document and chat with me to ask questions related to its content. Start by adding OpenAI API Key in the sidebar."
84
  st.session_state["messages"] = [
85
  Assistant(message=assistant_message).build_message()
 
89
 
90
  if prompt := st.chat_input(
91
  placeholder="Chat with your document",
92
+ disabled=(not st.secrets.openai_api_key),
93
  ):
94
  st.session_state.messages.append(User(message=prompt).build_message())
95
  st.chat_message(ChatProfileRoleEnum.User).write(prompt)
 
121
  with st.chat_message(ChatProfileRoleEnum.Assistant):
122
  stream_handler = StreamHandler(st.empty())
123
  llm = ChatOpenAI(
124
+ openai_api_key=st.secrets.openai_api_key,
125
  streaming=True,
126
  callbacks=[stream_handler],
127
  )
 
155
  def build_sidebar():
156
  with st.sidebar:
157
  st.title("πŸ“š InkChatGPT")
158
+ uploaded_file = st.file_uploader(
159
+ "Select a file", type=["pdf", "docx", "txt"], key="file_uploader"
 
 
 
160
  )
161
 
162
+ add_file = st.button(
163
+ "Process File",
164
+ disabled=(not uploaded_file and not st.secrets.openai_api_key),
165
+ )
166
+ if add_file and uploaded_file and st.secrets.openai_api_key.startswith("sk-"):
167
+ with st.spinner("πŸ’­ Thinking..."):
168
+ vector_store = load_and_process_file(uploaded_file)
169
+
170
+ if vector_store:
171
+ crc = initialize_chat_model(vector_store)
172
+ st.session_state.crc = crc
173
+ st.chat_message(ChatProfileRoleEnum.Assistant).write(
174
+ f"File: `{uploaded_file.name}`, processed successfully!"
175
+ )
 
 
 
 
 
 
 
 
 
176
 
177
 
178
  if __name__ == "__main__":