• Stars
    star
    6,698
  • Rank 5,608 (Top 0.2 %)
  • Language
    Python
  • License
    MIT License
  • Created about 1 year ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PDF GPT allows you to chat with the contents of your PDF file by using GPT capabilities. The most effective open source solution to turn your pdf files in a chatbot!

pdfGPT

Demo

  1. Demo URL: https://bhaskartripathi-pdfgpt-turbo.hf.space

  2. Demo Video:

    IMAGE ALT TEXT HERE

Version Updates (27 July, 2023):

  1. Improved error handling
  2. PDF GPT now supports Turbo models and GPT4 including 16K and 32K token model.
  3. Pre-defined questions for auto-filling the input.
  4. Implemented Chat History feature. image

Note on model performance

If you find the response for a specific question in the PDF is not good using Turbo models, then you need to understand that Turbo models such as gpt-3.5-turbo are chat completion models and will not give a good response in some cases where the embedding similarity is low. Despite the claim by OpenAI, the turbo model is not the best model for Q&A. In those specific cases, either use the good old text-DaVinci-003 or use GPT4 and above. These models invariably give you the most relevant output.

Upcoming Release Pipeline:

  1. Support for Falcon, Vicuna, Meta Llama
  2. OCR Support
  3. Multiple PDF file support
  4. OCR Support
  5. Node.Js based Web Application - With no trial, no API fees. 100% Open source.

Problem Description :

  1. When you pass a large text to Open AI, it suffers from a 4K token limit. It cannot take an entire pdf file as an input
  2. Open AI sometimes becomes overtly chatty and returns irrelevant response not directly related to your query. This is because Open AI uses poor embeddings.
  3. ChatGPT cannot directly talk to external data. Some solutions use Langchain but it is token hungry if not implemented correctly.
  4. There are a number of solutions like https://www.chatpdf.com, https://www.bespacific.com/chat-with-any-pdf, https://www.filechat.io they have poor content quality and are prone to hallucination problem. One good way to avoid hallucinations and improve truthfulness is to use improved embeddings. To solve this problem, I propose to improve embeddings with Universal Sentence Encoder family of algorithms (Read more here: https://tfhub.dev/google/collections/universal-sentence-encoder/1).

Solution: What is PDF GPT ?

  1. PDF GPT allows you to chat with an uploaded PDF file using GPT functionalities.
  2. The application intelligently breaks the document into smaller chunks and employs a powerful Deep Averaging Network Encoder to generate embeddings.
  3. A semantic search is first performed on your pdf content and the most relevant embeddings are passed to the Open AI.
  4. A custom logic generates precise responses. The returned response can even cite the page number in square brackets([]) where the information is located, adding credibility to the responses and helping to locate pertinent information quickly. The Responses are much better than the naive responses by Open AI.
  5. Andrej Karpathy mentioned in this post that KNN algorithm is most appropriate for similar problems: https://twitter.com/karpathy/status/1647025230546886658
  6. Enables APIs on Production using langchain-serve.

Docker

Run docker-compose -f docker-compose.yaml up to use it with Docker compose.

Use pdfGPT on Production using langchain-serve

Local playground

  1. Run lc-serve deploy local api on one terminal to expose the app as API using langchain-serve.
  2. Run python app.py on another terminal for a local gradio playground.
  3. Open http://localhost:7860 on your browser and interact with the app.

Cloud deployment

Make pdfGPT production ready by deploying it on Jina Cloud.

lc-serve deploy jcloud api

Show command output
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ App ID       โ”‚                                 langchain-3ff4ab2c9d                                 โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Phase        โ”‚                                       Serving                                        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Endpoint     โ”‚                      https://langchain-3ff4ab2c9d.wolf.jina.ai                       โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ App logs     โ”‚                               dashboards.wolf.jina.ai                                โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Swagger UI   โ”‚                    https://langchain-3ff4ab2c9d.wolf.jina.ai/docs                    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ OpenAPI JSON โ”‚                https://langchain-3ff4ab2c9d.wolf.jina.ai/openapi.json                โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Interact using cURL

