File size: 5,688 Bytes
ac98d23
 
 
 
 
 
 
6dd3d42
ac98d23
 
 
ae277d6
 
7703449
 
ae277d6
 
 
 
 
 
 
 
 
0e07197
 
 
 
 
bc5c925
4f45b17
0e07197
 
ae277d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f56b18
ae277d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f56b18
ae277d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba10c56
ae277d6
 
4f45b17
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
---
title: FinAI-Agent
emoji: πŸ’°
colorFrom: indigo
colorTo: green
sdk: streamlit
app_file: app.py
python_version: "3.13"    # ← specify your Python version
pinned: false
---

# Finance-Agent πŸ€–πŸ’°

[![Python](https://img.shields.io/badge/python-3.13-blue.svg)](https://www.python.org/)
[![Streamlit](https://img.shields.io/badge/Streamlit-App-red)](https://streamlit.io/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

---

## πŸš€ What Is This?

**Finance-Agent** is your chat-native **CFO co-pilot**.  
Ask plain-English questions about your company’s Excel ledger, and get **instant answers with context + charts**.

---
## TRY IT NOW!!

https://huggingface.co/spaces/Sbboss/FinAIAgent

![Demo](content/img.png)

---

### Example

You ➜ β€œCompare EBITDA and cash runway for Q1 vs Q2, convert EUR to USD.”

Agent ➜ β€œEBITDA Q1: $1.42 M, Q2: $1.65 M (+16.4 %).

Cash runway improved from 7.1 β†’ 8.3 months.”

---

## ✨ Feature Tour
- **Natural-Language Querying** β€” Gemini 2.5 Flash interprets finance jargon and casual questions alike.  
- **Dynamic Tool Routing** β€” LangChain React agent picks one or more of 7 custom Python tools (revenue variance, gross-margin %, OpEx breakdown…).  
- **Smart Currency Handling** β€” Detects EUR rows, fetches FX sheet, normalizes to USD.  
- **Date Inference** β€” β€œThis year”, β€œlast 3 months”, β€œJun’25” β†’ precise periods.  
- **Chart Factory** β€” Generates PNGs via Matplotlib (line, bar, area, stacked).  
- **Streamlit UI** β€” Slack-style sidebar, message persistence, spinner feedback.  
- **Excel Plug-and-Play** β€” Works with a single `data.xlsx` containing 4 sheets: `actuals`, `budget`, `cash`, `fx`.  
- **Fallback PythonREPL** β€” If no tool fits, agent writes ad-hoc Pandas code.  
- **Pytest Suite** β€” Automated tests for tool selection, calc accuracy, caching, and rendering.  
- **One-click Deploy** β€” Just `streamlit run app.py`.  

---

## πŸ—οΈ Architecture
```text
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€ Chat UI (Streamlit) ───────┐
β”‚  User Input + Chat History         β”‚
β”‚      ↓ finance_agent.invoke()      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚
                β–Ό
β”Œβ”€β”€ LangChain AgentExecutor ──────────────┐
β”‚  Prompt (system + history + input)      β”‚
β”‚  LLM: Gemini 2.5 Flash                  β”‚
β”‚  React tool-calling loop                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚ selects & runs
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Custom Tools                           β”‚
β”‚  β€’ get_revenue_variance                 β”‚
β”‚  β€’ get_gross_margin_pct                 β”‚
β”‚  β€’ get_opex_breakdown                   β”‚
β”‚  β€’ get_ebitda_proxy                     β”‚
β”‚  β€’ get_cash_runway                      β”‚
β”‚  β€’ plot_chart                           β”‚
β”‚  β€’ code_analysis (PythonREPL)           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```
---

## πŸ“‚ Folder Structure
```text
finance-agent/
β”œβ”€ agent/
β”‚  β”œβ”€ agent.py        # creates cached AgentExecutor
β”‚  β”œβ”€ utils.py        # numeric calculations
β”‚  └─ tests/          # pytest suite
β”œβ”€ data.xlsx          # sample ledger
β”œβ”€ app.py             # Streamlit front-end
β”œβ”€ requirements.txt
└─ .streamlit/
   └─ secrets.toml    # custom theme & font
```
---

## βš™οΈ Installation

```bash
git clone https://github.com/Sbboss/FinAIAgent.git
cd FinAIAgent
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
pip install -r requirements-dev.txt
streamlit run app.py
```
πŸƒ Quick Start

Drop your company ledger into data.xlsx with sheets: actuals, budget, cash, fx.

1. Run the app:
```bash
streamlit run app.py
```
2. Ask away:
```
β€œTrend gross margin % monthly for 2024.”
β€œWhy did revenue miss budget in Nov 25?”
β€œHow many months of runway if burn stays flat?”
```
πŸ‘‰ Charts appear inline; numeric answers include deltas vs budget and YOY.

___

πŸ”¬ Testing
```
pip install -r requirements-dev.txt
pytest -v
```
Covers:
Tool parameter validation
Correct tool selection via intermediate steps
Currency conversion accuracy
Streamlit image rendering
Caching behaviour (st.cache_resource)

---

πŸ› οΈ Configuration

| Setting                | File / Env               | Default               |
| ---------------------- | ------------------------ | --------------------- |
| Google Gemini API key  | `GOOGLE_API_KEY`         | put in `secrets.toml` |
| Excel file path        | `data.xlsx`              | β€”                     |
| LLM temperature        | agent                    | `0.2`                 |

---

πŸŒ… Roadmap

* πŸ”„ CSV and SQL connectors
* πŸ—ƒοΈ Vector memory for conversation continuity
* 🌐 Multi-LLM failover (Gemini β†’ GPT-4o)
* πŸ“ˆ Automatic dashboard snapshots to PDF
* πŸ’¬ Slack / Teams integration
* πŸŽ₯ Demo GIF: live chat β†’ chart β†’ answer

---

πŸ™Œ Contributing

1. Fork & branch (feat/your-feature)
2. Add tests (pytest -k your_test)
3. Submit PR with concise description
   
βœ… All significant changes must pass CI and keep coverage β‰₯ 95%.

---

πŸ“ License
MIT β€” see [LICENSE](LICENSE)
Finance-Agent turns spreadsheets into strategy. Ask. Analyze. Act. ⚑

---