AzizWazir commited on
Commit
c050171
·
verified ·
1 Parent(s): d94824f

Update app.py

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