(Change the URL to your own endpoint)

PDF url

curl -X 'POST' \
  'https://langchain-3ff4ab2c9d.wolf.jina.ai/ask_url' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "url": "https://uiic.co.in/sites/default/files/uploads/downloadcenter/Arogya%20Sanjeevani%20Policy%20CIS_2.pdf",
  "question": "What'\''s the cap on room rent?",
  "envs": {
    "OPENAI_API_KEY": "'"${OPENAI_API_KEY}"'"
    }
}'

{"result":" Room rent is subject to a maximum of INR 5,000 per day as specified in the Arogya Sanjeevani Policy [Page no. 1].","error":"","stdout":""}

PDF file

QPARAMS=$(echo -n 'input_data='$(echo -n '{"question": "What'\''s the cap on room rent?", "envs": {"OPENAI_API_KEY": "'"${OPENAI_API_KEY}"'"}}' | jq -s -R -r @uri))
curl -X 'POST' \
  'https://langchain-3ff4ab2c9d.wolf.jina.ai/ask_file?'"${QPARAMS}" \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'file=@Arogya_Sanjeevani_Policy_CIS_2.pdf;type=application/pdf'

{"result":" Room rent is subject to a maximum of INR 5,000 per day as specified in the Arogya Sanjeevani Policy [Page no. 1].","error":"","stdout":""}

Running on localhost

Credits : Adithya S

  1. Pull the image by entering the following command in your terminal or command prompt:
docker pull registry.hf.space/bhaskartripathi-pdfchatter:latest
  1. Download the Universal Sentence Encoder locally to your project's root folder. This is important because otherwise, 915 MB will be downloaded at runtime everytime you run it.
  2. Download the encoder using this link.
  3. Extract the downloaded file and place it in your project's root folder as shown below:
Root folder of your project
โ””โ”€โ”€โ”€Universal Sentence Encoder
|   โ”œโ”€โ”€โ”€assets
|   โ””โ”€โ”€โ”€variables
|   โ””โ”€โ”€โ”€saved_model.pb
|
โ””โ”€โ”€โ”€app.py
  1. If you have downloaded it locally, replace the code on line 68 in the API file:
self.use = hub.load('https://tfhub.dev/google/universal-sentence-encoder/4')

with:

self.use = hub.load('./Universal Sentence Encoder/')
  1. Now, To run PDF-GPT, enter the following command:
docker run -it -p 7860:7860 --platform=linux/amd64 registry.hf.space/bhaskartripathi-pdfchatter:latest python app.py

Original Source code with no integrations (for demo hosted in Hugging Face) :

https://huggingface.co/spaces/bhaskartripathi/pdfGPT_Turbo

UML

sequenceDiagram
    participant User
    participant System

    User->>System: Enter API Key
    User->>System: Upload PDF/PDF URL
    User->>System: Ask Question
    User->>System: Submit Call to Action

    System->>System: Blank field Validations
    System->>System: Convert PDF to Text
    System->>System: Decompose Text to Chunks (150 word length)
    System->>System: Check if embeddings file exists
    System->>System: If file exists, load embeddings and set the fitted attribute to True
    System->>System: If file doesn't exist, generate embeddings, fit the recommender, save embeddings to file and set fitted attribute to True
    System->>System: Perform Semantic Search and return Top 5 Chunks with KNN
    System->>System: Load Open AI prompt
    System->>System: Embed Top 5 Chunks in Open AI Prompt
    System->>System: Generate Answer with Davinci

    System-->>User: Return Answer

Flowchart

flowchart TB
A[Input] --> B[URL]
A -- Upload File manually --> C[Parse PDF]
B --> D[Parse PDF] -- Preprocess --> E[Dynamic Text Chunks]
C -- Preprocess --> E[Dynamic Text Chunks with citation history]
E --Fit-->F[Generate text embedding with Deep Averaging Network Encoder on each chunk]
F -- Query --> G[Get Top Results]
G -- K-Nearest Neighbour --> K[Get Nearest Neighbour - matching citation references]
K -- Generate Prompt --> H[Generate Answer]
H -- Output --> I[Output]

Star History

Star History Chart I am looking for more contributors from the open source community who can take up backlog items voluntarily and maintain the application jointly with me.

Also Try PyViralContent:

Have you ever thought why your social media posts, blog, article, advertising, YouTube video, or other content don't go viral? I have published a new Python Package: pyviralcontent ! ๐Ÿš€ It predicts the virality of your content along with readability scores! It uses multiple sophisticated algorithms to calculate your content's readability score and its predict its viral probability using Multi Criteria Decision Analysis. ๐Ÿ“ˆ Make your content strategy data-driven with pyviralcontent. Try it out and take your content's impact to the next level! ๐Ÿ’ฅ https://github.com/bhaskatripathi/pyviralcontent

License

This project is licensed under the MIT License. See the LICENSE.txt file for details.

Citation

If you use PDF-GPT in your research or wish to refer to the examples in this repo, please cite with:

@misc{pdfgpt2023,
  author = {Bhaskar Tripathi},
  title = {PDF-GPT},
  year = {2023},
  publisher = {GitHub},
  journal = {GitHub Repository},
  howpublished = {\url{https://github.com/bhaskatripathi/pdfGPT}}
}

More Repositories

1

GWOSVR

Sanitized Grey Wolf Optimizer(SGWO)-Support Vector Regressor (SVR)
Python
32
star
2

TypeTruth

TypeTruth is a Python library that detects whether a text is written by a human or AI. Ideal for fact-checking and content validation in the age of AI content generators.
Jupyter Notebook
15
star
3

HypothesisHub

An AI Tool for Automated Research Question and Hypothesis Generation from a given Scientific Literature
Jupyter Notebook
12
star
4

CEEMDAN_LSTM

An advancement on the EEMD method, Complete Ensemble Empirical Mode Decomposition with Adaptive Noise (CEEMDAN) allows for a granular spectral separation of the Intrinsic Mode Functions and a more precise reconstruction of the original signal (IMFs)
Python
10
star
5

FusionGPT

The OpenAI API has a token limit of 4K per request. This application addresses this limitation by dividing large text requests into smaller parts, each processed as a separate API request. The resulting responses are combined and presented to the user in their entirety. The approach enhances manageability and ensures adherence to the token limit.
JavaScript
4
star
6

BenchmarkFunctions

This is a running list of benchmark functions for Single and Multi-Objective Optimization problems
Python
3
star
7

Text2Diagram

Text2Diagram is an AI based diagramming tool that uses Natural language text to create diagrams.
TypeScript
3
star
8

Scientific_computing_libraries

Some of the most efficient libraries for Scientific Computation
HTML
3
star
9

Indian_national_anthem

Indian national anthem on Aurdino
C++
2
star
10

Randomwalks

Random Walk strategies for optimization algorithms using Low-Discrepancy Sequences and quasi-Monte Carlo simulations
Python
2
star
11

how_to_read_research_papers

How to read research papers
2
star
12

Interpolation

Interpolate missing values in any data using Cubic Spline Method. The code iterates over all the files in a directory, reads each files, applies interpolation and finally dumps the output file in another directory
Python
2
star
13

Minddoctor

A mental health and well being assistant
TypeScript
1
star
14

scientific_writing

Golden Rules of Scientific Writing | My Personal list of experiences
1
star
15

Journalfinder

The Scientific Journal Finder suggests the best matching journals for a given research paper.
Python
1
star
16

CitationFinder

The project aims to automate the process of generating relevant citations for a given text and conducting literature review for researchers.
Jupyter Notebook
1
star
17

AlpacaLoraLLaMA

Python
1
star