Update app.py
Browse files
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 (
|
| 335 |
with gr.Tab("๐ Analytics"):
|
| 336 |
gr.Markdown("## ๐ฏ Your Rewards Optimization Dashboard")
|
| 337 |
|
| 338 |
-
#
|
| 339 |
with gr.Row():
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
|
| 383 |
-
|
|
| 384 |
-
|
|
| 385 |
-
|
|
| 386 |
-
|
|
| 387 |
-
|
|
| 388 |
-
|
|
|
|
|
|
|
| 389 |
|
| 390 |
with gr.Column(scale=1):
|
| 391 |
gr.Markdown("### ๐ Monthly Trends & Insights")
|
| 392 |
-
|
| 393 |
-
|
| 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 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
-
|
| 410 |
-
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
|
| 414 |
gr.Markdown("---")
|
| 415 |
|
| 416 |
-
# Spending Forecast
|
| 417 |
-
|
| 418 |
-
|
| 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"):
|