Sharal commited on
Commit
93b3f24
·
verified ·
1 Parent(s): ac11d7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -112,20 +112,20 @@ def conversation(qa_chain, message, history):
112
  formatted_chat_history = format_chat_history(history)
113
  response = qa_chain.invoke({"question": message, "chat_history": formatted_chat_history})
114
  response_answer = response["answer"]
115
- if "Helpful Answer:" in response_answer:
116
- response_answer = response_answer.split("Helpful Answer:")[-1]
117
  response_sources = response["source_documents"]
118
- response_source1 = response_sources[0].page_content.strip()
119
- response_source1_page = response_sources[0].metadata["page"] + 1
120
- response_source2 = response_sources[1].page_content.strip()
121
- response_source2_page = response_sources[1].metadata["page"] + 1
122
- response_source3 = response_sources[2].page_content.strip()
123
- response_source3_page = response_sources[2].metadata["page"] + 1
 
 
124
  new_history = history + [(message, response_answer)]
125
- return qa_chain, new_history, response_answer, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
126
  except Exception as e:
127
  st.error(f"Error in conversation: {e}")
128
- return qa_chain, history, "", "", 0, "", 0, "", 0
129
 
130
  def main():
131
  st.sidebar.title("PDF Chatbot")
@@ -146,7 +146,7 @@ def main():
146
  if 'qa_chain' not in st.session_state:
147
  st.session_state['qa_chain'] = None
148
 
149
- st.sidebar.markdown("### Select Large Language Model (LLM) and input parameters")
150
  llm_option = st.sidebar.radio("Available LLMs", list_llm_simple)
151
 
152
  if st.sidebar.button("Initialize Question Answering Chatbot"):
@@ -163,19 +163,15 @@ def main():
163
 
164
  if st.button("Submit"):
165
  with st.spinner("Generating response..."):
166
- qa_chain, history, response_answer, response_source1, source1_page, response_source2, source2_page, response_source3, source3_page = conversation(st.session_state['qa_chain'], message, history)
167
  st.session_state['qa_chain'] = qa_chain
168
 
169
  st.markdown("### Chatbot Response")
170
  st.write(response_answer)
171
 
172
  with st.expander("Relevant context from the source document"):
173
- st.text_area("Source 1", value=response_source1, height=100)
174
- st.text(f"Page: {source1_page}")
175
- st.text_area("Source 2", value=response_source2, height=100)
176
- st.text(f"Page: {source2_page}")
177
- st.text_area("Source 3", value=response_source3, height=100)
178
- st.text(f"Page: {source3_page}")
179
 
180
  if __name__ == "__main__":
181
  main()
 
112
  formatted_chat_history = format_chat_history(history)
113
  response = qa_chain.invoke({"question": message, "chat_history": formatted_chat_history})
114
  response_answer = response["answer"]
 
 
115
  response_sources = response["source_documents"]
116
+
117
+ sources = []
118
+ for doc in response_sources:
119
+ sources.append({
120
+ "content": doc.page_content.strip(),
121
+ "page": doc.metadata["page"] + 1
122
+ })
123
+
124
  new_history = history + [(message, response_answer)]
125
+ return qa_chain, new_history, response_answer, sources
126
  except Exception as e:
127
  st.error(f"Error in conversation: {e}")
128
+ return qa_chain, history, "", []
129
 
130
  def main():
131
  st.sidebar.title("PDF Chatbot")
 
146
  if 'qa_chain' not in st.session_state:
147
  st.session_state['qa_chain'] = None
148
 
149
+ st.sidebar.markdown("### Select Large Language Model (LLM)")
150
  llm_option = st.sidebar.radio("Available LLMs", list_llm_simple)
151
 
152
  if st.sidebar.button("Initialize Question Answering Chatbot"):
 
163
 
164
  if st.button("Submit"):
165
  with st.spinner("Generating response..."):
166
+ qa_chain, history, response_answer, sources = conversation(st.session_state['qa_chain'], message, history)
167
  st.session_state['qa_chain'] = qa_chain
168
 
169
  st.markdown("### Chatbot Response")
170
  st.write(response_answer)
171
 
172
  with st.expander("Relevant context from the source document"):
173
+ for source in sources:
174
+ st.text_area(f"Source - Page {source['page']}", value=source["content"], height=100)
 
 
 
 
175
 
176
  if __name__ == "__main__":
177
  main()