Commit
·
af904a9
1
Parent(s):
e4b048c
Fix get_recommendation_for_category to generate recommendations when category_id is a budget _id
Browse files- app/smart_recommendation.py +74 -9
app/smart_recommendation.py
CHANGED
|
@@ -382,21 +382,86 @@ class SmartBudgetRecommender:
|
|
| 382 |
if budget_amount is None:
|
| 383 |
budget_amount = budget_amount_from_budget
|
| 384 |
|
| 385 |
-
# Generate recommendation
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
elif budget_amount_from_budget > 0:
|
| 391 |
# Budget exists but no categories - use budget name and amount
|
| 392 |
budget_name = budget_by_id.get("name", "Budget")
|
| 393 |
if budget_amount is None:
|
| 394 |
budget_amount = budget_amount_from_budget
|
|
|
|
| 395 |
# Generate recommendation using budget name
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
except (ValueError, TypeError):
|
| 401 |
pass # category_id is not a valid ObjectId, continue with normal check
|
| 402 |
except Exception:
|
|
|
|
| 382 |
if budget_amount is None:
|
| 383 |
budget_amount = budget_amount_from_budget
|
| 384 |
|
| 385 |
+
# Generate recommendation using the budget's data
|
| 386 |
+
avg_expense = budget_amount
|
| 387 |
+
monthly_values = [avg_expense]
|
| 388 |
+
std_dev = avg_expense * 0.05
|
| 389 |
+
months_analyzed = 1
|
| 390 |
+
|
| 391 |
+
data = {
|
| 392 |
+
"average_monthly": avg_expense,
|
| 393 |
+
"total": avg_expense,
|
| 394 |
+
"count": 1,
|
| 395 |
+
"months_analyzed": months_analyzed,
|
| 396 |
+
"std_dev": std_dev,
|
| 397 |
+
"monthly_values": monthly_values,
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
confidence = self._calculate_confidence(data)
|
| 401 |
+
|
| 402 |
+
# Get AI recommendation
|
| 403 |
+
ai_result = self._get_ai_recommendation(category_name, data, avg_expense)
|
| 404 |
+
if ai_result and ai_result.get("recommended_budget"):
|
| 405 |
+
recommended_budget = ai_result.get("recommended_budget")
|
| 406 |
+
reason = ai_result.get("reason", f"AI recommendation for {category_name}")
|
| 407 |
+
action = ai_result.get("action")
|
| 408 |
+
else:
|
| 409 |
+
recommended_budget = avg_expense * 1.10
|
| 410 |
+
reason = f"Based on your budget of {budget_amount:,.0f}, I recommend {recommended_budget:,.0f} to account for variability and inflation."
|
| 411 |
+
action = "increase"
|
| 412 |
+
|
| 413 |
+
return [BudgetRecommendation(
|
| 414 |
+
category=category_name,
|
| 415 |
+
category_id=actual_category_id,
|
| 416 |
+
average_expense=round(avg_expense, 2),
|
| 417 |
+
recommended_budget=round(recommended_budget or 0, 2),
|
| 418 |
+
reason=reason,
|
| 419 |
+
confidence=confidence,
|
| 420 |
+
action=action
|
| 421 |
+
)]
|
| 422 |
elif budget_amount_from_budget > 0:
|
| 423 |
# Budget exists but no categories - use budget name and amount
|
| 424 |
budget_name = budget_by_id.get("name", "Budget")
|
| 425 |
if budget_amount is None:
|
| 426 |
budget_amount = budget_amount_from_budget
|
| 427 |
+
|
| 428 |
# Generate recommendation using budget name
|
| 429 |
+
avg_expense = budget_amount
|
| 430 |
+
monthly_values = [avg_expense]
|
| 431 |
+
std_dev = avg_expense * 0.05
|
| 432 |
+
months_analyzed = 1
|
| 433 |
+
|
| 434 |
+
data = {
|
| 435 |
+
"average_monthly": avg_expense,
|
| 436 |
+
"total": avg_expense,
|
| 437 |
+
"count": 1,
|
| 438 |
+
"months_analyzed": months_analyzed,
|
| 439 |
+
"std_dev": std_dev,
|
| 440 |
+
"monthly_values": monthly_values,
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
confidence = self._calculate_confidence(data)
|
| 444 |
+
|
| 445 |
+
# Get AI recommendation
|
| 446 |
+
ai_result = self._get_ai_recommendation(budget_name, data, avg_expense)
|
| 447 |
+
if ai_result and ai_result.get("recommended_budget"):
|
| 448 |
+
recommended_budget = ai_result.get("recommended_budget")
|
| 449 |
+
reason = ai_result.get("reason", f"AI recommendation for {budget_name}")
|
| 450 |
+
action = ai_result.get("action")
|
| 451 |
+
else:
|
| 452 |
+
recommended_budget = avg_expense * 1.10
|
| 453 |
+
reason = f"Based on your budget of {budget_amount:,.0f}, I recommend {recommended_budget:,.0f} to account for variability."
|
| 454 |
+
action = "increase"
|
| 455 |
+
|
| 456 |
+
return [BudgetRecommendation(
|
| 457 |
+
category=budget_name,
|
| 458 |
+
category_id=category_id,
|
| 459 |
+
average_expense=round(avg_expense, 2),
|
| 460 |
+
recommended_budget=round(recommended_budget or 0, 2),
|
| 461 |
+
reason=reason,
|
| 462 |
+
confidence=confidence,
|
| 463 |
+
action=action
|
| 464 |
+
)]
|
| 465 |
except (ValueError, TypeError):
|
| 466 |
pass # category_id is not a valid ObjectId, continue with normal check
|
| 467 |
except Exception:
|