LogicGoInfotechSpaces commited on
Commit
05ed0ff
·
1 Parent(s): 65cfbc3

Make OpenAI primary recommender with rule fallback

Browse files
Files changed (2) hide show
  1. app/smart_recommendation.py +10 -10
  2. test_hf_api.py +1 -1
app/smart_recommendation.py CHANGED
@@ -55,23 +55,24 @@ class SmartBudgetRecommender:
55
  recommendations = []
56
  for category, data in category_data.items():
57
  avg_expense = data["average_monthly"]
58
- recommended_budget = self._calculate_recommended_budget(avg_expense, data)
59
  confidence = self._calculate_confidence(data)
60
-
61
- reason = self._generate_reason(category, avg_expense, recommended_budget)
62
-
63
- ai_result = self._get_ai_recommendation(category, data, avg_expense, recommended_budget)
64
  if ai_result:
65
- recommended_budget = ai_result.get("recommended_budget", recommended_budget)
66
- reason = ai_result.get("reason", reason)
67
  action = ai_result.get("action")
68
  else:
 
 
 
69
  action = None
70
 
71
  recommendations.append(BudgetRecommendation(
72
  category=category,
73
  average_expense=round(avg_expense, 2),
74
- recommended_budget=round(recommended_budget, 2),
75
  reason=reason,
76
  confidence=confidence,
77
  action=action
@@ -234,7 +235,7 @@ class SmartBudgetRecommender:
234
  result.sort(key=lambda x: x.average_monthly_expense, reverse=True)
235
  return result
236
 
237
- def _get_ai_recommendation(self, category: str, data: Dict, avg_expense: float, fallback_budget: float):
238
  """Use OpenAI to refine the budget recommendation."""
239
  if not OPENAI_API_KEY:
240
  return None
@@ -246,7 +247,6 @@ class SmartBudgetRecommender:
246
  f"Average spend: {avg_expense:.2f}\n"
247
  f"Std deviation: {data['std_dev']:.2f}\n"
248
  f"Months observed: {data['months_analyzed']}\n"
249
- f"Current suggested budget: {fallback_budget:.2f}\n"
250
  )
251
 
252
  prompt = (
 
55
  recommendations = []
56
  for category, data in category_data.items():
57
  avg_expense = data["average_monthly"]
 
58
  confidence = self._calculate_confidence(data)
59
+
60
+ # 1) Try OpenAI first (primary source of recommendation)
61
+ ai_result = self._get_ai_recommendation(category, data, avg_expense)
 
62
  if ai_result:
63
+ recommended_budget = ai_result.get("recommended_budget")
64
+ reason = ai_result.get("reason")
65
  action = ai_result.get("action")
66
  else:
67
+ # 2) Fallback to rule-based recommendation
68
+ recommended_budget = self._calculate_recommended_budget(avg_expense, data)
69
+ reason = self._generate_reason(category, avg_expense, recommended_budget)
70
  action = None
71
 
72
  recommendations.append(BudgetRecommendation(
73
  category=category,
74
  average_expense=round(avg_expense, 2),
75
+ recommended_budget=round(recommended_budget or 0, 2),
76
  reason=reason,
77
  confidence=confidence,
78
  action=action
 
235
  result.sort(key=lambda x: x.average_monthly_expense, reverse=True)
236
  return result
237
 
238
+ def _get_ai_recommendation(self, category: str, data: Dict, avg_expense: float):
239
  """Use OpenAI to refine the budget recommendation."""
240
  if not OPENAI_API_KEY:
241
  return None
 
247
  f"Average spend: {avg_expense:.2f}\n"
248
  f"Std deviation: {data['std_dev']:.2f}\n"
249
  f"Months observed: {data['months_analyzed']}\n"
 
250
  )
251
 
252
  prompt = (
test_hf_api.py CHANGED
@@ -8,7 +8,7 @@ import json
8
  from datetime import datetime, timedelta
9
 
10
  BASE_URL = "https://logicgoinfotechspaces-smart-budget-recommendation.hf.space"
11
- USER_ID = "68a834c3f4694b11efedacd2"
12
 
13
  def test_health():
14
  """Test health endpoint"""
 
8
  from datetime import datetime, timedelta
9
 
10
  BASE_URL = "https://logicgoinfotechspaces-smart-budget-recommendation.hf.space"
11
+ USER_ID = "688c80ca990b63f0e945ecd9"
12
 
13
  def test_health():
14
  """Test health endpoint"""