sammy786 commited on
Commit
10f88cc
ยท
verified ยท
1 Parent(s): f82669f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -69
app.py CHANGED
@@ -331,103 +331,168 @@ Get AI-powered credit card recommendations that maximize your rewards based on:
331
  cache_examples=False
332
  )
333
 
334
- # ========== Tab 2: Analytics (NEW) ==========
335
  with gr.Tab("๐Ÿ“Š Analytics"):
336
  gr.Markdown("## ๐ŸŽฏ Your Rewards Optimization Dashboard")
337
 
338
- # Top Metrics Row
339
  with gr.Row():
340
- with gr.Column(scale=1):
341
- gr.HTML("""
342
- <div class="metric-card">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
343
  <h2>$342</h2>
344
  <p>๐Ÿ’ฐ Potential Annual Savings</p>
345
  </div>
346
- """)
347
-
348
- with gr.Column(scale=1):
349
- gr.HTML("""
350
- <div class="metric-card metric-card-green">
351
  <h2>23%</h2>
352
  <p>๐Ÿ“ˆ Rewards Rate Increase</p>
353
  </div>
354
- """)
355
-
356
- with gr.Column(scale=1):
357
- gr.HTML("""
358
- <div class="metric-card metric-card-orange">
359
  <h2>156</h2>
360
  <p>โœ… Optimized Transactions</p>
361
  </div>
362
- """)
363
-
364
- with gr.Column(scale=1):
365
- gr.HTML("""
366
- <div class="metric-card metric-card-blue">
367
  <h2>87/100</h2>
368
  <p>โญ Optimization Score</p>
369
  </div>
370
- """)
 
 
371
 
372
  gr.Markdown("---")
373
 
374
- # Detailed Analytics
375
  with gr.Row():
376
  with gr.Column(scale=1):
377
  gr.Markdown("### ๐Ÿ’ฐ Category Spending Breakdown")
378
- gr.Markdown("""
379
- | Category | Monthly Spend | Best Card | Rewards | Rate |
380
- |----------|---------------|-----------|---------|------|
381
- | ๐Ÿ›’ Groceries | $450.00 | Amex Gold | $27.00 | 6% |
382
- | ๐Ÿฝ๏ธ Restaurants | $320.00 | Amex Gold | $12.80 | 4% |
383
- | โ›ฝ Gas | $180.00 | Costco Visa | $7.20 | 4% |
384
- | โœˆ๏ธ Travel | $850.00 | Sapphire Reserve | $42.50 | 5% |
385
- | ๐ŸŽฌ Entertainment | $125.00 | Freedom Unlimited | $1.88 | 1.5% |
386
- | ๐Ÿช Online Shopping | $280.00 | Amazon Prime | $16.80 | 6% |
387
- | **Total** | **$2,205.00** | - | **$108.18** | **4.9%** |
388
- """)
 
 
389
 
390
  with gr.Column(scale=1):
391
  gr.Markdown("### ๐Ÿ“ˆ Monthly Trends & Insights")
392
-
393
- gr.Markdown("""
394
- **๐Ÿ”ฅ Top Spending Categories:**
395
- 1. โœˆ๏ธ Travel: $850 (โ†‘ 45% from last month)
396
- 2. ๐Ÿ›’ Groceries: $450 (โ†‘ 12%)
397
- 3. ๐Ÿฝ๏ธ Restaurants: $320 (โ†“ 5%)
398
-
399
- **๐Ÿ’ก Optimization Opportunities:**
400
- - โœ… You're using optimal cards 87% of the time
401
- - ๐ŸŽฏ Switch to Chase Freedom for Q4 5% grocery bonus
402
- - โš ๏ธ Amex Gold dining cap approaching ($2,000 limit)
403
- - ๐Ÿ’ณ Consider applying for Citi Custom Cash
404
-
405
- **๐Ÿ† Best Performing Card:**
406
- Chase Sapphire Reserve - $42.50 rewards earned
407
-
408
- **๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ Year-to-Date:**
409
- - Total Rewards: $1,298.16
410
- - Potential if optimized: $1,640.00
411
- - **Money left on table: $341.84**
412
- """)
413
 
414
  gr.Markdown("---")
415
 
