eaglelandsonce commited on
Commit
4ecf027
·
1 Parent(s): 9eaa2b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -21
app.py CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
2
  from utils import *
3
 
4
 
5
- # streamlit session_state holds history, this mehtods cleas that history
6
  def clear_history():
7
  if 'history' in st.session_state:
8
  del st.session_state['history']
@@ -12,32 +12,26 @@ def clear_history():
12
  if __name__ == "__main__":
13
  import os
14
 
15
- # loading the OpenAI api key from .env
16
- #from dotenv import load_dotenv, find_dotenv
17
-
18
- #load_dotenv(find_dotenv(), override=True)
19
-
20
- # st.image('img.png')
21
-
22
  st.subheader('Load a Document and Ask a Question')
23
  with st.sidebar:
24
- # text_input for the OpenAI API key (alternative to python-dotenv and .env)
25
  api_key = st.text_input('OpenAI API Key:', type='password')
26
  if api_key:
27
  os.environ['OPENAI_API_KEY'] = api_key
28
 
29
- # file uploader widget
30
  uploaded_file = st.file_uploader('To upload a file drag and drop it on the area below:', type=['pdf', 'docx', 'txt', 'csv'])
31
 
32
- # chunk size number widget
33
  chunk_size = st.number_input('Chunk size:', min_value=100, max_value=2048, value=512, on_change=clear_history)
34
 
35
- # k number input widget
36
  k = st.number_input('k', min_value=1, max_value=20, value=3, on_change=clear_history)
37
 
38
- # add data button widget
39
  add_data = st.button('Add Data', on_click=clear_history)
40
-
41
  if add_data:
42
  if api_key:
43
  if uploaded_file and add_data: # if the user browsed a file
@@ -66,10 +60,10 @@ if __name__ == "__main__":
66
  else:
67
  st.error("Please provide your OpenAI API key above.....")
68
 
69
- # user's question text input widget
70
  q = st.text_input('Ask a question about the content of your file:')
71
- if q: # if the user entered a question and hit enter
72
- if 'vs' in st.session_state: # if there's the vector store (user uploaded, split and embedded a file)
73
  vector_store = st.session_state.vs
74
  st.write(f'k: {k}')
75
  answer = ask_and_get_answer(vector_store, q, k)
@@ -79,17 +73,16 @@ if __name__ == "__main__":
79
 
80
  st.divider()
81
 
82
- # if there's no chat history in the session state, create it
83
  if 'history' not in st.session_state:
84
  st.session_state.history = ''
85
 
86
- # the current question and answer
87
  value = f'Q: {q} \nA: {answer}'
88
 
89
  st.session_state.history = f'{value} \n {"-" * 100} \n {st.session_state.history}'
90
  h = st.session_state.history
91
 
92
- # text area widget for the chat history
93
  st.text_area(label='Chat History', value=h, key='history', height=400)
94
 
95
- # run the app: streamlit run ./chat_with_documents.py
 
2
  from utils import *
3
 
4
 
5
+ # streamlit session state holds history, this mehtods cleas that history
6
  def clear_history():
7
  if 'history' in st.session_state:
8
  del st.session_state['history']
 
12
  if __name__ == "__main__":
13
  import os
14
 
15
+ # create your side bar
 
 
 
 
 
 
16
  st.subheader('Load a Document and Ask a Question')
17
  with st.sidebar:
18
+ # use text_input to bring in your OpenAI API key
19
  api_key = st.text_input('OpenAI API Key:', type='password')
20
  if api_key:
21
  os.environ['OPENAI_API_KEY'] = api_key
22
 
23
+ # sidebar - file uploader widget, drag and drop, browse button works on windows not on mac
24
  uploaded_file = st.file_uploader('To upload a file drag and drop it on the area below:', type=['pdf', 'docx', 'txt', 'csv'])
25
 
26
+ # call the chunk size mehtod that sets the number
27
  chunk_size = st.number_input('Chunk size:', min_value=100, max_value=2048, value=512, on_change=clear_history)
28
 
29
+ # input the k number, a hight nuber will increase the search effectiveness, but is more expensive
30
  k = st.number_input('k', min_value=1, max_value=20, value=3, on_change=clear_history)
31
 
32
+ # click this sidebard button to add data
33
  add_data = st.button('Add Data', on_click=clear_history)
34
+ #chekc if data button has been clicked,if the api key is added and if a data file is available for upload
35
  if add_data:
36
  if api_key:
37
  if uploaded_file and add_data: # if the user browsed a file
 
60
  else:
61
  st.error("Please provide your OpenAI API key above.....")
62
 
63
+ # this is the main input widget that allows you to input your query of the uploaded document
64
  q = st.text_input('Ask a question about the content of your file:')
65
+ if q: # run the query if the user entered a question and hit enter
66
+ if 'vs' in st.session_state: # for seesion state, if there's the vector store (user uploaded, split and embedded a file)
67
  vector_store = st.session_state.vs
68
  st.write(f'k: {k}')
69
  answer = ask_and_get_answer(vector_store, q, k)
 
73
 
74
  st.divider()
75
 
76
+ # initialize a chat history if there's no chat history
77
  if 'history' not in st.session_state:
78
  st.session_state.history = ''
79
 
80
+ # your question and answer
81
  value = f'Q: {q} \nA: {answer}'
82
 
83
  st.session_state.history = f'{value} \n {"-" * 100} \n {st.session_state.history}'
84
  h = st.session_state.history
85
 
86
+ # chat history text area widget
87
  st.text_area(label='Chat History', value=h, key='history', height=400)
88