Spaces:
Sleeping
Sleeping
Commit
·
44be36b
1
Parent(s):
e44b3be
added pdf tools
Browse files
pages/Project_4_-_NLP_and_PDF_analyser.py
CHANGED
|
@@ -1,9 +1,79 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
################################################################################
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from src.functions_pdf import pymupdf_pdf_to_text, pypdf2_pdf_to_text, pdfminer_pdf_to_text, pdfplumber_pdf_to_text
|
| 4 |
|
| 5 |
################################################################################
|
| 6 |
|
| 7 |
+
# Sidebar for parameters
|
| 8 |
+
st.sidebar.title("App Parameters")
|
| 9 |
+
|
| 10 |
+
# Select method in the sidebar
|
| 11 |
+
method = st.sidebar.selectbox(
|
| 12 |
+
"Select the method to extract text from PDF",
|
| 13 |
+
("PyMuPDF", "PyPDF2", "pdfminer", "pdfplumber", "reportlab"),
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Main page title and description
|
| 17 |
+
st.title("NLP and PDF Analyser")
|
| 18 |
+
st.markdown(
|
| 19 |
+
"""
|
| 20 |
+
This tool allows you to extract text from PDF files using different methods.
|
| 21 |
+
Select a method, upload a PDF file, and extract the text.
|
| 22 |
+
"""
|
| 23 |
+
)
|
| 24 |
+
st.divider()
|
| 25 |
+
|
| 26 |
+
# File uploader
|
| 27 |
+
file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
| 28 |
+
|
| 29 |
+
if file is not None:
|
| 30 |
+
# Display file details
|
| 31 |
+
st.info(f"Uploaded file: **{file.name}** ({file.size / 1024:.2f} KB)")
|
| 32 |
+
|
| 33 |
+
# Extract text button
|
| 34 |
+
if st.button("Extract Text"):
|
| 35 |
+
# Save the uploaded file to a temporary location
|
| 36 |
+
with open("temp_uploaded_file.pdf", "wb") as temp_file:
|
| 37 |
+
temp_file.write(file.read())
|
| 38 |
+
|
| 39 |
+
# Use the temporary file path for processing
|
| 40 |
+
temp_file_path = "temp_uploaded_file.pdf"
|
| 41 |
+
|
| 42 |
+
# Extract text based on the selected method
|
| 43 |
+
st.subheader("Extracted Text")
|
| 44 |
+
if method == "PyMuPDF":
|
| 45 |
+
st.write("Using **PyMuPDF** for text extraction.")
|
| 46 |
+
text = pymupdf_pdf_to_text(temp_file_path)
|
| 47 |
+
elif method == "PyPDF2":
|
| 48 |
+
st.write("Using **PyPDF2** for text extraction.")
|
| 49 |
+
text = pypdf2_pdf_to_text(temp_file_path)
|
| 50 |
+
elif method == "pdfminer":
|
| 51 |
+
st.write("Using **pdfminer** for text extraction.")
|
| 52 |
+
text = pdfminer_pdf_to_text(temp_file_path)
|
| 53 |
+
elif method == "pdfplumber":
|
| 54 |
+
st.write("Using **pdfplumber** for text extraction.")
|
| 55 |
+
text = pdfplumber_pdf_to_text(temp_file_path)
|
| 56 |
+
################################################################################
|
| 57 |
+
# Clean up the temporary file
|
| 58 |
+
if os.path.exists(temp_file_path):
|
| 59 |
+
os.remove(temp_file_path)
|
| 60 |
+
################################################################################
|
| 61 |
+
else:
|
| 62 |
+
st.error("Invalid method selected.")
|
| 63 |
+
text = ""
|
| 64 |
+
|
| 65 |
+
# Display extracted text
|
| 66 |
+
if text:
|
| 67 |
+
st.text_area("Extracted Text", text, height=300)
|
| 68 |
|
| 69 |
+
# Download button for extracted text
|
| 70 |
+
st.download_button(
|
| 71 |
+
label="Download Extracted Text",
|
| 72 |
+
data=text,
|
| 73 |
+
file_name="extracted_text.txt",
|
| 74 |
+
mime="text/plain",
|
| 75 |
+
)
|
| 76 |
+
else:
|
| 77 |
+
st.warning("No text extracted. Please check the PDF file or method.")
|
| 78 |
+
else:
|
| 79 |
+
st.warning("Please upload a PDF file to proceed.")
|
requirements.txt
CHANGED
|
@@ -14,4 +14,8 @@ langchain-core
|
|
| 14 |
langgraph>0.2.27
|
| 15 |
sentry-sdk
|
| 16 |
langchain-mongodb
|
| 17 |
-
langchain-huggingface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
langgraph>0.2.27
|
| 15 |
sentry-sdk
|
| 16 |
langchain-mongodb
|
| 17 |
+
langchain-huggingface
|
| 18 |
+
PyMuPDF
|
| 19 |
+
PyPDF2
|
| 20 |
+
pdfminer.six
|
| 21 |
+
pdfplumber
|
src/__pycache__/functions_langchain.cpython-311.pyc
CHANGED
|
Binary files a/src/__pycache__/functions_langchain.cpython-311.pyc and b/src/__pycache__/functions_langchain.cpython-311.pyc differ
|
|
|
src/__pycache__/functions_pdf.cpython-311.pyc
ADDED
|
Binary file (2.49 kB). View file
|
|
|
src/__pycache__/functions_scrapper.cpython-311.pyc
CHANGED
|
Binary files a/src/__pycache__/functions_scrapper.cpython-311.pyc and b/src/__pycache__/functions_scrapper.cpython-311.pyc differ
|
|
|
src/functions_pdf.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pymupdf
|
| 2 |
+
from PyPDF2 import PdfReader
|
| 3 |
+
from pdfminer.high_level import extract_text
|
| 4 |
+
from langchain.document_loaders import PDFPlumberLoader
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def pymupdf_pdf_to_text(file_path):
|
| 8 |
+
"""
|
| 9 |
+
Extract text from a PDF file using PyMuPDF.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
file_path (str): Path to the PDF file.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
str: Extracted text from the PDF file.
|
| 16 |
+
"""
|
| 17 |
+
doc = pymupdf.open(stream=file_path.read(), filetype="pdf")
|
| 18 |
+
text = ""
|
| 19 |
+
for page in doc:
|
| 20 |
+
text += page.get_text() + "\n"
|
| 21 |
+
return text
|
| 22 |
+
|
| 23 |
+
def pypdf2_pdf_to_text(file_path):
|
| 24 |
+
"""
|
| 25 |
+
Extract text from a PDF file using PyPDF2.
|
| 26 |
+
|
| 27 |
+
Args:
|
| 28 |
+
file_path (str): Path to the PDF file.
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
str: Extracted text from the PDF file.
|
| 32 |
+
"""
|
| 33 |
+
reader = PdfReader(file_path)
|
| 34 |
+
text = ""
|
| 35 |
+
for page in reader.pages:
|
| 36 |
+
text += page.extract_text() + "\n"
|
| 37 |
+
return text
|
| 38 |
+
|
| 39 |
+
def pdfminer_pdf_to_text(file_path):
|
| 40 |
+
"""
|
| 41 |
+
Extract text from a PDF file using pdfminer.
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
file_path (str): Path to the PDF file.
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
str: Extracted text from the PDF file.
|
| 48 |
+
"""
|
| 49 |
+
# Implementation for pdfminer extraction goes here
|
| 50 |
+
text = extract_text(file_path)
|
| 51 |
+
return text
|
| 52 |
+
|
| 53 |
+
def pdfplumber_pdf_to_text(file_path):
|
| 54 |
+
"""
|
| 55 |
+
Extract text from a PDF file using pdfplumber.
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
file_path (str): Path to the PDF file.
|
| 59 |
+
|
| 60 |
+
Returns:
|
| 61 |
+
str: Extracted text from the PDF file.
|
| 62 |
+
"""
|
| 63 |
+
loader = PDFPlumberLoader(file_path)
|
| 64 |
+
documents = loader.load()
|
| 65 |
+
text = ""
|
| 66 |
+
for doc in documents:
|
| 67 |
+
text += doc.page_content + "\n"
|
| 68 |
+
return text
|
| 69 |
+
|