LogicGoInfotechSpaces commited on
Commit
24cd56b
·
1 Parent(s): ef928f0

Fix undefined variable error and ensure user-provided budget_amount is used for OpenAI recommendations

Browse files
Files changed (1) hide show
  1. app/smart_recommendation.py +42 -36
app/smart_recommendation.py CHANGED
@@ -383,42 +383,48 @@ class SmartBudgetRecommender:
383
  if budget_amount and budget_amount > 0:
384
  # Generate recommendation using the provided budget_amount (user's input)
385
  avg_expense = budget_amount
386
- monthly_values = [avg_expense]
387
- std_dev = avg_expense * 0.05
388
- months_analyzed = 1
389
-
390
- data = {
391
- "average_monthly": avg_expense,
392
- "total": avg_expense,
393
- "count": 1,
394
- "months_analyzed": months_analyzed,
395
- "std_dev": std_dev,
396
- "monthly_values": monthly_values,
397
- }
398
-
399
- confidence = self._calculate_confidence(data)
400
-
401
- # Get AI recommendation
402
- ai_result = self._get_ai_recommendation(category_name, data, avg_expense)
403
- if ai_result and ai_result.get("recommended_budget"):
404
- recommended_budget = ai_result.get("recommended_budget")
405
- reason = ai_result.get("reason", f"AI recommendation for {category_name}")
406
- action = ai_result.get("action")
407
- else:
408
- recommended_budget = avg_expense * 1.10
409
- reason = f"Based on your budget of {budget_amount:,.0f}, I recommend {recommended_budget:,.0f} to account for variability and inflation."
410
- action = "increase"
411
-
412
- return [BudgetRecommendation(
413
- category=category_name,
414
- category_id=actual_category_id,
415
- average_expense=round(avg_expense, 2),
416
- recommended_budget=round(recommended_budget or 0, 2),
417
- reason=reason,
418
- confidence=confidence,
419
- action=action
420
- )]
421
- elif budget_amount_from_budget > 0:
 
 
 
 
 
 
422
  # Budget exists but no categories - use budget name and amount
423
  budget_name = budget_by_id.get("name", "Budget")
424
  if budget_amount is None:
 
383
  if budget_amount and budget_amount > 0:
384
  # Generate recommendation using the provided budget_amount (user's input)
385
  avg_expense = budget_amount
386
+ monthly_values = [avg_expense]
387
+ std_dev = avg_expense * 0.05
388
+ months_analyzed = 1
389
+
390
+ data = {
391
+ "average_monthly": avg_expense,
392
+ "total": avg_expense,
393
+ "count": 1,
394
+ "months_analyzed": months_analyzed,
395
+ "std_dev": std_dev,
396
+ "monthly_values": monthly_values,
397
+ }
398
+
399
+ confidence = self._calculate_confidence(data)
400
+
401
+ # Get AI recommendation - pass the user's budget_amount so OpenAI knows what they set
402
+ ai_result = self._get_ai_recommendation(category_name, data, avg_expense)
403
+ if ai_result and ai_result.get("recommended_budget"):
404
+ recommended_budget = ai_result.get("recommended_budget")
405
+ reason = ai_result.get("reason", f"AI recommendation for {category_name}")
406
+ action = ai_result.get("action")
407
+ else:
408
+ recommended_budget = avg_expense * 1.10
409
+ reason = f"Based on your budget of {budget_amount:,.0f}, I recommend {recommended_budget:,.0f} to account for variability and inflation."
410
+ action = "increase"
411
+
412
+ return [BudgetRecommendation(
413
+ category=category_name,
414
+ category_id=actual_category_id,
415
+ average_expense=round(avg_expense, 2),
416
+ recommended_budget=round(recommended_budget or 0, 2),
417
+ reason=reason,
418
+ confidence=confidence,
419
+ action=action
420
+ )]
421
+
422
+ # Budget exists but no categories - use budget name and amount
423
+ budget_max_amount = float(budget_by_id.get("maxAmount", 0) or 0)
424
+ budget_spend_amount = float(budget_by_id.get("spendAmount", 0) or 0)
425
+ budget_amount_from_budget = budget_spend_amount if budget_spend_amount > 0 else budget_max_amount
426
+
427
+ if budget_amount_from_budget > 0:
428
  # Budget exists but no categories - use budget name and amount
429
  budget_name = budget_by_id.get("name", "Budget")
430
  if budget_amount is None: