vishwask commited on
Commit
b8a80e6
·
1 Parent(s): ddbca10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -109
app.py CHANGED
@@ -188,6 +188,9 @@ def get_message_history():
188
  docs = load_data()
189
  qa_chain = load_model(docs)
190
 
 
 
 
191
  print('2')
192
  print(time.time())
193
  if prompt := st.chat_input("How can I help you today?"):
@@ -206,9 +209,7 @@ if prompt := st.chat_input("How can I help you today?"):
206
  result = qa_chain(prompt)
207
  print('4')
208
  print(time.time())
209
- sound_file = BytesIO()
210
- tts = gTTS(result['result'], lang='en')
211
- tts.write_to_fp(sound_file)
212
  output = [result['result']]
213
 
214
  for item in output:
@@ -220,116 +221,60 @@ if prompt := st.chat_input("How can I help you today?"):
220
 
221
  print('5')
222
  print(time.time())
223
- ### READ IN PDF
224
- page_number = int(result['source_documents'][0].metadata['page'])
225
- doc = fitz.open(str(result['source_documents'][0].metadata['source']))
226
-
227
- text = str(result['source_documents'][0].page_content)
228
- if text != '':
229
- for page in doc:
230
- ### SEARCH
231
- text_instances = page.search_for(text)
232
-
233
- ### HIGHLIGHT
234
- for inst in text_instances:
235
- highlight = page.add_highlight_annot(inst)
236
- highlight.update()
237
-
238
- ### OUTPUT
239
- doc.save("/home/user/app/pdf2image/output.pdf", garbage=4, deflate=True, clean=True)
240
-
241
- # pdf_to_open = repr(result['source_documents'][0].metadata['source'])
242
-
243
- def pdf_page_to_image(pdf_file, page_number, output_image):
244
- # Open the PDF file
245
- pdf_document = fitz.open(pdf_file)
246
-
247
- # Get the specific page
248
- page = pdf_document[page_number]
249
-
250
- # Define the image DPI (dots per inch)
251
- dpi = 300 # You can adjust this as needed
252
-
253
- # Convert the page to an image
254
- pix = page.get_pixmap(matrix=fitz.Matrix(dpi / 100, dpi / 100))
255
-
256
- # Save the image as a PNG file
257
- pix.save(output_image, "png")
258
-
259
- # Close the PDF file
260
- pdf_document.close()
261
-
262
-
263
- pdf_page_to_image('/home/user/app/pdf2image/output.pdf', page_number, '/home/user/app/pdf2image/output.png')
264
 
265
- image = Image.open('/home/user/app/pdf2image/output.png')
266
- st.image(image)
267
- st.audio(sound_file)
268
-
269
- # if 'clickedR' not in st.session_state:
270
- # st.session_state.clickedR = False
271
-
272
- # def click_buttonR():
273
- # st.session_state.clickedR = True
274
- # if st.session_state.clickedR:
275
- # message_placeholder.markdown(full_response+repr(result['source_documents'][0]))
276
-
277
- # ref = st.button('References', on_click = click_buttonR)
278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
 
280
- # if 'clickedA' not in st.session_state:
281
- # st.session_state.clickedA = False
282
-
283
- # def click_buttonA():
284
- # st.session_state.clickedA = True
285
- # if st.session_state.clickedA:
286
- # sound_file = BytesIO()
287
- # tts = gTTS(result['result'], lang='en')
288
- # tts.write_to_fp(sound_file)
289
- # st.audio(sound_file)
290
-
291
-
292
- # ref = st.button(':speaker:', on_click = click_buttonA)
293
-
294
-
295
-
296
-
297
-
298
- #st.session_state.clickedR = False
299
-
300
- # #if ref:
301
- # message_placeholder.markdown(full_response+repr(result['source_documents'][0]))
302
- # #if sound:
303
- # sound_file = BytesIO()
304
- # tts = gTTS(result['result'], lang='en')
305
- # tts.write_to_fp(sound_file)
306
- # html_string = """
307
- # <audio controls autoplay>
308
- # <source src="/content/sound_file" type="audio/wav">
309
- # </audio>
310
- # """
311
- # message_placeholder.markdown(html_string, unsafe_allow_html=True) # will display a st.audio with the sound you specified in the "src" of the html_string and autoplay it
312
- # #time.sleep(5) # wait for 2 seconds to finish the playing of the audio
313
- #response_sentiment = st.radio(
314
- # "How was the Assistant's response?",
315
- # ["😁", "😕", "😢"],
316
- # key="response_sentiment",
317
- # disabled=st.session_state.disabled,
318
- # horizontal=True,
319
- # index=1,
320
- # help="This helps us improve the model.",
321
- # # hide the radio button on click
322
- # on_change=on_select(),
323
- #)
324
- #logger.info(f"{user_session_id} | {full_response} | {response_sentiment}")
325
-
326
- # # Logging to FastAPI Endpoint
327
- # headers = {"Authorization": f"Bearer {secret_token}"}
328
- # log_data = {"log": f"{user_session_id} | {full_response} | {response_sentiment}"}
329
- # response = requests.post(fastapi_endpoint, json=log_data, headers=headers, timeout=10)
330
- # if response.status_code == 200:
331
- # logger.info("Query logged successfully")
332
 
 
 
 
 
 
 
 
 
333
  st.session_state.messages.append({"role": "assistant", "content": full_response})
334
 
335
 
 
188
  docs = load_data()
189
  qa_chain = load_model(docs)
190
 
191
+
192
+
193
+
194
  print('2')
195
  print(time.time())
196
  if prompt := st.chat_input("How can I help you today?"):
 
209
  result = qa_chain(prompt)
210
  print('4')
211
  print(time.time())
212
+
 
 
213
  output = [result['result']]
214
 
215
  for item in output:
 
221
 
222
  print('5')
223
  print(time.time())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
+ def generate_pdf():
226
+ page_number = int(result['source_documents'][0].metadata['page'])
227
+ doc = fitz.open(str(result['source_documents'][0].metadata['source']))
 
 
 
 
 
 
 
 
 
 
228
 
229
+ text = str(result['source_documents'][0].page_content)
230
+ if text != '':
231
+ for page in doc:
232
+ ### SEARCH
233
+ text_instances = page.search_for(text)
234
+
235
+ ### HIGHLIGHT
236
+ for inst in text_instances:
237
+ highlight = page.add_highlight_annot(inst)
238
+ highlight.update()
239
+
240
+ ### OUTPUT
241
+ doc.save("/home/user/app/pdf2image/output.pdf", garbage=4, deflate=True, clean=True)
242
+
243
+ # pdf_to_open = repr(result['source_documents'][0].metadata['source'])
244
+
245
+ def pdf_page_to_image(pdf_file, page_number, output_image):
246
+ # Open the PDF file
247
+ pdf_document = fitz.open(pdf_file)
248
+
249
+ # Get the specific page
250
+ page = pdf_document[page_number]
251
+
252
+ # Define the image DPI (dots per inch)
253
+ dpi = 300 # You can adjust this as needed
254
 
255
+ # Convert the page to an image
256
+ pix = page.get_pixmap(matrix=fitz.Matrix(dpi / 100, dpi / 100))
257
+
258
+ # Save the image as a PNG file
259
+ pix.save(output_image, "png")
260
+
261
+ # Close the PDF file
262
+ pdf_document.close()
263
+
264
+
265
+ pdf_page_to_image('/home/user/app/pdf2image/output.pdf', page_number, '/home/user/app/pdf2image/output.png')
266
+
267
+ image = Image.open('/home/user/app/pdf2image/output.png')
268
+ st.image(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269
 
270
+ def generate_audio():
271
+ sound_file = BytesIO()
272
+ tts = gTTS(result['result'], lang='en')
273
+ tts.write_to_fp(sound_file)
274
+ st.audio(sound_file)
275
+
276
+ st.button(':speaker:', type='primary',on_click=generate_audio)
277
+ st.button('Reference',type='primary',on_click=generate_pdf)
278
  st.session_state.messages.append({"role": "assistant", "content": full_response})
279
 
280