cryogenic22 commited on
Commit
50ddc65
·
verified ·
1 Parent(s): b3d37d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -15
app.py CHANGED
@@ -14,6 +14,8 @@ if 'qa_system' not in st.session_state:
14
  st.session_state.qa_system = None
15
  if 'documents_initialized' not in st.session_state:
16
  st.session_state.documents_initialized = False
 
 
17
 
18
  def handle_doc_select():
19
  st.session_state.current_chat = True
@@ -35,14 +37,27 @@ def display_chat_interface():
35
 
36
  # Chat input
37
  if prompt := st.chat_input("Ask about the RFPs..."):
 
38
  st.session_state.chat_history.append(HumanMessage(content=prompt))
 
39
  try:
40
  with st.spinner("Analyzing documents..."):
 
 
 
 
 
 
 
 
41
  response = st.session_state.qa_system.invoke({
42
  "input": prompt,
43
- "chat_history": st.session_state.chat_history
44
  })
 
 
45
  st.session_state.chat_history.append(AIMessage(content=response["output"]))
 
46
  except Exception as e:
47
  st.error(f"Error generating response: {e}")
48
 
@@ -150,20 +165,33 @@ def main():
150
  )
151
 
152
  if uploaded_files:
153
- with st.spinner("Processing documents..."):
154
- all_texts, document_names, document_pages = backend.upload_and_parse_documents(uploaded_files)
155
- if all_texts:
156
- try:
157
- with conn:
158
- for doc, doc_name in zip(all_texts, document_names):
159
- conn.execute(
160
- "INSERT INTO documents (name, content) VALUES (?, ?)",
161
- (doc_name, doc)
162
- )
163
- st.success(f"Successfully uploaded {len(uploaded_files)} documents!")
164
- except Exception as e:
165
- st.error(f"Error saving documents to database: {e}")
166
-
 
 
 
 
 
 
 
 
 
 
 
 
 
167
  display_knowledge_base(conn)
168
 
169
  with col2:
 
14
  st.session_state.qa_system = None
15
  if 'documents_initialized' not in st.session_state:
16
  st.session_state.documents_initialized = False
17
+ if 'processed_files' not in st.session_state:
18
+ st.session_state.processed_files = set()
19
 
20
  def handle_doc_select():
21
  st.session_state.current_chat = True
 
37
 
38
  # Chat input
39
  if prompt := st.chat_input("Ask about the RFPs..."):
40
+ # Add user message to chat history
41
  st.session_state.chat_history.append(HumanMessage(content=prompt))
42
+
43
  try:
44
  with st.spinner("Analyzing documents..."):
45
+ # Convert chat history to the format expected by the QA system
46
+ formatted_history = []
47
+ for msg in st.session_state.chat_history[:-1]: # Exclude the current message
48
+ if isinstance(msg, HumanMessage):
49
+ formatted_history.append({"role": "user", "content": msg.content})
50
+ else:
51
+ formatted_history.append({"role": "assistant", "content": msg.content})
52
+
53
  response = st.session_state.qa_system.invoke({
54
  "input": prompt,
55
+ "chat_history": formatted_history
56
  })
57
+
58
+ # Add AI response to chat history
59
  st.session_state.chat_history.append(AIMessage(content=response["output"]))
60
+
61
  except Exception as e:
62
  st.error(f"Error generating response: {e}")
63
 
 
165
  )
166
 
167
  if uploaded_files:
168
+ # Filter out already processed files
169
+ new_files = [f for f in uploaded_files if f.name not in st.session_state.processed_files]
170
+
171
+ if new_files:
172
+ with st.spinner("Processing documents..."):
173
+ all_texts, document_names, document_pages = backend.upload_and_parse_documents(new_files)
174
+ if all_texts:
175
+ try:
176
+ with conn:
177
+ for doc, doc_name in zip(all_texts, document_names):
178
+ # Check if document already exists in database
179
+ cursor = conn.cursor()
180
+ cursor.execute("SELECT id FROM documents WHERE name = ?", (doc_name,))
181
+ existing_doc = cursor.fetchone()
182
+
183
+ if not existing_doc:
184
+ conn.execute(
185
+ "INSERT INTO documents (name, content) VALUES (?, ?)",
186
+ (doc_name, doc)
187
+ )
188
+ # Add to processed files set
189
+ st.session_state.processed_files.add(doc_name)
190
+
191
+ st.success(f"Successfully uploaded {len(new_files)} new documents!")
192
+ except Exception as e:
193
+ st.error(f"Error saving documents to database: {e}")
194
+
195
  display_knowledge_base(conn)
196
 
197
  with col2: