Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

Repository files navigation

AI-Powered Tax Sensitization System

An intelligent tax sensitization system that automates transaction Sensitization, reduces manual review work by up to 70%, and enables real-time anomaly detection. Built with FastAPI, OpenAI GPT-4o-mini, and Google Sheets.

The Problem

Modern enterprises operate on financial systems that weren't designed with tax in mind. This results in:

  • 90% of transactions missing tax codes
  • 60% of transactions with zero tax amounts
  • Inconsistent data formats and missing tax context
  • Tax teams spending 70% of their time fixing data instead of analyzing it

The Solution

Instead of replacing entire ERP systems, this system layers intelligent automation on top of existing systems:

  • AI Classification: Infers missing tax attributes from transaction descriptions
  • Validation: Compares AI suggestions against reference tax rates
  • Anomaly Detection: Flags transactions requiring human review
  • Automated Routing: Separates clean transactions from those needing review

Key Features

🤖 AI-Powered Classification

  • Automatically classifies transaction types (supplies, services, equipment, software, etc.)
  • Determines taxability status and suggests appropriate tax rates
  • Provides confidence scores (0-1 scale) for routing decisions
  • Generates rationale explanations for each classification

✅ Automated Validation

  • Compares AI-suggested rates against reference tax data
  • Validates with configurable tolerance (±0.25% default)
  • Handles missing reference data gracefully

🚨 Anomaly Detection

  • Missing Tax Detection: Flags taxable items with zero tax amounts
  • Rate Mismatch Detection: Identifies discrepancies between AI and reference rates
  • Covers 70% of common tax issues automatically

📊 Automated Workflow

  • Processes 100+ transactions per request
  • Manual trigger endpoint (FIRST PRIORITY)
  • Scheduled daily processing (SECOND PRIORITY)
  • Email notifications with summary statistics

🏗️ Production-Ready Architecture

  • Service Abstraction: Clean AI service interface supports multiple providers (OpenAI, Claude, Gemini, local models)
  • Transaction-Level Resilience: Individual failures don't stop batch processing
  • Cost-Optimized: Uses GPT-4o-mini (1/200th the cost of GPT-4)
  • Modular Design: Separated services, processors, and workflows for maintainability

Performance Metrics

  • Throughput: 100+ transactions per request
  • Efficiency: Reduces manual review work by up to 70%
  • Accuracy: High-confidence classifications (>0.85) for most standard transactions
  • Cost: ~$0.15/$0.60 per 1M tokens (GPT-4o-mini)

Quick Start

Prerequisites

  • Python 3.9 or higher
  • Google Cloud Project with APIs enabled:
    • Google Sheets API
    • Gmail API
    • Google Drive API
  • OpenAI API account
  • Google account for OAuth authorization

Installation

# Clone the repository
git clone <repository-url>
cd TaxSensitizationProject

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Configuration

  1. Set up Google Cloud OAuth (one-time setup):

    • Go to Google Cloud Console
    • Create a project and enable: Google Sheets API, Gmail API, Google Drive API
    • Configure OAuth consent screen
    • Create OAuth 2.0 Client ID (Desktop app type)
    • Download credentials as credentials.json in project root
  2. Set up Google Sheet:

    • Create a new Google Sheet with tabs:
      • Raw Transactions - Input transaction data
      • Tax Reference Data - Tax rates by jurisdiction
    • Share with your Google account
    • Copy the Sheet ID from the URL
  3. Configure Environment Variables:

Create .env file:

# Google OAuth
GOOGLE_CREDENTIALS_FILE=credentials.json
GOOGLE_TOKEN_FILE=token.json

# Google Sheets
GOOGLE_SHEETS_SPREADSHEET_ID=your_sheet_id

# Gmail API
GMAIL_API_ENABLED=true
EMAIL_FROM=your-email@gmail.com
EMAIL_TO=recipient@company.com

# OpenAI
OPENAI_API_KEY=sk-your-api-key
OPENAI_MODEL=gpt-4o-mini

# Optional
SCHEDULE_CRON=0 6 * * *  # Daily at 6 AM
LOG_LEVEL=INFO

Run the Application

uvicorn app.main:app --reload

On first run, the application will guide you through OAuth authorization:

  • Interactive: Automatically opens browser
  • WSL: Provides instructions for wslview or explorer.exe
  • Headless: Displays URL for manual copy/paste

Visit: http://localhost:8000/docs

Test the System

Trigger manual processing:

curl -X POST http://localhost:8000/api/v1/process/trigger

API Endpoints

Process Transactions

POST /api/v1/process/trigger

Manually trigger the tax classification workflow (FIRST PRIORITY).

Check Status

GET /api/v1/process/status

Returns current processing status (idle, processing, completed, failed).

Get Summary

GET /api/v1/process/summary

Returns last processing summary statistics including:

  • Total transactions processed
  • Auto-validated count and percentage
  • Review needed count
  • Anomaly breakdown

Health Check

GET /api/v1/health

Verifies service is running.

System Architecture

Processing Flow

Manual Trigger / Cron → OAuth Auth → Read Sheets → Preprocess → 
AI Classify → Validate → Detect Anomalies → Route → 
Write Sheets → Calculate Summary → Send Email

AI Service Abstraction

The system uses a clean abstraction pattern that allows swapping AI providers:

# Abstract interface
class AIService:
    def classify_transaction(self, transaction) -> ClassificationResponse:
        raise NotImplementedError

# OpenAI implementation (example)
class OpenAIService(AIService):
    def classify_transaction(self, transaction):
        # OpenAI-specific implementation
        ...

