File size: 6,026 Bytes
af365fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
---
title: Money Manager
emoji: 💸
colorFrom: green
colorTo: blue
sdk: gradio
sdk_version: 4.44.0
app_file: app.py
pinned: false
---

# 💸 Personal Finance Manager

A Gradio-based web application for managing personal finances with LLM-powered natural language expense logging. Log expenses like "Spent $15 on a burrito at Chipotle" and let AI parse them into organized ledger entries.

## Features**Natural Language Parsing**: Describe expenses in your own words—the LLM handles extraction
📊 **Dynamic Ledger**: Real-time table showing all expenses with sorting and filtering
💰 **Total Tracking**: Automatically calculated total spending that updates instantly
🏷️ **Smart Categorization**: Expenses are automatically categorized (Food, Transportation, Utilities, etc.)
🎨 **Clean Dashboard**: Financial-themed UI using Gradio's Soft theme
🔄 **Session Persistence**: Ledger data persists throughout your session
⚡ **Fallback Parser**: Works even without LLM API keys using rule-based parsing

## Tech Stack

- **Frontend**: Gradio (Python web framework)
- **Data**: Pandas (DataFrames)
- **LLM**: LangChain with HuggingFace Hub or OpenAI
- **Language**: Python 3.8+

## Setup

### 1. Clone the Repository
```bash
cd financemanager
```

### 2. Create Virtual Environment
```bash
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
```

### 3. Install Dependencies
```bash
pip install -r requirements.txt
```

### 4. Configure API Keys (Optional)

Copy `.env.example` to `.env` and add your API keys:

```bash
cp .env.example .env
```

Edit `.env`:
- **HuggingFace**: Get token from https://huggingface.co/settings/tokens
- **OpenAI**: Get key from https://platform.openai.com/api-keys

If you don't configure any API keys, the app will use the fallback rule-based parser.

### 5. Run the Application

```bash
python app.py
```

The app will launch at `http://localhost:7860`

## Usage

1. **Describe Your Expense**: Type a natural language description in the input box
   - Examples:
     - "Spent $15 on a burrito at Chipotle"
     - "Paid $1200 for rent today"
     - "Gas: $45.50"
     - "Movie tickets $32"

2. **Click Log Expense** or press Enter
3. **View Results**: 
   - Status message confirms the entry
   - Table updates with the new expense
   - Total spending updates automatically
   - Expenses sorted by date (newest first)

## How It Works

### LLM-Based Parsing (Recommended)
When an LLM is configured, the app sends your input to the model with this prompt:

```
Parse this expense and return JSON with:
- date (YYYY-MM-DD)
- description (what was purchased)
- category (Food, Transportation, Utilities, etc.)
- amount (numeric value)
```

The LLM returns structured JSON that the app parses and stores.

### Fallback Parser
Without an LLM, the app uses:
- **Regex** to extract dollar amounts
- **Keyword matching** for category detection
- **Current date** for entries without explicit dates

## Expense Data Structure

Each entry contains:

| Field | Example | Notes |
|-------|---------|-------|
| Date | 2024-05-01 | YYYY-MM-DD format |
| Description | Burrito at Chipotle | What was purchased |
| Category | Food | Auto-categorized |
| Amount | 15.00 | Dollar amount |

## Supported Categories

- **Food**: Restaurant, groceries, coffee
- **Transportation**: Gas, Uber, parking, taxi
- **Utilities**: Electric, water, internet, phone
- **Entertainment**: Movies, concerts, books, games
- **Rent**: Rent, mortgage, apartment
- **Other**: Uncategorized expenses

## Deployment to HuggingFace Spaces

### 1. Create a Space
- Go to https://huggingface.co/spaces
- Click "Create new Space"
- Choose "Gradio" as the SDK
- Set repository visibility to public/private

### 2. Upload Files
```bash
git clone https://huggingface.co/spaces/your-username/your-space
cd your-space
# Copy app.py, requirements.txt, .env to this directory
git add .
git commit -m "Add finance manager"
git push
```

### 3. Add Secrets
In your Space's Settings → Repository secrets, add:
- `HUGGINGFACEHUB_API_TOKEN`
- `OPENAI_API_KEY` (if using OpenAI)

The space will auto-deploy and be accessible at: `https://huggingface.co/spaces/your-username/your-space`

## Customization

### Change Theme
In `app.py`, line 214:
```python
with gr.Blocks(theme=gr.themes.Soft()) as demo:
```

Try: `Default()`, `Glass()`, `Monochrome()`, `Soft()`, `Base()`

### Add More Categories
Edit `parse_expense_fallback()` function, around line 149:
```python
categories = {
    "Shopping": ["amazon", "mall", "store", "buy"],
    "Medical": ["doctor", "pharmacy", "clinic"],
    # Add more...
}
```

### Change LLM Model
In `initialize_llm()`, line 68:
```python
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
# Try: "HuggingFaceH4/zephyr-7b-beta", "meta-llama/Llama-2-7b-chat-hf"
```

## Limitations

- ⚠️ Session data is not persisted between app restarts (no database)
- ⚠️ All amounts are in USD (no multi-currency support)
- ⚠️ LLM parsing may fail for very ambiguous inputs
- ⚠️ No built-in authentication (use for personal/private deployments)

## Future Enhancements

- [ ] CSV export functionality
- [ ] Monthly/yearly summaries with charts
- [ ] Budget alerts
- [ ] Receipt image upload
- [ ] Multi-currency support
- [ ] SQLite database for persistence
- [ ] User authentication for Spaces deployment

## Troubleshooting

### "LLM not available" Warning
The app works without an LLM. This just means it's using the fallback parser. Add an API key to `.env` to enable intelligent parsing.

### "JSON parsing error"
The LLM response format was unexpected. Try rephrasing your expense description or check your API key.

### App Hangs on Startup
- Check that your API keys are correct
- Ensure you have internet connectivity
- Try disabling the LLM by not setting environment variables

## License

MIT License - feel free to modify and deploy!

## Support

For issues or suggestions, please check the code comments or modify as needed.

---

**Happy budgeting! 💰**