Building an Invoice Scanner with OpenAI, LangChain, and Streamlit

Karthikeyan Rathinam
4 min readNov 30, 2023

--

In today’s digital age, automating tasks like extracting invoice details from documents can significantly streamline business processes. In this tutorial, we’ll guide you through creating an Invoice Scanner using OpenAI’s GPT model, LangChain for chat-based interaction, and Streamlit for a user-friendly interface.

Just in touch with Karthikeyan Rathinam: Linkedin, GitHub, Youtube

Prerequisites

Make sure you have the required Python packages installed. Create a requirements.txt file with the following content:

openai==0.27.8
docx2txt==0.8
langchain==0.0.225
PyPDF2==3.0.1
python-dotenv==1.0.0
streamlit==1.24.0
pdfplumber

Install the dependencies using:

pip install -r requirements.txt

Setting Up Your Environment

  1. Obtain OpenAI API Key:
  • Create a .env file in your project directory.
  • Add your OpenAI API key to the file:
OPENAI_API_KEY="sk-(Replace Your OPENAI API Key)"
  • Make sure to replace (Replace Your OPENAI API Key) with your actual OpenAI API key.

Run the Application:

streamlit run app.py
  1. This will start a local server. Open your browser and navigate to http://localhost:8501 to access the Invoice Scanner.
  2. For Colab users, use the following commands:

For Local Machine & Colab

!streamlit run app.py &>/content/logs.txt & npx localtunnel --port 8501 & curl ipv4.icanhazip.com

Copy the IPv4 address from the output and paste it on the localtunnel page.

Understanding the Code

Let’s break down the key components of the app.py file:

Function Definitions

The project includes predefined functions for handling different types of invoices. Here’s an example function definition:

{
"name": "invoice",
"description": "Get invoice information to extract values if not available value fill 'NA'",
"parameters": {
// ... (see provided code for detailed parameters)
},
"required": ["from", "Contact for Sender", "to", "Contact for Recipient", "invoice_number", "order_number", "invoice_date", "total", "invoice_payment", "invoice_status", "invoice_notes"]
}

Streamlit Interface

The Streamlit interface is designed to be user-friendly. Users can upload PDF or Word documents containing invoices, and the application will scan and extract relevant details.

LangChain Integration

LangChain is used for chat-based interaction with OpenAI’s GPT model. The ChatOpenAI class facilitates communication with the language model.

Handling PDF and Word Documents

The application uses pdfplumber and docx2txt to extract text from PDF and Word documents, respectively. It then formats the text as a prompt and sends it to the OpenAI model for processing.

Conclusion

Congratulations! You’ve built a powerful Invoice Scanner using OpenAI, LangChain, and Streamlit. This tool can be a game-changer for businesses dealing with numerous invoices by automating the extraction of relevant information.

Feel free to customize and expand the functionality further based on your specific requirements. This project serves as a great starting point for integrating AI into document processing workflows.

Happy coding! 🚀

GitHub Repository : https://github.com/karthikeyan-rathinam/Custom-Document-and-Invoice-with-generative-ai

Any Queries feel free to ask!!Follow

Feel free to reach out if you have any questions or need further assistance.

Just in touch with Karthikeyan Rathinam: Linkedin, GitHub, Youtube

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Responses (1)

Write a response