Automating Bill Splitting with AI: A Practical Guide for Developers

Pavan Kunchala
4 min readDec 31, 2024

--

Introduction

Splitting bills can be frustrating and time-consuming, especially when dealing with complex receipts or shared expenses. What if we could use AI to automate this process? đź’ˇ

In this blog, we’ll walk through a Python-based tool that:

  • Processes a PDF bill, converts it into Markdown.
  • Analyzes itemized purchases.
  • Generates detailed cost breakdowns, including shared expenses.

The project leverages VisionParser for PDF processing and CrewAI for AI-powered task orchestration. You can find the complete code on GitHub and even collaborate to build more exciting features!

Step 1: Setting Up the Environment

Before diving into the code, let’s set up the environment:

Dependencies

  • Python 3.8+
  • crewai for orchestrating AI tasks.
  • vision-parse for converting PDF bills to text.

Install the dependencies using:

pip install crewai vision-parse

Ensure your language model server (e.g., Ollama) is running:

ollama serve

Step 2: Converting PDF to Markdown

The first step is to extract text from a bill PDF and convert it into Markdown format. This makes it easier to process and analyze.

Code Explanation

from vision_parse import VisionParser, PDFPageConfig
def convert_pdf_to_markdown(pdf_path: str) -> str:
# Configure how the PDF pages will be processed
page_config = PDFPageConfig(
dpi=400, # High DPI for better text recognition
color_space="RGB", # Use RGB color space
include_annotations=False, # Ignore annotations in the PDF
preserve_transparency=True # Maintain transparency in the document
)
# Initialize the VisionParser
parser = VisionParser(
model_name="llama3.2-vision:11b", # AI model for text extraction
temperature=0.2, # Controls randomness in extraction
top_p=0.3, # Focus on top likely extractions
extraction_complexity=False, # Use simple extraction for efficiency
page_config=page_config # Page settings defined above
)
# Process the PDF and return content as Markdown
markdown_pages = parser.convert_pdf(pdf_path)
result = "\n\n".join([f"--- Page {i+1} ---\n{content}" for i, content in enumerate(markdown_pages)])
return result

This function processes a PDF file, page by page, and converts it into Markdown text. The Markdown format provides a structured way to visualize the bill’s content.

Step 3: Defining the AI Agent

An Agent in CrewAI represents an AI persona with a specific role, goal, and backstory. In this case, the agent is a “Bill Splitter.”

Code Explanation

from crewai import Agent, LLM
# Initialize the language model
llm = LLM(
model='ollama/mistral-nemo', # Specify the LLM model
base_url='http://localhost:11434' # URL where the model server is running
)
# Define the agent
bill_splitter_agent = Agent(
role="Bill Splitter", # The AI persona's role
goal="Analyze the provided bill and determine the cost per person based on their purchases.",
backstory=(
"You are highly skilled at reading and interpreting complex billing documents. "
"With an eye for detail, you ensure fairness by accurately breaking down bills "
"into individual costs based on what each person bought. Everyone relies on you "
"to resolve disputes over shared expenses."
),
verbose=True, # Provide detailed output for debugging
llm=llm # Assign the language model to this agent
)

The Bill Splitter Agent is designed to read Markdown content, understand user inputs, and calculate individual costs while handling shared expenses.

Step 4: Structuring Inputs

For our tool to analyze purchases, we need a structured way to provide data.

Input Example

inputs = {
"markdown_content": markdown_content, # Processed content from the PDF
"purchases": {
"Pavan": ["protein bars", "milk"],
"Bob": ["veg tofu"],
"Charlie": ["protein powder", "turmeric gummy"]
}
}
  • Markdown Content: Extracted text from the bill.
  • Purchases: Maps users to the items they are responsible for.

Step 5: Orchestrating Tasks

The Task defines the logic for splitting the bill. CrewAI coordinates the process and outputs a detailed Markdown file.

Code Explanation

from crewai import Task, Crew, Process
# Define the task
split_bill_task = Task(
description=(
"Analyze the provided bill in Markdown format and determine the cost per person "
"based on the description of what each person bought.\n\n"
"Inputs:\n"
"- Bill Markdown Content: {markdown_content}\n"
"- Purchases Description: {purchases}\n\n"
"Divide shared items equally and include detailed calculations for each user."
),
expected_output=(
"A detailed breakdown of the total bill and individual costs based on purchases. "
"Output the result in a clear Markdown format."
),
agent=bill_splitter_agent, # Assign the Bill Splitter Agent
output_file='bill_output.md' # Save the results in a Markdown file
)
# Create the crew
crew = Crew(
agents=[bill_splitter_agent],
tasks=[split_bill_task],
process=Process.sequential # Tasks are executed sequentially
)
# Execute the task
result = crew.kickoff(inputs=inputs)
print(result)

Encourage Collaboration

The project is open for contributions! Here are some ways to expand it:

  • Add Tax Handling: Automatically include tax in shared expenses.
  • Advanced Input Parsing: Extract itemized data directly from the Markdown.
  • Visualization: Generate pie charts or graphs for cost summaries.

Check out the full code on GitHub and let’s collaborate to make this tool even better!

Conclusion

By combining AI with practical coding, we’ve automated a tedious task and made it efficient and transparent. This project showcases the power of tools like VisionParser and CrewAI in solving real-world problems.

Have ideas or improvements? Let’s connect and build something amazing together! 🚀

I know there is a lot more to improve with my explanation and its mostly code, I will try to update them as quickly as possible and give your suggestions in my email(possible improvements or topics I should write an article about) and If you want to talk about this or any LLM or computer vision topics do message me on my Linkedin here

--

--

Pavan Kunchala
Pavan Kunchala

Written by Pavan Kunchala

Machine learning & Computer Vision Engineer |Deep learning and Reinforcement learning enthusiast

No responses yet