An interactive PDF Question & Answer application built with Python, NLP, Scikit-learn, PyPDF, and Streamlit.
Upload a PDF, ask questions about its content, and the application automatically searches the document to find the most relevant information using TF-IDF vectorization and cosine similarity.
- 📤 Upload PDF documents
- 📖 Automatically extract text from PDF files
- 🧹 Clean and preprocess extracted text
- 🧩 Split large documents into searchable text sections
- 🔎 Search using TF-IDF
- 📊 Rank relevant sections using Cosine Similarity
- 🤖 Extract the most relevant sentence as an answer
- 📚 Display multiple relevant document sections
- 🎯 Show relevance scores
- ⚙️ Adjustable chunk size
- 🔢 Adjustable number of relevant sections
- 💻 Simple and interactive Streamlit interface
- ❤️ Lightweight — no external LLM or API key required
The application follows a simple NLP-based retrieval pipeline:
📄 PDF Upload
↓
📖 Text Extraction
↓
🧹 Text Cleaning
↓
🧩 Text Chunking
↓
🔎 TF-IDF Vectorizer
↓
📊 Document Vector Matrix
↓
💬 User Question
↓
🔢 Question Vectorization
↓
📐 Cosine Similarity Calculation
↓
🏆 Top-K Relevant Chunks
↓
🤖 Best Sentence Extraction
↓
💡 Answer
| Technology | Purpose |
|---|---|
| 🐍 Python | Core programming language |
| 🎈 Streamlit | Web application interface |
| 📄 PyPDF | PDF text extraction |
| 🧠 Scikit-learn | TF-IDF and cosine similarity |
| 🔤 Regular Expressions | Text cleaning and sentence processing |
pdf-qa-ai/
│
├── app.py
├── requirements.txt
├── README.md
└── .gitignore
git clone https://github.com/your-username/pdf-qa-ai.gitcd pdf-qa-aipython -m venv venvvenv\Scripts\activatesource venv/bin/activatepip install -r requirements.txtCreate a requirements.txt file:
streamlit
pypdf
scikit-learn
Start the Streamlit application using:
streamlit run app.pyThe application will open in your browser.
Usually, Streamlit runs at:
http://localhost:8501
Click 📤 Upload your PDF and select a PDF document.
The application extracts readable text from the PDF and divides it into smaller searchable sections.
Enter a question related to the document.
For example:
What is machine learning?
Other examples:
What are the main objectives?
What are the advantages?
Who is the author?
What conclusion does the document give?
Click:
🔍 Find Answer
The application searches the document and returns the most relevant information.
The application also displays the top relevant sections along with their similarity scores.
The application uses PyPDF to extract text from every page of the uploaded PDF.
reader = PdfReader(pdf_file)Unnecessary whitespace and null characters are removed before processing.
text = re.sub(r"\s+", " ", text)Large documents are divided into smaller sections.
This makes searching more efficient and allows the application to identify specific parts of the document.
The chunk size can be adjusted from the sidebar.
The application converts document chunks into numerical vectors using TF-IDF.
TF-IDF stands for:
Term Frequency – Inverse Document Frequency
It gives higher importance to words that are useful for distinguishing one document section from another.
The application also uses:
ngram_range=(1, 2)which considers both individual words and two-word phrases.
The user's question is converted into a TF-IDF vector.
Cosine similarity is then calculated between the question and each PDF section.
The sections with the highest similarity scores are considered the most relevant.
After retrieving the most relevant sections, the application compares the words in the question with sentences in those sections.
The sentence with the highest overlap is selected as the answer.
Suppose the PDF contains:
Machine learning is a branch of artificial intelligence
that enables computers to learn from data without being
explicitly programmed.
User asks:
What is machine learning?
The application searches the document and may return:
Machine learning is a branch of artificial intelligence that enables computers to learn from data without being explicitly programmed.
It also displays the relevant section and its relevance score.
The application provides two configurable parameters.
Controls the approximate size of each searchable text section.
Available range:
300 – 1500 characters
Larger chunks provide more context, while smaller chunks can provide more precise retrieval.
Controls how many top matching sections are displayed.
Available range:
1 – 5
- Easy to use
- Fast document search
- No OpenAI API key required
- No database required
- No model training required
- Works completely with traditional NLP techniques
- Lightweight and suitable for beginners
- Easy to deploy using Streamlit
- Useful for experimenting with information retrieval
This project uses TF-IDF-based retrieval, so it does not understand language semantically like modern embedding models or large language models.
For example, questions using completely different wording from the PDF may receive a lower similarity score even when the document contains the correct information.
The current version also works best with text-based PDFs. Scanned PDFs containing images may not produce readable text unless OCR is added.
The project can be upgraded with:
- 🧠 Semantic search using sentence embeddings
- 🔍 Vector databases such as FAISS or Chroma
- 🤖 Transformer-based Question Answering
- 📑 Page-number citations
- 📝 Automatic document summarization
- 💬 Chat-style conversation
- 📚 Multiple PDF support
- 🔗 Source citations for every answer
- 🖼️ OCR support for scanned PDFs
- 🌐 Multilingual PDF support
- 💾 Persistent document indexing
- 📊 Answer confidence scoring
This application can be deployed easily using Streamlit Community Cloud.
Basic deployment process:
GitHub Repository
↓
Push app.py
↓
Add requirements.txt
↓
Connect Repository
↓
Deploy with Streamlit
↓
🌐 Live Application
📤 Upload PDF
↓
📖 Extract Text
↓
🧹 Clean Text
↓
🧩 Create Chunks
↓
🔎 Build TF-IDF Index
↓
💬 Ask Question
↓
📐 Calculate Similarity
↓
🏆 Retrieve Relevant Sections
↓
🤖 Generate Extractive Answer
This project demonstrates practical implementation of:
- Natural Language Processing
- Information Retrieval
- Text Preprocessing
- TF-IDF
- Cosine Similarity
- Extractive Question Answering
- Streamlit Application Development
- PDF Processing
- Machine Learning-based Search
Built with Python, NLP & Streamlit.
If you find this project useful, consider giving the repository a ⭐ on GitHub!
Sneha Mule
Computer Science & Engineering Student