# Dependency injection
ai_service = get_ai_service()  # Returns configured implementation

This architecture naturally supports:

  • OpenAI (current implementation)
  • Claude (Anthropic)
  • Gemini (Google)
  • Local models
  • Any provider implementing the interface

Priority System

  1. FIRST PRIORITY: Manual trigger endpoint (POST /api/v1/process/trigger)
  2. SECOND PRIORITY: Scheduled cron jobs (default: daily at 6 AM)

Google Sheet Structure

Raw Transactions Tab

Column Type Required Description
transaction_id Text Yes Unique identifier
description Text Yes Transaction description
amount Number Yes Transaction amount
location Text Yes State code (TX, CA, etc.)
tax_code Text No Often null
tax_amount Number No Often 0 or null

Tax Reference Data Tab

Column Type Required Description
location Text Yes State code (TX, CA)
expected_tax_rate Text Yes Rate as "8.25%"
jurisdiction_name Text No Full state name

Output Sheets (Auto-Created)

  • Tax-Ready Output: Clean, validated transactions
  • Needs Review: Flagged transactions requiring manual review

How It Works

1. AI Classification

The AI service analyzes each transaction and infers:

  • Transaction type (supplies, services, equipment, etc.)
  • Taxability status (Taxable/Non-taxable)
  • Suggested tax rate for the jurisdiction
  • Confidence score (0-1 scale)
  • Rationale explanation

Prompt Engineering:

  • System prompt: Establishes AI role as tax classification assistant
  • User prompt: Provides transaction details and classification requirements
  • Response format: Structured JSON for consistent parsing

2. Validation

AI-suggested rates are compared against reference tax data:

  • Rate comparison with tolerance (0.25% default)
  • Match/mismatch status assignment
  • Graceful handling of missing reference data

3. Anomaly Detection

Two high-impact patterns catch most issues:

Pattern 1: Missing Tax on Taxable Items

  • If taxable_status = "Taxable" AND tax_amount = 0
  • Flags as MissingTaxCollection
  • Catches ~50% of common issues

Pattern 2: Rate Mismatch

  • If AI rate doesn't match reference rate
  • Flags as RateMismatch
  • Catches ~20% of common issues

4. Routing Decision

  • 0 anomalies → Clean transactions (auto-validated)
  • >0 anomalies → Review queue (manual review needed)

Project Structure

TaxSensitizationProject/
├── app/
│   ├── main.py                 # FastAPI application entry point
│   ├── config.py               # Configuration management
│   ├── models/                 # Data models (Transaction, Classification)
│   ├── services/               # Service layer
│   │   ├── ai_service.py       # AI service abstraction (OpenAI implementation)
│   │   ├── sheets_service.py   # Google Sheets integration
│   │   ├── gmail_service.py    # Gmail API integration
│   │   ├── oauth_service.py    # OAuth authentication
│   │   └── scheduler.py        # Task scheduling
│   ├── processors/             # Data processing
│   │   ├── preprocessor.py     # Data normalization
│   │   ├── validator.py        # Rate validation
│   │   └── anomaly_detector.py # Anomaly detection
│   ├── workflows/              # Workflow orchestration
│   │   └── tax_sensitization.py # Main processing workflow
│   ├── api/                    # API routes
│   │   └── routes.py           # FastAPI endpoints
│   └── tests/                  # Test suite
├── .projectinfo/               # Project documentation
├── requirements.txt
├── .env                        # Environment variables 
└── README.md

Key Challenges Solved

Challenge 1: OAuth in WSL/Headless Environments

Solution: Environment detection with appropriate flow for each scenario (interactive, WSL, headless).

Challenge 2: AI API Rate Limits & Costs

Solution:

  • GPT-4o-mini (cost-effective)
  • Retry logic with exponential backoff
  • Small delays between requests
  • Structured output reduces parsing errors

Challenge 3: Transaction Processing Resilience

Solution: Transaction-level error handling ensures batch processing continues despite individual failures.

Challenge 4: Provider Flexibility

Solution: Service abstraction pattern allows swapping AI providers without framework changes.

Troubleshooting

OAuth Issues

  • Missing credentials.json: Download OAuth credentials from GCP Console
  • Browser doesn't open (WSL): Use wslview <url> or explorer.exe <url>
  • Access denied: Ensure all requested permissions are granted

Google Sheets Issues

  • Sheet not found: Verify Sheet ID and sharing permissions
  • Permission denied: Ensure OAuth account has Editor access

AI Classification Issues

  • API rate limits: System includes automatic retry with exponential backoff
  • JSON parsing errors: Structured output format minimizes parsing issues
  • High costs: Using GPT-4o-mini keeps costs low (~$0.15/$0.60 per 1M tokens)

Development

Run with Auto-Reload

uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

API Documentation

VS Code Debugging

  • Press F5 and select "FastAPI: Run Server (Debug)"
  • Set breakpoints by clicking left margin
  • See .vscode/DEBUG_GUIDE.md for complete guide

Security Notes

  • OAuth tokens stored in token.json (gitignored)
  • Never commit .env file or OAuth credentials
  • Use API_KEY environment variable for endpoint authentication in production
  • Review OAuth consent screen before deploying

TL;DR: AI-powered tax classification system that processes 100+ transactions per request, reduces manual review work by 70%, and automatically flags tax anomalies. Built with FastAPI, OpenAI GPT-4o-mini, and Google Sheets. Clean service abstraction allows using different AI providers as needed.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages