Perk lands $300M credit facility to push its AI platform into the US
Back to Tutorials
techTutorialintermediate

Perk lands $300M credit facility to push its AI platform into the US

June 4, 20266 views5 min read

Learn to build an AI-powered spend management platform that analyzes corporate expenses and provides intelligent recommendations, similar to what Perk is developing for enterprise clients.

Introduction

In this tutorial, we'll explore how to build and deploy an AI-powered spend management platform similar to what companies like Perk are developing. We'll focus on creating a core AI module that can analyze corporate spending patterns and provide intelligent recommendations. This tutorial assumes you have a basic understanding of Python, machine learning concepts, and REST API development.

Understanding how companies like Perk leverage AI in spend management is crucial for modern business operations. The platform we'll build will demonstrate key concepts such as data preprocessing, pattern recognition, and intelligent decision-making systems.

Prerequisites

  • Python 3.8 or higher installed
  • Basic understanding of machine learning concepts
  • Experience with REST APIs and Flask
  • Knowledge of data analysis with Pandas and NumPy
  • Access to a database (SQLite for this tutorial)
  • Basic understanding of JSON and API interactions

Step-by-Step Instructions

1. Set up the project structure

First, create a project directory and set up the basic structure for our AI spend management system.

mkdir perk_ai_platform
 cd perk_ai_platform
 mkdir api models data
 touch app.py requirements.txt

This structure separates our API layer, machine learning models, and data handling components, following good software architecture practices.

2. Install required dependencies

Create a requirements.txt file with the necessary packages:

flask==2.3.3
flask-cors==4.0.0
scikit-learn==1.3.0
pandas==2.0.3
numpy==1.24.3
sqlalchemy==2.0.15

Install the dependencies:

pip install -r requirements.txt

We're using Flask for the API, scikit-learn for machine learning, and SQLAlchemy for database operations. These tools are industry-standard for building scalable AI applications.

3. Create the database model

Set up the database schema in models/database.py:

from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime

Base = declarative_base()


class Expense(Base):
    __tablename__ = 'expenses'

    id = Column(Integer, primary_key=True)
    employee_id = Column(String(50))
    category = Column(String(100))
    amount = Column(Float)
    date = Column(DateTime, default=datetime.utcnow)
    vendor = Column(String(200))
    status = Column(String(50), default='pending')


class Recommendation(Base):
    __tablename__ = 'recommendations'

    id = Column(Integer, primary_key=True)
    expense_id = Column(Integer)
    recommendation_type = Column(String(100))
    confidence_score = Column(Float)
    description = Column(String(500))
    created_at = Column(DateTime, default=datetime.utcnow)

This database design captures the essential data needed for spend analysis and recommendation generation.

4. Implement the AI recommendation engine

Create the AI logic in models/recommendation_engine.py:

import pandas as pd
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import IsolationForest
import numpy as np


class SpendRecommendationEngine:
    def __init__(self):
        self.scaler = StandardScaler()
        self.cluster_model = KMeans(n_clusters=3, random_state=42)
        self.anomaly_detector = IsolationForest(contamination=0.1, random_state=42)

    def preprocess_data(self, expenses_df):
        # Create features for clustering
        features = expenses_df[['amount', 'category_encoded']].copy()
        return features

    def cluster_expenses(self, expenses_df):
        # Encode categories numerically
        expenses_df['category_encoded'] = pd.Categorical(expenses_df['category']).codes
        
        features = self.preprocess_data(expenses_df)
        scaled_features = self.scaler.fit_transform(features)
        
        # Apply clustering
        clusters = self.cluster_model.fit_predict(scaled_features)
        expenses_df['cluster'] = clusters
        
        return expenses_df

    def detect_anomalies(self, expenses_df):
        # Encode categories numerically
        expenses_df['category_encoded'] = pd.Categorical(expenses_df['category']).codes
        
        features = self.preprocess_data(expenses_df)
        scaled_features = self.scaler.transform(features)
        
        # Detect anomalies
        anomalies = self.anomaly_detector.fit_predict(scaled_features)
        expenses_df['is_anomaly'] = anomalies
        
        return expenses_df

    def generate_recommendations(self, expenses_df):
        recommendations = []
        
        # Group by cluster and generate recommendations
        for cluster_id in expenses_df['cluster'].unique():
            cluster_data = expenses_df[expenses_df['cluster'] == cluster_id]
            avg_amount = cluster_data['amount'].mean()
            
            if avg_amount > 1000:  # High-value cluster
                recommendations.append({
                    'type': 'budget_review',
                    'confidence': 0.85,
                    'description': f'High-value spending cluster detected. Consider budget review for {cluster_id} category.'
                })
            elif avg_amount < 50:  # Low-value cluster
                recommendations.append({
                    'type': 'optimization',
                    'confidence': 0.75,
                    'description': f'Low-value spending detected. Consider optimization opportunities for {cluster_id} category.'
                })
        
        return recommendations

