Author: Karthik M
This Flask web application allows users to upload a PDF file, generate a summary, and ask questions about the content of the PDF. The application leverages Cohere for text summarization and question answering. It uses FAISS for efficient similarity search within the text.
- Flask
- PyPDF2
- Langchain (for text processing and QA chain)
- langchain_community
- Cohere API
- FAISS
- cohere
- knowledge_base: Stores the knowledge base created from the PDF content.
- api_key: Stores the API key for accessing Cohere services.
Generates a summary of the given text using the Cohere API.
-
Parameters:
full_text
(str): The complete text extracted from the PDF.api_key
(str): The API key for Cohere.
-
Returns:
summary
(str): The generated summary.
Renders the main page and handles PDF file uploads.
-
GET: Renders the
index.html
template. -
POST: Processes the uploaded PDF file, extracts text, generates a summary, and creates a knowledge base.
- Checks if a file part is present in the request.
- Extracts text from the uploaded PDF using PyPDF2.
- Splits the text into chunks using
CharacterTextSplitter
. - Generates a summary of the full text using the
generate_summary
function. - Evaluates the summary length.
- Creates a knowledge base using FAISS and Cohere embeddings.
-
Returns:
- JSON response containing the summary and its evaluation.
Handles question answering based on the uploaded PDF content.
-
Parameters (JSON):
question
(str): The question to be answered.
-
Returns:
- JSON response containing the answer to the question.
The application includes error handling for scenarios such as missing files, errors during summary generation, and errors during question answering.
- Navigate to the main page and upload a PDF file.
- The application extracts text from the PDF, generates a summary, and evaluates the summary's length.
- Use the
/ask
endpoint to submit questions about the uploaded PDF content. - The application uses similarity search to find relevant chunks of text and a QA chain to generate an answer.
The feedback block allows users to rate their experience and leave comments. It includes:
- Star Rating: Users click on stars to rate. The
toggleStar(starNumber)
function updates star colors and sets the rating. - Comment Textarea: Users can type comments.
- Submit Button: On clicking,
submitFeedback()
disables inputs, changes the button text to "Submitted," and shows an alert thanking the user with their rating.
To install the necessary libraries, run:
pip install Flask PyPDF2 langchain langchain_community cohere faiss-cpu
Run the Flask application by executing:
if __name__ == '__main__':
app.run(debug=True)
Once the server is running, open your web browser and go to http://127.0.0.1:5000 to access the application.
This project is licensed under the MIT License. See the LICENSE file for details.