omastraa

Chat with your PDF using Pinata,OpenAI and Streamlit

In this tutorial, we’ll build a simple chat interface that allows users to upload a PDF, retrieve its content using OpenAI’s API, and display the responses in a chat-like interface using Streamlit. We will also leverage to upload and store the PDF files.

Let's have a little glance at what we are building before moving forward:
 

Prerequisites :

  • Basic knowledge of Python
  • Pinata API key (for uploading PDFs)
  • OpenAI API key (for generating responses)
  • Streamlit installed (for building the UI)

Step 1: Project Setup

Start by creating a new Python project directory:
 

mkdir chat-with-pdf cd chat-with-pdf python3 -m venv venv source venv/bin/activate pip install streamlit openai requests PyPDF2

 

Now, create a .env file in the root of your project and add the following environment variables:
 

PINATA_API_KEY=<Your Pinata API Key> PINATA_SECRET_API_KEY=<Your Pinata Secret Key> OPENAI_API_KEY=<Your OpenAI API Key>

 

One have to manage OPENAI_API_KEY by own as it's paid.But let's go through the process of creating api keys in Pinita.

So, before proceeding further let us know what Pinata is why we are using it.

Pinata

Pinata is a service that provides a platform for storing and managing files on IPFS (InterPlanetary File System), a decentralized and distributed file storage system.

  • Decentralized Storage: Pinata helps you store files on IPFS, a decentralized network.
  • Easy to Use: It provides user-friendly tools and APIs for file management.
  • File Availability: Pinata keeps your files accessible by "pinning" them on IPFS.
  • NFT Support: It's great for storing metadata for NFTs and Web3 apps.
  • Cost-Effective: Pinata can be a cheaper alternative to traditional cloud storage.

Let's create required tokens by signin:

token1

Next step is to verify your registered email :

token2

After verifying signin to generate api keys :

token3

After that go to API Key Section and Create New API Keys:

token4

Finally, keys are successfully generated.Please copy that keys and save it in your code editor.

token5
 

OPENAI_API_KEY=<Your OpenAI API Key> PINATA_API_KEY=XXXXXXXX PINATA_SECRET_API_KEY=XXXXXXXXXX

 

Step 2: PDF Upload using Pinata

We’ll use Pinata’s API to upload PDFs and get a hash (CID) for each file. 

Step 3: Setting up OpenAI
Next, we’ll create a function that uses the OpenAI API to interact with the text extracted from the PDF. We’ll leverage OpenAI’s gpt-4o or gpt-4o-mini model for chat responses.

 

Step 4: Building the Streamlit Interface

Now that we have our helper functions ready, it’s time to build the Streamlit app that will upload PDFs, fetch responses from OpenAI, and display the chat.

 

 

Step 5: Running the App

To run the app locally, use the following command:
 

streamlit run filename.py

 

Our file is successfully uploaded in Pinata Platform :
final uploading