Update app.py
Browse files
app.py
CHANGED
|
@@ -2250,40 +2250,89 @@ with gr.Blocks(
|
|
| 2250 |
|
| 2251 |
|
| 2252 |
def respond(message, chat_history, user_id):
|
| 2253 |
-
"""Handle chat responses with
|
| 2254 |
if not message.strip():
|
| 2255 |
return "", chat_history
|
| 2256 |
|
|
|
|
| 2257 |
user_context = {}
|
| 2258 |
try:
|
| 2259 |
analytics = client.get_user_analytics(user_id)
|
| 2260 |
if analytics.get('success'):
|
| 2261 |
data = analytics.get('data', {})
|
| 2262 |
user_context = {
|
|
|
|
| 2263 |
'cards': safe_get(data, 'cards', ['Amex Gold', 'Chase Sapphire Reserve']),
|
| 2264 |
'monthly_spending': safe_get(data, 'total_spending', 0),
|
| 2265 |
-
'top_category': safe_get(data, 'top_category', 'Groceries')
|
|
|
|
|
|
|
| 2266 |
}
|
| 2267 |
except Exception as e:
|
| 2268 |
print(f"Error getting user context: {e}")
|
| 2269 |
user_context = {
|
|
|
|
| 2270 |
'cards': ['Amex Gold', 'Chase Sapphire Reserve'],
|
| 2271 |
'monthly_spending': 3450.75,
|
| 2272 |
-
'top_category': 'Groceries'
|
|
|
|
|
|
|
| 2273 |
}
|
| 2274 |
|
|
|
|
| 2275 |
try:
|
| 2276 |
-
if config.
|
| 2277 |
-
bot_response =
|
|
|
|
| 2278 |
else:
|
| 2279 |
-
|
|
|
|
|
|
|
| 2280 |
except Exception as e:
|
| 2281 |
-
print(f"Chat error: {e}")
|
| 2282 |
-
|
|
|
|
| 2283 |
|
| 2284 |
chat_history.append((message, bot_response))
|
| 2285 |
return "", chat_history
|
| 2286 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2287 |
msg.submit(respond, [msg, chatbot, chat_user], [msg, chatbot])
|
| 2288 |
send_btn.click(respond, [msg, chatbot, chat_user], [msg, chatbot])
|
| 2289 |
|
|
|
|
| 2250 |
|
| 2251 |
|
| 2252 |
def respond(message, chat_history, user_id):
|
| 2253 |
+
"""Handle chat responses with Gemini AI"""
|
| 2254 |
if not message.strip():
|
| 2255 |
return "", chat_history
|
| 2256 |
|
| 2257 |
+
# ✅ Get user context for personalized responses
|
| 2258 |
user_context = {}
|
| 2259 |
try:
|
| 2260 |
analytics = client.get_user_analytics(user_id)
|
| 2261 |
if analytics.get('success'):
|
| 2262 |
data = analytics.get('data', {})
|
| 2263 |
user_context = {
|
| 2264 |
+
'user_id': user_id,
|
| 2265 |
'cards': safe_get(data, 'cards', ['Amex Gold', 'Chase Sapphire Reserve']),
|
| 2266 |
'monthly_spending': safe_get(data, 'total_spending', 0),
|
| 2267 |
+
'top_category': safe_get(data, 'top_category', 'Groceries'),
|
| 2268 |
+
'total_rewards': safe_get(data, 'total_rewards', 0),
|
| 2269 |
+
'optimization_score': safe_get(data, 'optimization_score', 75)
|
| 2270 |
}
|
| 2271 |
except Exception as e:
|
| 2272 |
print(f"Error getting user context: {e}")
|
| 2273 |
user_context = {
|
| 2274 |
+
'user_id': user_id,
|
| 2275 |
'cards': ['Amex Gold', 'Chase Sapphire Reserve'],
|
| 2276 |
'monthly_spending': 3450.75,
|
| 2277 |
+
'top_category': 'Groceries',
|
| 2278 |
+
'total_rewards': 125.50,
|
| 2279 |
+
'optimization_score': 75
|
| 2280 |
}
|
| 2281 |
|
| 2282 |
+
# ✅ Use Gemini for chat responses
|
| 2283 |
try:
|
| 2284 |
+
if config.USE_GEMINI and gemini.enabled:
|
| 2285 |
+
bot_response = gemini.chat_response(message, user_context, chat_history)
|
| 2286 |
+
print(f"✅ Gemini chat response generated")
|
| 2287 |
else:
|
| 2288 |
+
# Fallback response
|
| 2289 |
+
bot_response = generate_fallback_response(message, user_context)
|
| 2290 |
+
print(f"⚠️ Using fallback response (Gemini not available)")
|
| 2291 |
except Exception as e:
|
| 2292 |
+
print(f"❌ Chat error: {e}")
|
| 2293 |
+
print(traceback.format_exc())
|
| 2294 |
+
bot_response = generate_fallback_response(message, user_context)
|
| 2295 |
|
| 2296 |
chat_history.append((message, bot_response))
|
| 2297 |
return "", chat_history
|
| 2298 |
|
| 2299 |
+
|
| 2300 |
+
def generate_fallback_response(message: str, user_context: dict) -> str:
|
| 2301 |
+
"""Generate a simple fallback response when Gemini is unavailable"""
|
| 2302 |
+
message_lower = message.lower()
|
| 2303 |
+
|
| 2304 |
+
# Card-specific questions
|
| 2305 |
+
if "amex" in message_lower or "gold" in message_lower:
|
| 2306 |
+
return "The Amex Gold Card offers 4x points on dining and groceries at U.S. supermarkets (up to $25,000/year). It's excellent for food spending!"
|
| 2307 |
+
|
| 2308 |
+
elif "chase" in message_lower and "sapphire" in message_lower:
|
| 2309 |
+
return "Chase Sapphire Reserve offers 3x points on travel and dining, plus premium travel benefits like airport lounge access and travel credits."
|
| 2310 |
+
|
| 2311 |
+
elif "citi" in message_lower and "custom" in message_lower:
|
| 2312 |
+
return "Citi Custom Cash gives you 5% cashback on your top spending category each month (up to $500), then 1% on everything else."
|
| 2313 |
+
|
| 2314 |
+
# Category questions
|
| 2315 |
+
elif "grocery" in message_lower or "groceries" in message_lower:
|
| 2316 |
+
return f"For groceries, I recommend using a card with high grocery rewards. Your top spending category is {user_context.get('top_category', 'Groceries')}."
|
| 2317 |
+
|
| 2318 |
+
elif "restaurant" in message_lower or "dining" in message_lower:
|
| 2319 |
+
return "For dining, cards like Amex Gold (4x points) or Chase Sapphire Reserve (3x points) offer excellent rewards."
|
| 2320 |
+
|
| 2321 |
+
elif "travel" in message_lower:
|
| 2322 |
+
return "For travel, consider Chase Sapphire Reserve (3x points) or cards with travel-specific bonuses and protections."
|
| 2323 |
+
|
| 2324 |
+
# Spending questions
|
| 2325 |
+
elif "spending" in message_lower or "how much" in message_lower:
|
| 2326 |
+
return f"Based on your profile, you've spent ${user_context.get('monthly_spending', 0):.2f} this month, earning ${user_context.get('total_rewards', 0):.2f} in rewards."
|
| 2327 |
+
|
| 2328 |
+
elif "optimize" in message_lower or "maximize" in message_lower:
|
| 2329 |
+
return f"Your optimization score is {user_context.get('optimization_score', 0)}/100. To improve, use the 'Get Recommendation' tab for each purchase!"
|
| 2330 |
+
|
| 2331 |
+
# Default
|
| 2332 |
+
else:
|
| 2333 |
+
return "I'm here to help with credit card recommendations! Try asking about specific cards, categories, or your spending patterns. For personalized recommendations, use the 'Get Recommendation' tab."
|
| 2334 |
+
|
| 2335 |
+
|
| 2336 |
msg.submit(respond, [msg, chatbot, chat_user], [msg, chatbot])
|
| 2337 |
send_btn.click(respond, [msg, chatbot, chat_user], [msg, chatbot])
|
| 2338 |
|