File size: 3,131 Bytes
f0179e3
 
 
0887c48
 
f0179e3
0887c48
f0179e3
 
 
3a7c1eb
3a4ffcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0887c48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
title: AI FINANCIAL INSIGHTS
emoji: πŸ“š
colorFrom: green
colorTo: yellow
sdk: docker
pinned: true
---

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference

## AI Financial Insights

AI Financial Insights provides personalized spending summaries that mirror the AI-driven budgeting direction outlined for WalletSync, helping users spot changing expense patterns before overspending occurs. The service analyzes up to the three most recent months of transaction history (or gracefully handles brand-new users) and surfaces conversational insights such as β€œYour grocery expenses increased by 22% this month compared to last month.”

### Features
- FastAPI backend with `/insights` endpoint that accepts a list of transactions and returns contextual insights per category plus overall month-over-month highlights.
- Logic considers up to the latest three months of data; if less history is available, it adapts without errors.
- Dockerfile tailored for Hugging Face Spaces (listening on port `7860`).

### Project structure
```
.
β”œβ”€β”€ app
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py          # FastAPI entrypoint
β”‚   β”œβ”€β”€ schemas.py       # Pydantic models
β”‚   └── services.py      # Insight generation logic
β”œβ”€β”€ requirements.txt
└── Dockerfile
```

### Local development
1. Create a virtual environment and install dependencies:
   ```
   python -m venv .venv
   .\.venv\Scripts\activate
   python -m pip install --upgrade pip
   python -m pip install -r requirements.txt
   ```
2. Export your Mongo connection string (provided by WalletSync) before running the API:
   ```
   setx MONGODB_URI "mongodb://expenseuser:Kem_6o%3F%3F@165.227.69.221:27017/expense?authSource=admin"
   ```
3. Run the FastAPI server (per user rule, use `python3` to launch uvicorn):
   ```
   python3 -m uvicorn app.main:app --host 0.0.0.0 --port 8000
   ```
4. Open the docs at `http://localhost:8000/docs`.

### Request example
```
POST /insights
{
  "user_id": "demo-user",
  "transactions": [
    {"timestamp": "2025-09-05T10:00:00Z", "category": "Groceries", "amount": 120.5, "currency": "INR"},
    {"timestamp": "2025-10-04T11:00:00Z", "category": "Groceries", "amount": 150.0, "currency": "INR"},
    {"timestamp": "2025-10-15T09:30:00Z", "category": "Travel",    "amount": 2000.0, "currency": "INR"}
  ]
}
```

### Docker (Hugging Face Space)
Build and test locally:
```
docker build -t ai-financial-insights .
docker run -it -p 7860:7860 ai-financial-insights
```
When deploying on Hugging Face, add `MONGODB_URI` as a Space secret so the container can reach your database.

### Deploying to Hugging Face
1. Log in and clone the Space:
   ```
   git clone https://huggingface.co/spaces/LogicGoInfotechSpaces/AI_FINANCIAL_INSIGHTS
   cd AI_FINANCIAL_INSIGHTS
   ```
2. Copy the project files into the cloned repo and commit:
   ```
   git add .
   git commit -m "Add AI Financial Insights FastAPI app"
   git push
   ```
3. The Docker Space automatically builds and serves the FastAPI app on port `7860`. Monitor the Space page for build logs and status.