Commit
·
eb33258
1
Parent(s):
b3d55f3
Add aggressive final validation to ALWAYS prevent lazy 'keep' recommendations and handle data corruption
Browse files- app/smart_recommendation.py +33 -0
app/smart_recommendation.py
CHANGED
|
@@ -484,6 +484,39 @@ class SmartBudgetRecommender:
|
|
| 484 |
else:
|
| 485 |
print(f"⚠️ OpenAI returned invalid data, using rule-based for {category_name}: {recommended_budget}")
|
| 486 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
# Create recommendation
|
| 488 |
return [BudgetRecommendation(
|
| 489 |
category=category_name,
|
|
|
|
| 484 |
else:
|
| 485 |
print(f"⚠️ OpenAI returned invalid data, using rule-based for {category_name}: {recommended_budget}")
|
| 486 |
|
| 487 |
+
# 🚨 AGGRESSIVE FINAL VALIDATION: ALWAYS prevent lazy "keep" recommendations
|
| 488 |
+
# Check if recommended_budget is essentially the same as avg_expense (within 2% tolerance)
|
| 489 |
+
tolerance_percent = 0.02 # 2% tolerance
|
| 490 |
+
tolerance = abs(avg_expense * tolerance_percent)
|
| 491 |
+
difference = abs(recommended_budget - avg_expense)
|
| 492 |
+
|
| 493 |
+
if action == "keep" and difference <= tolerance:
|
| 494 |
+
# They're essentially the same - FORCE an increase
|
| 495 |
+
print(f"🚨 AGGRESSIVE VALIDATION: Overriding lazy 'keep' recommendation")
|
| 496 |
+
print(f" avg_expense={avg_expense:,.2f}, recommended_budget={recommended_budget:,.2f}, difference={difference:,.2f}, tolerance={tolerance:,.2f}")
|
| 497 |
+
recommended_budget = avg_expense * 1.10 # Force 10% increase
|
| 498 |
+
action = "increase"
|
| 499 |
+
reason = f"Based on your spending pattern, I recommend increasing your budget by 10% to {recommended_budget:,.0f} to account for inflation, variability, and unexpected expenses. This provides a safety buffer for better financial planning."
|
| 500 |
+
|
| 501 |
+
# Check for data corruption (unreasonably large numbers)
|
| 502 |
+
if recommended_budget > 1e15 or avg_expense > 1e15:
|
| 503 |
+
print(f"🚨 DATA CORRUPTION DETECTED: Numbers are unreasonably large!")
|
| 504 |
+
print(f" avg_expense={avg_expense:,.2e}, recommended_budget={recommended_budget:,.2e}")
|
| 505 |
+
# Use a safe fallback - cap at reasonable maximum
|
| 506 |
+
if avg_expense > 1e15:
|
| 507 |
+
print(f" Capping avg_expense from {avg_expense:,.2e} to 1,000,000,000")
|
| 508 |
+
avg_expense = 1e9 # 1 billion as safe maximum
|
| 509 |
+
recommended_budget = avg_expense * 1.10
|
| 510 |
+
action = "increase"
|
| 511 |
+
reason = f"Based on your spending data, I recommend a budget of {recommended_budget:,.0f} (10% increase from average) to account for variability and inflation."
|
| 512 |
+
|
| 513 |
+
# EXTRA SAFETY: If action is still "keep" and values are very close, force change
|
| 514 |
+
if action == "keep" and difference < (avg_expense * 0.05): # Within 5%
|
| 515 |
+
print(f"🚨 EXTRA SAFETY CHECK: Forcing change from 'keep' (difference is {difference:,.2f}, which is < 5% of average)")
|
| 516 |
+
recommended_budget = avg_expense * 1.10
|
| 517 |
+
action = "increase"
|
| 518 |
+
reason = f"I recommend increasing your budget by 10% to {recommended_budget:,.0f} to provide a buffer for inflation and unexpected expenses."
|
| 519 |
+
|
| 520 |
# Create recommendation
|
| 521 |
return [BudgetRecommendation(
|
| 522 |
category=category_name,
|