👥 Customer Segmentation and Market Agent

AI-powered customer intelligence system with automatic RFM analysis and targeted campaign creation

Python 3.9+ Gemini 2.0 Flash TinyDB Natural Language Google Colab

📋 Project Overview & Problem Statement

Challenge: Marketing teams struggle with manual customer segmentation, spending days analyzing spreadsheets to identify VIP customers, at-risk segments, and campaign targets. This process is time-consuming, error-prone, and requires SQL/Python expertise that many marketers lack.

Solution: Customer Segmentation and Market Agent uses Google Gemini AI to convert natural language questions into executable Python code. It automatically creates customer profiles, performs RFM (Recency, Frequency, Monetary) analysis, and generates targeted marketing campaigns - all through simple English queries.

Key Benefits

🤖 AI Capabilities & Technical Innovation

🧠 Natural Language to Code

Gemini 2.0 Flash converts business questions into executable TinyDB queries and Pandas operations. Code-as-plan pattern ensures accuracy.

📊 Automatic Customer Profiling

Analyzes transaction history to create comprehensive customer profiles with total orders, spending, profit contribution, and favorite categories.

🎯 5-Segment Classification

VIP (>$2K spent, 5+ orders), Regular ($500-$2K), At-Risk (180+ days inactive), New (≤2 orders), Churned (365+ days)

📈 Campaign Targeting

Generate marketing campaigns with precise customer lists, segment targeting, and automatic ID tracking for CRM integration.

3-Table Database Architecture

AI Processing Pipeline

🛠️ Technical Architecture & Implementation

AI & Code Generation

Google Gemini 2.0 Flash Code-as-Plan Pattern Natural Language Processing Dynamic Query Building Safe Code Execution

Data Analytics Stack

TinyDB NoSQL Pandas DataFrames Matplotlib Charts Seaborn Visualizations NumPy Calculations

Deployment Platform

Google Colab Jupyter Notebook Cloud Storage Secrets Management

Customer Segmentation Logic

# Automatic segmentation rules: VIP: total_spent > $2,000 AND total_orders >= 5 Regular: total_spent between $500-$2,000 At-Risk: days_since_last_order > 180 New: total_orders <= 2 Churned: days_since_last_order > 365 # Each customer profile tracks: - customer_id, customer_name - total_orders, total_spent, total_profit_contributed - average_order_value - first_order_date, last_order_date, days_since_last_order - favorite_category, segment, tags

💬 Sample Questions & Use Cases

Question Type Example Query Action
Customer Intelligence "Who are our top 10 VIP customers by total spending?" Queries customers_tbl, generates bar chart
Segment Analysis "How many customers are in each segment?" Counts by segment, creates distribution chart
Campaign Creation "Create a holiday campaign targeting VIP customers" Inserts campaign record with customer IDs
At-Risk Detection "What is the average order value of at-risk customers?" Filters at-risk segment, calculates AOV
Category Analysis "Which category has the most churned customers?" Groups by favorite_category, counts churned

Advanced Query Examples

📖 Development Setup & Installation Guide

Quick Start with Google Colab (Recommended)

  1. Open Colab Notebook: Click "Launch in Google Colab" button above
  2. Add API Key: Click 🔑 icon in left sidebar → Add Secret → Name: GEMINI_API_KEY
  3. Run Setup Cells: Install dependencies (TinyDB, Pandas, Gemini)
  4. Upload CSV: Upload your e-commerce transaction dataset
  5. Ask Questions: Use marketing_agent() function with natural language queries

Dataset Requirements

Your CSV should include these columns:

Local Installation (Alternative)

# Clone the repository git clone https://github.com/lyven81/ai-project.git cd ai-project/projects/customer-segmentation-agent # Create virtual environment python -m venv venv source venv/bin/activate # Windows: venv\Scripts\activate # Install dependencies pip install -r requirements.txt # Set up environment variables cp .env.example .env # Add your Gemini API key to .env # Run in Jupyter jupyter notebook customer_segmentation_and_market_agent.py

Environment Configuration

# Required API Configuration GEMINI_API_KEY=your_gemini_api_key_here # Optional Segmentation Rules VIP_MIN_SPENT=2000 VIP_MIN_ORDERS=5 AT_RISK_DAYS_THRESHOLD=180 CHURNED_DAYS_THRESHOLD=365

🚀 Deployment Options & Usage Workflow

Google Colab Deployment (Primary Method)

# 1. Upload dataset CSV uploaded = files.upload() df = pd.read_csv('your_dataset.csv') # 2. Create database (3 tables) db, orders_tbl, customers_tbl, campaigns_tbl = create_marketing_database(df) # 3. Ask questions in natural language result = marketing_agent( "Who are our top 10 VIP customers by total spending? Show in bar chart", db=db, orders_tbl=orders_tbl, customers_tbl=customers_tbl, campaigns_tbl=campaigns_tbl, ) # 4. View generated code, execution logs, and results # The agent shows: # - Question interpretation # - Generated Python/TinyDB code # - Before/after database snapshots # - Answer with visualization

Workflow Example

Output & Transparency

📊 Performance Metrics & Business Impact

3-5s
Code Generation Time
1-2s
Query Execution Time
10,000+
Orders Processed
5
Customer Segments

Business Value Demonstration

Technical Performance