Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
#
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
st.error(f"Error during PDF to image conversion: {e}")
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
st.
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|