Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
124 |
new_history = history + [(message, response_answer)]
|
125 |
-
return qa_chain, new_history, response_answer,
|
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,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)
|
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,
|
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 |
-
|
174 |
-
|
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()
|