416
- # Spending Forecast
417
- with gr.Row():
418
- gr.Markdown("""
419
- ### ๐Ÿ”ฎ Next Month Forecast
420
-
421
- Based on your spending patterns:
422
- - **Predicted Spend:** $2,350
423
- - **Predicted Rewards:** $115.25
424
- - **Cards to Watch:** Amex Gold (dining cap), Freedom (quarterly bonus)
425
-
426
- **Recommendations:**
427
- 1. ๐Ÿ’ณ Use Chase Freedom for groceries in Q4 (5% back)
428
- 2. โš ๏ธ Monitor Amex Gold dining spend (cap at $2,000)
429
- 3. ๐ŸŽฏ Book holiday travel with Sapphire Reserve for 5x points
430
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
431
 
432
  # ========== Tab 3: About ==========
433
  with gr.Tab("โ„น๏ธ About"):
 
331
  cache_examples=False
332
  )
333
 
334
+ # ========== Tab 2: Analytics (DYNAMIC) ==========
335
  with gr.Tab("๐Ÿ“Š Analytics"):
336
  gr.Markdown("## ๐ŸŽฏ Your Rewards Optimization Dashboard")
337
 
338
+ # User selector for analytics
339
  with gr.Row():
340
+ analytics_user = gr.Dropdown(
341
+ choices=SAMPLE_USERS,
342
+ value=SAMPLE_USERS,[object Object],,
343
+ label="๐Ÿ‘ค View Analytics For User",
344
+ scale=3
345
+ )
346
+ refresh_analytics_btn = gr.Button(
347
+ "๐Ÿ”„ Refresh Analytics",
348
+ variant="secondary",
349
+ scale=1
350
+ )
351
+
352
+ # Top Metrics Row (Dynamic)
353
+ metrics_display = gr.HTML(
354
+ value="""
355
+ <div style="display: flex; gap: 10px; flex-wrap: wrap;">
356
+ <div class="metric-card" style="flex: 1; min-width: 200px;">
357
  <h2>$342</h2>
358
  <p>๐Ÿ’ฐ Potential Annual Savings</p>
359
  </div>
360
+ <div class="metric-card metric-card-green" style="flex: 1; min-width: 200px;">
 
 
 
 
361
  <h2>23%</h2>
362
  <p>๐Ÿ“ˆ Rewards Rate Increase</p>
363
  </div>
364
+ <div class="metric-card metric-card-orange" style="flex: 1; min-width: 200px;">
 
 
 
 
365
  <h2>156</h2>
366
  <p>โœ… Optimized Transactions</p>
367
  </div>
368
+ <div class="metric-card metric-card-blue" style="flex: 1; min-width: 200px;">
 
 
 
 
369
  <h2>87/100</h2>
370
  <p>โญ Optimization Score</p>
371
  </div>
372
+ </div>
373
+ """
374
+ )
375
 
376
  gr.Markdown("---")
377
 
378
+ # Detailed Analytics (Dynamic)
379
  with gr.Row():
380
  with gr.Column(scale=1):
381
  gr.Markdown("### ๐Ÿ’ฐ Category Spending Breakdown")
382
+ spending_table = gr.Markdown(
383
+ value="""
384
+ | Category | Monthly Spend | Best Card | Rewards | Rate |
385
+ |----------|---------------|-----------|---------|------|
386
+ | ๐Ÿ›’ Groceries | $450.00 | Amex Gold | $27.00 | 6% |
387
+ | ๐Ÿฝ๏ธ Restaurants | $320.00 | Amex Gold | $12.80 | 4% |
388
+ | โ›ฝ Gas | $180.00 | Costco Visa | $7.20 | 4% |
389
+ | โœˆ๏ธ Travel | $850.00 | Sapphire Reserve | $42.50 | 5% |
390
+ | ๐ŸŽฌ Entertainment | $125.00 | Freedom Unlimited | $1.88 | 1.5% |
391
+ | ๐Ÿช Online Shopping | $280.00 | Amazon Prime | $16.80 | 6% |
392
+ | **Total** | **$2,205.00** | - | **$108.18** | **4.9%** |
393
+ """
394
+ )
395
 
396
  with gr.Column(scale=1):
397
  gr.Markdown("### ๐Ÿ“ˆ Monthly Trends & Insights")
398
+ insights_display = gr.Markdown(
399
+ value="""
400
+ **๐Ÿ”ฅ Top Spending Categories:**
401
+ 1. โœˆ๏ธ Travel: $850 (โ†‘ 45% from last month)
402
+ 2. ๐Ÿ›’ Groceries: $450 (โ†‘ 12%)
403
+ 3. ๐Ÿฝ๏ธ Restaurants: $320 (โ†“ 5%)
404
+
405
+ **๐Ÿ’ก Optimization Opportunities:**
406
+ - โœ… You're using optimal cards 87% of the time
407
+ - ๐ŸŽฏ Switch to Chase Freedom for Q4 5% grocery bonus
408
+ - โš ๏ธ Amex Gold dining cap approaching ($2,000 limit)
409
+
410
+ **๐Ÿ† Best Performing Card:**
411
+ Chase Sapphire Reserve - $42.50 rewards earned
412
+
413
+ **๐Ÿ“Š Year-to-Date:**
414
+ - Total Rewards: $1,298.16
415
+ - Potential if optimized: $1,640.00
416
+ - **Money left on table: $341.84**
417
+ """
418
+ )
419
 
420
  gr.Markdown("---")
421
 
422
+ # Spending Forecast (Dynamic)
423
+ forecast_display = gr.Markdown(
424
+ value="""
425
+ ### ๐Ÿ”ฎ Next Month Forecast
426
+
427
+ Based on your spending patterns:
428
+ - **Predicted Spend:** $2,350
429
+ - **Predicted Rewards:** $115.25
430
+ - **Cards to Watch:** Amex Gold (dining cap), Freedom (quarterly bonus)
431
+
432
+ **Recommendations:**
433
+ 1. ๐Ÿ’ณ Use Chase Freedom for groceries in Q4 (5% back)
434
+ 2. โš ๏ธ Monitor Amex Gold dining spend (cap at $2,000)
435
+ 3. ๐ŸŽฏ Book holiday travel with Sapphire Reserve for 5x points
436
+ """
437
+ )
438
+
439
+ # Status indicator
440
+ analytics_status = gr.Markdown(
441
+ value="*Analytics loaded for u_alice*",
442
+ elem_classes=["status-text"]
443
+ )
444
+
445
+ # ===================== Analytics Update Function =====================
446
+ def update_analytics(user_id: str) -> tuple:
447
+ """Fetch and format analytics for selected user"""
448
+ try:
449
+ # Fetch analytics data
450
+ analytics_data = client.get_user_analytics(user_id)
451
+
452
+ # Format for display
453
+ from utils.formatters import format_analytics_metrics
454
+ metrics_html, table_md, insights_md, forecast_md = format_analytics_metrics(analytics_data)
455
+
456
+ # Status message
457
+ from datetime import datetime
458
+ status = f"*Analytics updated for {user_id} at {datetime.now().strftime('%I:%M %p')}*"
459
+
460
+ return metrics_html, table_md, insights_md, forecast_md, status
461
+
462
+ except Exception as e:
463
+ error_msg = f"โŒ Error loading analytics: {str(e)}"
464
+ return (
465
+ "<p>Error loading metrics</p>",
466
+ "Error loading table",
467
+ "Error loading insights",
468
+ "Error loading forecast",
469
+ error_msg
470
+ )
471
+
472
+ # Connect analytics refresh to button and dropdown
473
+ refresh_analytics_btn.click(
474
+ fn=update_analytics,
475
+ inputs=[analytics_user],
476
+ outputs=[
477
+ metrics_display,
478
+ spending_table,
479
+ insights_display,
480
+ forecast_display,
481
+ analytics_status
482
+ ]
483
+ )
484
+
485
+ analytics_user.change(
486
+ fn=update_analytics,
487
+ inputs=[analytics_user],
488
+ outputs=[
489
+ metrics_display,
490
+ spending_table,
491
+ insights_display,
492
+ forecast_display,
493
+ analytics_status
494
+ ]
495
+ )
496
 
497
  # ========== Tab 3: About ==========
498
  with gr.Tab("โ„น๏ธ About"):