This engine uses clustering to group similar expenses and anomaly detection to identify unusual spending patterns, both key components of intelligent spend management systems.

5. Build the Flask API

Implement the main API in app.py:

from flask import Flask, request, jsonify
from flask_cors import CORS
from models.database import Base, engine, Expense, Recommendation
from models.recommendation_engine import SpendRecommendationEngine
from sqlalchemy.orm import sessionmaker
import pandas as pd

app = Flask(__name__)
CORS(app)

# Initialize database
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)

engine = SpendRecommendationEngine()

@app.route('/expenses', methods=['POST'])
def add_expense():
    session = Session()
    try:
        data = request.get_json()
        expense = Expense(
            employee_id=data['employee_id'],
            category=data['category'],
            amount=data['amount'],
            vendor=data['vendor']
        )
        session.add(expense)
        session.commit()
        return jsonify({'message': 'Expense added successfully'}), 201
    except Exception as e:
        session.rollback()
        return jsonify({'error': str(e)}), 400
    finally:
        session.close()

@app.route('/recommendations', methods=['GET'])
def get_recommendations():
    session = Session()
    try:
        # Get all expenses
        expenses = session.query(Expense).all()
        if not expenses:
            return jsonify({'recommendations': []}), 200
        
        # Convert to DataFrame
        expenses_data = [{
            'id': e.id,
            'employee_id': e.employee_id,
            'category': e.category,
            'amount': e.amount,
            'vendor': e.vendor
        } for e in expenses]
        
        df = pd.DataFrame(expenses_data)
        
        # Process with AI engine
        clustered_df = engine.cluster_expenses(df)
        anomaly_df = engine.detect_anomalies(clustered_df)
        recommendations = engine.generate_recommendations(anomaly_df)
        
        return jsonify({'recommendations': recommendations}), 200
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

This API provides endpoints for adding expenses and retrieving AI-powered recommendations, simulating how a real spend management platform would function.

6. Test the system

Create a test script to validate our implementation:

import requests
import json

# Test data
expenses = [
    {'employee_id': 'EMP001', 'category': 'Travel', 'amount': 1200.0, 'vendor': 'Airline Inc'},
    {'employee_id': 'EMP002', 'category': 'Meals', 'amount': 75.0, 'vendor': 'Restaurant Co'},
    {'employee_id': 'EMP003', 'category': 'Travel', 'amount': 2500.0, 'vendor': 'Hotel Corp'},
    {'employee_id': 'EMP004', 'category': 'Office Supplies', 'amount': 15.0, 'vendor': 'Office Store'}
]

# Add expenses
for expense in expenses:
    response = requests.post('http://localhost:5000/expenses', 
                           json=expense)
    print(f"Added expense: {response.json()}")

# Get recommendations
response = requests.get('http://localhost:5000/recommendations')
print("Recommendations:")
print(json.dumps(response.json(), indent=2))

Run this test script to verify that our system correctly processes expenses and generates intelligent recommendations.

Summary

This tutorial demonstrated how to build a foundational AI-powered spend management system similar to what companies like Perk are implementing. We created a system that can cluster similar expenses, detect anomalies, and generate actionable recommendations. The key components include:

  • Database design for storing expense data
  • Machine learning algorithms for pattern recognition
  • REST API for system interaction
  • Integration of AI insights into business operations

While this is a simplified implementation, it showcases the core architecture and concepts that power enterprise spend management platforms. Real-world implementations would include more sophisticated models, better data handling, and integration with existing corporate systems.

Source: TNW Neural

Related Articles