AzizWazir commited on
Commit
d57bc5f
·
verified ·
1 Parent(s): 76aa7af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -89
app.py CHANGED
@@ -1,97 +1,50 @@
1
  import streamlit as st
 
2
  from pdf2image import convert_from_path
 
3
  import pytesseract
4
- import os
5
- from pathlib import Path
6
- from sentence_transformers import SentenceTransformer
7
- import pinecone
8
- import tempfile
9
- import shutil
10
-
11
- # Debug: Check PATH and pdfinfo accessibility
12
- st.write("System PATH:", os.environ["PATH"])
13
- if shutil.which("pdfinfo") is None:
14
- st.error("pdfinfo is not found in the current environment. Please check your PATH.")
15
- else:
16
- st.success("pdfinfo found and accessible.")
17
-
18
- # Initialize Streamlit app
19
- st.title("PDF Image to Text/Word Converter with Search Capability")
20
- st.write("Upload your PDF to extract text or Word document and search content within it.")
21
 
22
- # Create a temporary directory
23
- temp_dir = tempfile.mkdtemp()
24
 
25
- # Upload PDF file
26
- uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
27
-
28
- if uploaded_file:
29
- pdf_path = Path(temp_dir) / uploaded_file.name
30
- with open(pdf_path, "wb") as f:
31
- f.write(uploaded_file.read())
32
-
33
- st.write("File uploaded successfully!")
34
-
35
- # Convert PDF pages to images
36
- st.write("Converting PDF to images...")
37
  try:
38
- images = convert_from_path(pdf_path, output_folder=temp_dir, poppler_path="/usr/bin") # Update path as needed
 
 
 
 
 
 
 
39
  except Exception as e:
40
  st.error(f"Error during PDF to image conversion: {e}")
41
- st.stop()
42
-
43
- # Extract text from images
44
- st.write("Extracting text from images...")
45
- extracted_text = ""
46
- for idx, image in enumerate(images):
47
- st.image(image, caption=f"Page {idx + 1}", use_column_width=True)
48
- text = pytesseract.image_to_string(image)
49
- extracted_text += text + "\n"
50
-
51
- # Save extracted text to a .txt file
52
- text_file_path = Path(temp_dir) / "extracted_text.txt"
53
- with open(text_file_path, "w", encoding="utf-8") as text_file:
54
- text_file.write(extracted_text)
55
-
56
- st.success("Text extraction complete!")
57
-
58
- # Option to download text file
59
- st.download_button(
60
- label="Download Extracted Text",
61
- data=extracted_text,
62
- file_name="extracted_text.txt",
63
- mime="text/plain",
64
- )
65
-
66
- # Initialize vector model and Pinecone
67
- st.write("Initializing vector search...")
68
- model = SentenceTransformer('all-MiniLM-L6-v2')
69
- pinecone.init(api_key="YOUR_PINECONE_API_KEY", environment="us-west1-gcp") # Replace with your Pinecone details
70
-
71
- # Create Pinecone index if it doesn't exist
72
- index_name = "pdf-text-index"
73
- if index_name not in pinecone.list_indexes():
74
- pinecone.create_index(index_name, dimension=384)
75
- index = pinecone.Index(index_name)
76
-
77
- # Generate vector embeddings and upload to Pinecone
78
- st.write("Generating vector embeddings...")
79
- sentences = extracted_text.split("\n")
80
- embeddings = model.encode(sentences)
81
- for i, embedding in enumerate(embeddings):
82
- index.upsert([(f"sentence-{i}", embedding, {"sentence": sentences[i]})])
83
-
84
- # Search functionality
85
- st.write("Search within the extracted text")
86
- query = st.text_input("Enter your query:")
87
- if query:
88
- query_vector = model.encode([query])[0]
89
- results = index.query(query_vector, top_k=5, include_metadata=True)
90
- st.write("Top results:")
91
- for match in results["matches"]:
92
- st.write(f"Score: {match['score']}, Text: {match['metadata']['sentence']}")
93
-
94
- # Cleanup temporary files
95
- if st.button("Clean Temporary Files"):
96
- shutil.rmtree(temp_dir)
97
- st.success("Temporary files cleaned!")
 
1
  import streamlit as st
2
+ import os
3
  from pdf2image import convert_from_path
4
+ from PIL import Image
5
  import pytesseract
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # Ensure Poppler is in the PATH (you might need to adjust this for your server)
8
+ os.environ["PATH"] += ":/usr/local/bin" # Update this with the correct Poppler path if needed
9
 
10
+ def pdf_to_text(pdf_path):
 
 
 
 
 
 
 
 
 
 
 
11
  try:
12
+ # Convert PDF to images
13
+ images = convert_from_path(pdf_path)
14
+ text = ""
15
+
16
+ for image in images:
17
+ text += pytesseract.image_to_string(image)
18
+
19
+ return text
20
  except Exception as e:
21
  st.error(f"Error during PDF to image conversion: {e}")
22
+ return None
23
+
24
+ def main():
25
+ st.title("PDF to Text Converter")
26
+
27
+ # Upload PDF file
28
+ uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
29
+
30
+ if uploaded_file is not None:
31
+ # Save uploaded file temporarily
32
+ with open("uploaded_file.pdf", "wb") as f:
33
+ f.write(uploaded_file.getbuffer())
34
+
35
+ st.text("Converting PDF to text...")
36
+
37
+ text = pdf_to_text("uploaded_file.pdf")
38
+
39
+ if text:
40
+ st.text_area("Extracted Text", text, height=300)
41
+
42
+ # Create downloadable Word file
43
+ word_file = "output.docx"
44
+ with open(word_file, "w") as f:
45
+ f.write(text)
46
+
47
+ st.download_button("Download Word File", word_file)
48
+
49
+ if __name__ == "__main__":
50
+ main()