catastropiyush commited on
Commit
ec0e05f
1 Parent(s): d788dc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -6,6 +6,7 @@ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
6
  from llama_index.core import Settings
7
  import os
8
  import base64
 
9
 
10
  # Load environment variables
11
  load_dotenv()
@@ -31,11 +32,20 @@ DATA_DIR = "data"
31
  os.makedirs(DATA_DIR, exist_ok=True)
32
  os.makedirs(PERSIST_DIR, exist_ok=True)
33
 
 
34
  def displayPDF(file):
35
- with open(file, "rb") as f:
36
- base64_pdf = base64.b64encode(f.read()).decode('utf-8')
37
- pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="600" type="application/pdf"></iframe>'
38
- st.markdown(pdf_display, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
39
 
40
  def data_ingestion():
41
  documents = SimpleDirectoryReader(DATA_DIR).load_data()
@@ -89,7 +99,7 @@ with st.sidebar:
89
  f.write(uploaded_file.getbuffer())
90
  data_ingestion() # Process PDF every time new file is uploaded
91
  st.success("PDF is ready!")
92
- displayPDF(filepath)
93
 
94
  user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
95
  if user_prompt:
 
6
  from llama_index.core import Settings
7
  import os
8
  import base64
9
+ import fitz # PyMuPDF
10
 
11
  # Load environment variables
12
  load_dotenv()
 
32
  os.makedirs(DATA_DIR, exist_ok=True)
33
  os.makedirs(PERSIST_DIR, exist_ok=True)
34
 
35
+
36
  def displayPDF(file):
37
+ # Open the PDF file
38
+ pdf_document = fitz.open(file)
39
+ # Render the first page
40
+ first_page = pdf_document.load_page(0)
41
+ # Convert the page to an image
42
+ pix = first_page.get_pixmap()
43
+ # Save the image
44
+ image_path = "first_page.png"
45
+ pix.save(image_path)
46
+ # Display the image in Streamlit
47
+ st.image(image_path)
48
+
49
 
50
  def data_ingestion():
51
  documents = SimpleDirectoryReader(DATA_DIR).load_data()
 
99
  f.write(uploaded_file.getbuffer())
100
  data_ingestion() # Process PDF every time new file is uploaded
101
  st.success("PDF is ready!")
102
+ displayPDF(filepath)
103
 
104
  user_prompt = st.chat_input("Ask me anything about the content of the PDF:")
105
  if user_prompt: