-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
128 lines (87 loc) · 2.82 KB
/
Copy pathagent.py
File metadata and controls
128 lines (87 loc) · 2.82 KB
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
from langchain_ollama import OllamaLLM
import pandas as pd
llm = OllamaLLM(model="llama3")
CATEGORIES = [
"Food",
"Transport",
"Bills",
"Shopping",
"Grocery",
"Entertainment",
"Health",
"Education",
"Other"
]
KEYWORD_MAP = {
"Food": ["restaurant", "burger", "pizza", "kfc", "mcdonald", "cafe"],
"Grocery": ["mart", "store", "grocery", "supermarket"],
"Transport": ["uber", "careem", "fuel", "petrol", "bus", "train"],
"Bills": ["electric", "wifi", "internet", "gas", "water", "bill"],
"Entertainment": ["movie", "netflix", "game", "cinema"],
"Health": ["hospital", "pharmacy", "medicine", "doctor"],
"Education": ["course", "university", "book", "udemy"],
"Shopping": ["amazon", "clothes", "shirt", "shoes"]
}
def rule_based_classification(spent_on: str):
spent_on = str(spent_on).lower()
for category, keywords in KEYWORD_MAP.items():
for word in keywords:
if word in spent_on:
return category
return None
def llm_classify(spent_on, importance, amount):
prompt = f"""
Classify the transaction into ONE of these categories:
{CATEGORIES}
Description: {spent_on}
Importance: {importance}
Amount: {amount}
Return only the category name.
If unsure, return "Other".
"""
response = llm.invoke(prompt).strip()
if response not in CATEGORIES:
return "Other"
return response
def categorize_transactions(df: pd.DataFrame):
df = df.copy()
predicted_categories = []
for _, row in df.iterrows():
spent_on = row["spent_on"]
importance = row["importance"]
amount = row["amount"]
category = rule_based_classification(spent_on)
if not category:
category = llm_classify(spent_on, importance, amount)
predicted_categories.append(category)
df["predicted_category"] = predicted_categories
return df
# AI-Generated Financial Insight
def generate_financial_insight(total_spent, category_df, monthly_df):
if category_df.empty:
return "No financial data available."
category_text = "\n".join(
[f"{row['category']}: PKR {row['total']:,.0f}"
for _, row in category_df.iterrows()]
)
monthly_text = "\n".join(
[f"{row['month']}: PKR {row['total']:,.0f}"
for _, row in monthly_df.iterrows()]
)
prompt = f"""
You are a financial advisor AI.
Analyze the user's financial data and provide:
1. A short summary of overall spending.
2. Identify the highest spending category.
3. Comment on monthly trend if visible.
4. Provide 2-3 practical improvement suggestions.
Financial Data:
Total Spending: PKR {total_spent:,.0f}
Category Breakdown:
{category_text}
Monthly Trend:
{monthly_text}
Keep the response concise and professional.
"""
response = llm.invoke(prompt)
return response.strip()