This project offers a simple OCR solution for expense documents through AI (Mistral).
First you need to add the aws sdk lambda to your Gemfile:
gem 'aws-sdk-lambda'
Then run :
bundle install
To invoke the function:
# Get your AWS credentials
aws_env = Rails.application.credentials.dig(Rails.env.to_sym, :aws)
credentials = Aws::Credentials.new(aws_env[:access_key_id], aws_env[:secret_access_key])
# Create Lambda client
lambda_client = Aws::Lambda::Client.new(aws_env[:access_key_id], aws_env[:secret_access_key])
# Define the payload
payload = { url: "DOCUMENT_URL", content_type: "DOCUMENT_TYPE"}.to_json
# Invoke the function
response = lambda_client.invoke({
function_name: "expenseOCR",
payload: payload,
invocation_type: 'RequestResponse',
})
# Get results
result = JSON.parse(response.payload.string)
In the payload, you need to pass the following parameters:
url
: URL of the document to be processedcontent_type
: type of the document (image/png
,image/jpeg
,application/pdf
)
The response will be a JSON object with the following structure:
{
"amount": 123.45,
"currency": "EUR",
"date": "2025-03-14",
"frequency": "one-time",
"category": "meal",
"comment": "This expense was categorized as 'meal' because it corresponds to a restaurant bill for a single person, which is generally deductible if related to business."
}
frequency
can beperiodic
orone-time
.category
is the category of the expense defined by the AI (see categorization section).
The possible categories are based on the Embarq App :
- meal (e.g., restaurant, catering, food delivery)
- telco (e.g., phone, internet, communication services)
- transport_lodging (e.g., taxi, flight, hotel, car rental)
- supplies (e.g., office supplies, equipment purchases)
- client_gift (e.g., gifts, promotional items, entertainment expenses, restaurant for more than one person)
- misc (if no other category fits).
To allow your app to access this function, you must add this IAM policy to the aws user that will be running the function:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "lambda:InvokeFunction",
"Resource": "arn:aws:lambda:eu-west-3:057449144315:function:expenseOCR"
}
]
}
Also, you need to ask an Embarq AWS admin to update the Lambda function's permissions :
- Go to AWS Lambda Console Lambda Dashboard.
- Select ExpenseOCR function.
- Go to Configurations -> Permissions.
- Under Resource-based policy, click Add permissions → AWS Account.
- Enter the AWS Account ID of the external account.
- Choose lambda:InvokeFunction as the action.
- Click Save.
You can test manually by running the following command :
rake run
Don't forget to set the variables DOCUMENT_URL
and DOCUMENT_TYPE
in the .env file.