Update app.py
Browse files
app.py
CHANGED
@@ -129,7 +129,7 @@ for message in st.session_state.messages:
|
|
129 |
def on_select():
|
130 |
st.session_state.disabled = True
|
131 |
|
132 |
-
|
133 |
def get_message_history():
|
134 |
for message in st.session_state.messages:
|
135 |
role, content = message["role"], message["content"]
|
@@ -147,11 +147,12 @@ if prompt := st.chat_input("How can I help you today?"):
|
|
147 |
message_placeholder = st.empty()
|
148 |
full_response = ""
|
149 |
message_history = "\n".join(list(get_message_history())[-3:])
|
150 |
-
|
151 |
-
result = qa_chain(
|
152 |
output = [result['result']]
|
153 |
|
154 |
def generate_pdf():
|
|
|
155 |
page_number = int(result['source_documents'][0].metadata['page'])
|
156 |
doc = fitz.open(str(result['source_documents'][0].metadata['source']))
|
157 |
text = str(result['source_documents'][0].page_content)
|
@@ -171,9 +172,11 @@ if prompt := st.chat_input("How can I help you today?"):
|
|
171 |
pix.save(output_image, "png")
|
172 |
pdf_document.close()
|
173 |
pdf_page_to_image('/home/user/app/pdf2image/output.pdf', page_number, '/home/user/app/pdf2image/output.png')
|
174 |
-
image = Image.open('/home/user/app/pdf2image/output.png')
|
175 |
-
st.image(image)
|
176 |
-
st.session_state.image_displayed = True
|
|
|
|
|
177 |
|
178 |
def generate_audio():
|
179 |
sound_file = BytesIO()
|
@@ -188,8 +191,25 @@ if prompt := st.chat_input("How can I help you today?"):
|
|
188 |
message_placeholder.markdown(full_response + "▌")
|
189 |
message_placeholder.markdown(full_response)
|
190 |
|
191 |
-
|
192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
195 |
|
|
|
129 |
def on_select():
|
130 |
st.session_state.disabled = True
|
131 |
|
132 |
+
|
133 |
def get_message_history():
|
134 |
for message in st.session_state.messages:
|
135 |
role, content = message["role"], message["content"]
|
|
|
147 |
message_placeholder = st.empty()
|
148 |
full_response = ""
|
149 |
message_history = "\n".join(list(get_message_history())[-3:])
|
150 |
+
question = st.text_input("Ask your question", placeholder="Try to include context in your question")
|
151 |
+
result = qa_chain(question)
|
152 |
output = [result['result']]
|
153 |
|
154 |
def generate_pdf():
|
155 |
+
generate_audio()
|
156 |
page_number = int(result['source_documents'][0].metadata['page'])
|
157 |
doc = fitz.open(str(result['source_documents'][0].metadata['source']))
|
158 |
text = str(result['source_documents'][0].page_content)
|
|
|
172 |
pix.save(output_image, "png")
|
173 |
pdf_document.close()
|
174 |
pdf_page_to_image('/home/user/app/pdf2image/output.pdf', page_number, '/home/user/app/pdf2image/output.png')
|
175 |
+
# image = Image.open('/home/user/app/pdf2image/output.png')
|
176 |
+
# st.image(image)
|
177 |
+
# st.session_state.image_displayed = True
|
178 |
+
path = '/home/user/app/pdf2image/output.png'
|
179 |
+
return path
|
180 |
|
181 |
def generate_audio():
|
182 |
sound_file = BytesIO()
|
|
|
191 |
message_placeholder.markdown(full_response + "▌")
|
192 |
message_placeholder.markdown(full_response)
|
193 |
|
194 |
+
def display_image(image_path):
|
195 |
+
"""Displays an image at the given path."""
|
196 |
+
st.image(image_path)
|
197 |
+
|
198 |
+
|
199 |
+
# Create a button to display the image.
|
200 |
+
if st.button("Display Image"):
|
201 |
+
# Get the image path from the session state dictionary.
|
202 |
+
image_path = st.session_state.get("image_path")
|
203 |
+
|
204 |
+
# If the image path is not set, set it to the default image path.
|
205 |
+
if image_path is None:
|
206 |
+
image_path = generate_pdf()
|
207 |
+
|
208 |
+
# Display the image.
|
209 |
+
display_image(image_path)
|
210 |
+
|
211 |
+
# Store the image path in the session state dictionary.
|
212 |
+
st.session_state["image_path"] = image_path
|
213 |
|
214 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
215 |
|