JARVISXIRONMAN commited on
Commit
205eb5c
·
verified ·
1 Parent(s): 2714c9e

Upload budget_allocator.py

Browse files
Files changed (1) hide show
  1. backend/budget_allocator.py +93 -0
backend/budget_allocator.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from openai import OpenAI
4
+ from backend import expense_manager
5
+ load_dotenv()
6
+
7
+ client = OpenAI(
8
+ api_key=os.environ.get("GROQ_API_KEY"),
9
+ base_url="https://api.groq.com/openai/v1",
10
+ )
11
+
12
+ def suggest_budget(user_id: str, expenses=None):
13
+ """
14
+ Analyze user expenses and return a budget suggestion using Groq AI.
15
+
16
+ Input: user_id and optional expense list
17
+ Output: dictionary with numeric split + AI explanation
18
+ Example:
19
+ {
20
+ "Spending": 0.6,
21
+ "Savings": 0.3,
22
+ "Investments": 0.1,
23
+ "message": "You spend most on rent, consider lowering entertainment."
24
+ }
25
+ """
26
+ if expenses is None:
27
+ expenses = expense_manager.list_expenses(user_id)
28
+
29
+ if not expenses:
30
+ return {
31
+ "Spending": 0.6,
32
+ "Savings": 0.3,
33
+ "Investments": 0.1,
34
+ "message": "No expense data found — using default 60/30/10 split."
35
+ }
36
+
37
+ summary_lines = []
38
+ for e in expenses:
39
+ summary_lines.append(f"{e['name']}: {e['amount']} ({e['category']}) on {e['date']}")
40
+ summary_text = "\n".join(summary_lines)
41
+
42
+ prompt = f"""
43
+ You are FinSync AI, a financial budgeting assistant.
44
+
45
+ User's Expense Summary:
46
+ {summary_text}
47
+
48
+ Task:
49
+ 1. Analyze how the user spends money.
50
+ 2. Suggest an ideal percentage allocation among:
51
+ - Spending (daily essentials, bills, rent)
52
+ - Savings (emergency fund)
53
+ - Investments (future growth)
54
+ 3. Return result in strict JSON format like this:
55
+ {{
56
+ "Spending": 0.55,
57
+ "Savings": 0.30,
58
+ "Investments": 0.15,
59
+ "message": "Short reasoning or advice"
60
+ }}
61
+ """
62
+
63
+ try:
64
+ response = client.responses.create(
65
+ model=GROQ_MODEL,
66
+ input=prompt,
67
+ temperature=0.3
68
+ )
69
+ ai_text = response.output_text.strip()
70
+
71
+ import json, re
72
+ json_match = re.search(r"\{[\s\S]*\}", ai_text)
73
+ if json_match:
74
+ parsed = json.loads(json_match.group(0))
75
+ for k in ["Spending", "Savings", "Investments"]:
76
+ if k not in parsed:
77
+ parsed[k] = 0.0
78
+ return parsed
79
+ else:
80
+ return {
81
+ "Spending": 0.6,
82
+ "Savings": 0.3,
83
+ "Investments": 0.1,
84
+ "message": ai_text or "AI could not parse response properly."
85
+ }
86
+
87
+ except Exception as e:
88
+ return {
89
+ "Spending": 0.6,
90
+ "Savings": 0.3,
91
+ "Investments": 0.1,
92
+ "message": f"Groq API error: {e}"
93
+ }