A backend REST API for personal and business expense tracking with AI-powered automatic categorization and a natural language chatbot for financial analysis. Built with Java, Spring Boot, Python, LangChain, and Ollama.
- Transaction Logging — Record income and expense transactions with category, amount, and date
- ML Auto-Categorization — Automatically classifies expenses using a Python ML microservice (TF-IDF + Logistic Regression)
- AI Chatbot — Ask natural language questions about your expenses ("How much did I spend in June?", "What are my biggest expenses?")
- Statistical Analysis — Aggregate spending by category, time period, or custom filters
- Monthly/Weekly Reports — Query spending summaries over any date range
- Docker Support — Full stack runs with a single
docker compose up
| Layer | Technology |
|---|---|
| Language | Java 17, Python 3.12 |
| Framework | Spring Boot 4, Flask |
| ML | Scikit-learn (TF-IDF + Logistic Regression) |
| AI Chatbot | LangChain, Ollama, qwen2.5:7b |
| Database | MySQL 8 |
| ORM | JPA / Hibernate |
| Build | Gradle |
| DevOps | Docker, Docker Compose |
POST /expenses/save
↓
Spring Boot (Java)
↓
Python ML Microservice → predicts category from title
↓
MySQL Database
POST /chat
↓
Spring Boot (Java)
↓
Python Chat Microservice (LangChain + Ollama)
↓
Fetches expenses from Spring Boot → formats data → LLM generates answer
When a user submits an expense without a category, Spring Boot calls the Python ML service which predicts the category automatically. For chat queries, the chatbot fetches live expense data, pre-calculates totals in Python, and uses a local LLM (via Ollama) to generate a human-readable answer.
Located in ml-service/ — a standalone Flask app that exposes a /predict endpoint.
How it works:
- Input: expense title (e.g.
"taxi to airport") - Model: TF-IDF vectorizer + Logistic Regression pipeline
- Output: predicted category + confidence score
Supported categories: FOOD, TRANSPORT, RENT, SHOPPING, OTHER
Example:
curl -X POST http://localhost:5000/predict \
-H "Content-Type: application/json" \
-d '{"title": "taxi to airport"}'{
"title": "taxi to airport",
"category": "TRANSPORT",
"confidence": 0.9821
}Prediction examples:
| Title | Predicted Category |
|---|---|
| taxi to airport | TRANSPORT |
| grocery shopping | FOOD |
| monthly rent payment | RENT |
| new shoes nike | SHOPPING |
| netflix subscription | OTHER |
Located in chat-service/ — a Flask app powered by LangChain and Ollama for natural language financial queries.
How it works:
- User asks a question in natural language
- Python fetches live expense data from Spring Boot
- Pre-calculates totals, breakdowns, and statistics
- Local LLM (qwen2.5:7b via Ollama) generates a human-readable answer
Example questions:
| Question | Answer |
|---|---|
| "How much did I spend in June?" | Total with monthly breakdown |
| "What was my biggest expense?" | Top expenses per day |
| "How much did I spend on food?" | Category total |
| "How can I reduce my expenses?" | Financial advice based on data |
Example:
curl -X POST http://localhost:5001/chat \
-H "Content-Type: application/json" \
-d '{"question": "How much did I spend in June?"}'{
"question": "How much did I spend in June?",
"answer": "Your total expense in June 2026 was $3,212.50...",
"expense_count": 24
}Also accessible via Spring Boot:
POST /chat
{
"question": "How much did I spend on food this month?"
}
- Java 17+
- Python 3.12+
- MySQL 8+
- Docker (optional)
1. Clone the repo:
git clone https://github.com/vusal466/expense-tracker-api.git
cd expense-tracker-api2. Set up environment variables:
cp .env.example .env
# Edit .env with your values3. Start ML service:
cd ml-service
pip install flask scikit-learn numpy
python app.py4. Start Chat service:
cd chat-service
pip install flask langchain langchain-ollama langchain-community
python app.py5. Start Spring Boot:
./gradlew bootRunAPI available at http://localhost:8080/swagger-ui/index.html
docker compose up --buildAll services start automatically: MySQL, Python ML service, Chat service, Spring Boot.
Note: Ollama must be running on your host machine before starting Docker. The chat service connects to Ollama via
host.docker.internal:11434.
POST /expenses/save - Add expense (category auto-predicted by ML)
GET /expenses/all - List all expenses
GET /expenses/{id} - Get expense by ID
PUT /expenses/update/{id} - Update expense
DELETE /expenses/delete/{id} - Delete expense
GET /expenses/date/{date} - Get expenses by date
GET /expenses/summary - Income vs expense summary
GET /expenses/total - Total expenses between dates
POST /chat - Ask natural language questions about expenses
POST /expenses/save
{
"title": "taxi to airport",
"amount": 15.0,
"type": "EXPENSE"
}{
"id": 1,
"title": "taxi to airport",
"amount": 15.0,
"type": "EXPENSE",
"category": "TRANSPORT",
"date": "2026-05-31"
}Category is automatically predicted — no need to send it manually.
- Budget alerts — notify when spending exceeds set limits
- Spending predictions — forecast next month's expenses with ML
- PDF report generation — monthly expense reports
- Retrain ML model with user feedback
- JWT Authentication
- RAG Document Assistant
Vusal Jafarli — Java Backend Developer
- GitHub: @vusal466
- LinkedIn: vusaljafarli