1MR commited on
Commit
bf9e95f
·
verified ·
1 Parent(s): cab3b34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -16
app.py CHANGED
@@ -121,28 +121,47 @@ def main():
121
  st.warning("Please enter a valid HuggingFace API token.")
122
  return
123
 
 
 
 
 
 
 
 
124
  user_question = st.text_input("Ask a question about your documents:")
125
  if user_question:
126
- handle_userinput(user_question)
 
 
 
127
 
 
128
  docs = st.file_uploader("Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
129
  if st.button("Process"):
130
  with st.spinner("Processing"):
131
- doc_list = []
132
- for file in docs:
133
- if file.type == 'text/plain':
134
- doc_list.extend(get_text_file(file))
135
- elif file.type in ['application/octet-stream', 'application/pdf']:
136
- doc_list.extend(get_pdf_text(file))
137
- elif file.type == 'text/csv':
138
- doc_list.extend(get_csv_file(file))
139
- elif file.type == 'application/json':
140
- doc_list.extend(get_json_file(file))
141
-
142
- text_chunks = get_text_chunks(doc_list)
143
- vectorstore = get_vectorstore(text_chunks)
144
-
145
- st.session_state.conversation = get_conversation_chain(vectorstore, tokenH)
 
 
 
 
 
 
 
 
146
 
147
 
148
  if __name__ == '__main__':
 
121
  st.warning("Please enter a valid HuggingFace API token.")
122
  return
123
 
124
+ # Initialize session state variables
125
+ if "conversation" not in st.session_state:
126
+ st.session_state.conversation = None
127
+ if "chat_history" not in st.session_state:
128
+ st.session_state.chat_history = []
129
+
130
+ # User input for questions
131
  user_question = st.text_input("Ask a question about your documents:")
132
  if user_question:
133
+ if st.session_state.conversation:
134
+ handle_userinput(user_question)
135
+ else:
136
+ st.warning("Please upload and process files first!")
137
 
138
+ # File uploader and processing
139
  docs = st.file_uploader("Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
140
  if st.button("Process"):
141
  with st.spinner("Processing"):
142
+ if docs:
143
+ doc_list = []
144
+ for file in docs:
145
+ if file.type == 'text/plain':
146
+ doc_list.extend(get_text_file(file))
147
+ elif file.type in ['application/octet-stream', 'application/pdf']:
148
+ doc_list.extend(get_pdf_text(file))
149
+ elif file.type == 'text/csv':
150
+ doc_list.extend(get_csv_file(file))
151
+ elif file.type == 'application/json':
152
+ doc_list.extend(get_json_file(file))
153
+
154
+ # Generate text chunks
155
+ text_chunks = get_text_chunks(doc_list)
156
+
157
+ # Create vector store
158
+ vectorstore = get_vectorstore(text_chunks)
159
+
160
+ # Initialize conversation chain
161
+ st.session_state.conversation = get_conversation_chain(vectorstore, tokenH)
162
+ st.success("Documents processed successfully!")
163
+ else:
164
+ st.warning("Please upload at least one document to process.")
165
 
166
 
167
  if __name__ == '__main__':