The PDF Chatbot project is a web application that allows users to upload a PDF and interact with its content through a chatbot interface. This project leverages Gemini AI (a generative AI model) for generating responses based on the content of the PDF. The backend is built with FastAPI, and the frontend is developed using Streamlit. The project utilizes Retrieval-Augmented Generation (RAG) techniques to enhance the chatbot's responses.
The project is divided into two main components: Backend and Frontend. The structure is as follows:
pdf_chatbot/ \
├── backend/ \
│ ├── main.py # FastAPI backend application \
│ ├── gemini_client.py # Gemini API client integration \
│ ├── .env # Environment variables \
│ \
├── frontend/ \
│ ├── app.py # Streamlit frontend application \
│ \
├── tests/ \
│ ├── backend/ \
│ │ └── test_main.py # Unit tests for FastAPI endpoints \
│ ├── frontend/ \
│ │ └── test_app.py # Unit tests for Streamlit application \
│ \
├── screenshots/ \
│ ├── fastapi_swagger_ui.png # Swagger UI screenshot (FastAPI) \
│ ├── streamlit_app.png # Streamlit app screenshot (Frontend) \
│ \
├── requirements.txt # Project dependencies \
├── venv/ # Python virtual environment \
└── README.md # Project documentation \
-
Clone the repository:
git clone https://github.com/Gitkakkar1597/PDF-Chatbot.git cd pdf_chatbot
-
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the dependencies:
pip install -r requirements.txt
-
Create a
.env
file in thebackend
directory and add your Gemini API key:# Gemini API Key API_KEY=<your_gemini_api_key>
-
Start the FastAPI backend:
uvicorn backend.main:app --reload
The backend will be available at http://127.0.0.1:8000.
-
Start the Streamlit frontend:
streamlit run frontend/app.py
The frontend will be available at http://localhost:8501.
-
Upload a PDF:
- Navigate to the Streamlit interface.
- Use the "Upload PDF" button to upload a PDF file.
-
Ask a Question:
- After uploading a PDF, enter your query in the text input box and click "Send".
- View the chatbot's response and continue asking questions as needed.
Uploads a PDF file to the server.
Request:
- Form Data:
file
(PDF file)
Response:
200 OK
with a message:{"message": "PDF uploaded successfully."}
400 Bad Request
for invalid file formats
Queries the uploaded PDF content using the AI model.
Request:
- Body: JSON object with
query
(string)
Response:
200 OK
with the AI response:{"response": "<response from Gemini AI>"}
400 Bad Request
if no PDF is uploaded or if there's an issue with the query
To ensure the application is functioning correctly, run the unit tests:
-
Run backend tests:
pytest tests/backend/test_main.py
-
Run frontend tests:
pytest tests/frontend/test_app.py
The PDF Chatbot application provides a seamless interaction with PDF documents through a chatbot interface. The application consists of:
- Backend: FastAPI handles PDF uploads and queries, interacts with Gemini AI for response generation.
- Frontend: Streamlit allows users to upload PDFs and interact with the chatbot.
- AI Model: Gemini AI processes queries based on the content of the uploaded PDFs using Retrieval-Augmented Generation (RAG) techniques.
-
User Interaction with the Streamlit Frontend:
- Navigate to http://localhost:8501.
- Upload a PDF through the Streamlit interface.
-
FastAPI Backend Processing:
- FastAPI receives and processes the uploaded PDF.
- Extracts text from the PDF and stores it for querying.
-
Querying the PDF:
- After uploading, the user submits a query through the Streamlit interface.
- FastAPI handles the query, retrieves the PDF content, and sends it along with the query to Gemini AI.
-
Response Generation:
- Gemini AI generates a response based on the query and the extracted PDF content.
- The response is sent back to the FastAPI backend.
-
Displaying the Response:
- FastAPI sends the response to the Streamlit frontend.
- Streamlit displays the chatbot's response and updates the chat history.
-
Continuing Interaction:
- The user can continue to ask questions, with the chat history maintaining all interactions.
The application involves:
- Uploading a PDF via Streamlit.
- Processing the PDF with FastAPI.
- Handling queries and generating responses using Gemini AI.
- Displaying responses and maintaining chat history in Streamlit.
- Continuing interactions as needed.
This workflow ensures an efficient and user-friendly experience for interacting with PDF documents through a chatbot interface.
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes and commit them (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature-branch
). - Create a pull request.