fracapuano commited on
Commit
0e17089
1 Parent(s): a96d162

add: toward multiple files support

Browse files
Files changed (1) hide show
  1. qa/qa.py +26 -19
qa/qa.py CHANGED
@@ -3,7 +3,7 @@ from openai.error import OpenAIError
3
  from .utils import *
4
  from typing import Text, Union
5
 
6
- multiple_files = False
7
 
8
  def clear_submit():
9
  """
@@ -52,18 +52,21 @@ def qa_main():
52
  upload_document_greenlight = False
53
  uploaded_processed_document_greenlight = False
54
  # OpenAI API Key - TODO: consider adding a key valid for everyone
55
- st.header("Configure OpenAI API Key")
56
- st.warning('Please enter your OpenAI API Key!', icon='⚠️')
57
- user_secret = st.text_input(
58
- "Insert your OpenAI API key here ([get your API key](https://platform.openai.com/account/api-keys)).",
59
- type="password",
60
- placeholder="Paste your OpenAI API key here (sk-...)",
61
- help="You can get your API key from https://platform.openai.com/account/api-keys.",
62
- value=st.session_state.get("OPENAI_API_KEY", ""),
63
- )
 
 
 
64
  if user_secret:
65
  if set_openai_api_key(user_secret):
66
- st.success('OpenAI API key successfully provided!', icon='✅')
67
  upload_document_greenlight = True
68
 
69
  if upload_document_greenlight:
@@ -74,18 +77,22 @@ def qa_main():
74
  type=["pdf", "docx", "txt", "py", "json", "html", "css", "md"],
75
  help="Scanned documents are not supported yet 🥲",
76
  on_change=clear_submit,
77
- accept_multiple_files=multiple_files,
78
  )
79
 
80
- # reading the uploaded file
81
- if uploaded_file is not None:
 
82
  # toggle internal file submission state to True
83
  st.session_state["file_submitted"] = True
84
- # parse the file using custom parsers
85
- doc = file_to_doc(uploaded_file)
86
- # converts the files into a list of documents
87
- text = text_to_docs(text=tuple(doc))
88
-
 
 
 
89
  try:
90
  with st.spinner("Indexing the document... This might take a while!"):
91
  index = embed_docs(tuple(text))
 
3
  from .utils import *
4
  from typing import Text, Union
5
 
6
+ multiple_files = True
7
 
8
  def clear_submit():
9
  """
 
52
  upload_document_greenlight = False
53
  uploaded_processed_document_greenlight = False
54
  # OpenAI API Key - TODO: consider adding a key valid for everyone
55
+ # st.header("Configure OpenAI API Key")
56
+ # st.warning('Please enter your OpenAI API Key!', icon='⚠️')
57
+
58
+ # uncomment the following lines to add a user-specific key
59
+ # user_secret = st.text_input(
60
+ # "Insert your OpenAI API key here ([get your API key](https://platform.openai.com/account/api-keys)).",
61
+ # type="password",
62
+ # placeholder="Paste your OpenAI API key here (sk-...)",
63
+ # help="You can get your API key from https://platform.openai.com/account/api-keys.",
64
+ # value=st.session_state.get("OPENAI_API_KEY", ""),
65
+ # )
66
+ user_secret = st.secrets["OPENAI_API_KEY"]
67
  if user_secret:
68
  if set_openai_api_key(user_secret):
69
+ st.success('OpenAI API key successfully accessed!', icon='✅')
70
  upload_document_greenlight = True
71
 
72
  if upload_document_greenlight:
 
77
  type=["pdf", "docx", "txt", "py", "json", "html", "css", "md"],
78
  help="Scanned documents are not supported yet 🥲",
79
  on_change=clear_submit,
80
+ accept_multiple_files=multiple_files
81
  )
82
 
83
+ # reading the uploaded files
84
+ text = []
85
+ if len(uploaded_file) != 0:
86
  # toggle internal file submission state to True
87
  st.session_state["file_submitted"] = True
88
+ for file in uploaded_file:
89
+ # parse the file using custom parsers
90
+ file_doc = file_to_doc(file)
91
+ # converts the files into a list of documents
92
+ file_text = text_to_docs(text=tuple(file_doc), file_name=file.name)
93
+ text.extend(file_text)
94
+
95
+ # embeds the documents using OpenAI API
96
  try:
97
  with st.spinner("Indexing the document... This might take a while!"):
98
  index = embed_docs(tuple(text))