File size: 4,750 Bytes
847392c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
"""
Example usage of the Smart Budget Recommendation API
This script demonstrates how to use the API endpoints.
"""
import requests
from datetime import datetime
# Base URL - Update this to your deployed Hugging Face Space URL
BASE_URL = "http://localhost:8000" # Change to your Hugging Face Space URL
# Example user ID
USER_ID = "user123"
def create_sample_expenses():
"""Create sample expenses for testing"""
expenses = [
{
"user_id": USER_ID,
"amount": 3500,
"category": "Groceries",
"description": "Monthly groceries",
"date": "2024-01-15T00:00:00",
"type": "expense"
},
{
"user_id": USER_ID,
"amount": 4000,
"category": "Groceries",
"description": "Monthly groceries",
"date": "2024-02-15T00:00:00",
"type": "expense"
},
{
"user_id": USER_ID,
"amount": 3800,
"category": "Groceries",
"description": "Monthly groceries",
"date": "2024-03-15T00:00:00",
"type": "expense"
},
{
"user_id": USER_ID,
"amount": 4200,
"category": "Groceries",
"description": "Monthly groceries",
"date": "2024-04-15T00:00:00",
"type": "expense"
},
{
"user_id": USER_ID,
"amount": 2000,
"category": "Transport",
"description": "Monthly transport",
"date": "2024-01-20T00:00:00",
"type": "expense"
},
{
"user_id": USER_ID,
"amount": 2200,
"category": "Transport",
"description": "Monthly transport",
"date": "2024-02-20T00:00:00",
"type": "expense"
},
]
print("Creating sample expenses...")
for expense in expenses:
response = requests.post(f"{BASE_URL}/expenses", json=expense)
if response.status_code == 200:
print(f"✓ Created expense: {expense['category']} - Rs.{expense['amount']}")
else:
print(f"✗ Failed to create expense: {response.text}")
def get_recommendations():
"""Get budget recommendations"""
print("\n" + "="*50)
print("Getting Smart Budget Recommendations...")
print("="*50)
# Get recommendations for next month
next_month = datetime.now().month + 1
next_year = datetime.now().year
if next_month > 12:
next_month = 1
next_year += 1
response = requests.get(
f"{BASE_URL}/recommendations/{USER_ID}",
params={"month": next_month, "year": next_year}
)
if response.status_code == 200:
recommendations = response.json()
print(f"\nFound {len(recommendations)} recommendations:\n")
for rec in recommendations:
print(f"Category: {rec['category']}")
print(f" Average Expense: Rs.{rec['average_expense']:,.0f}")
print(f" Recommended Budget: Rs.{rec['recommended_budget']:,.0f}")
print(f" Confidence: {rec['confidence']*100:.0f}%")
print(f" Reason: {rec['reason']}")
print()
else:
print(f"Error: {response.status_code} - {response.text}")
def get_category_expenses():
"""Get category expense averages"""
print("\n" + "="*50)
print("Getting Category Expense Averages...")
print("="*50)
response = requests.get(
f"{BASE_URL}/category-expenses/{USER_ID}",
params={"months": 3}
)
if response.status_code == 200:
categories = response.json()
print(f"\nFound {len(categories)} categories:\n")
for cat in categories:
print(f"{cat['category']}:")
print(f" Average Monthly: Rs.{cat['average_monthly_expense']:,.0f}")
print(f" Total Transactions: {cat['total_expenses']}")
print(f" Months Analyzed: {cat['months_analyzed']}")
print()
else:
print(f"Error: {response.status_code} - {response.text}")
if __name__ == "__main__":
print("Smart Budget Recommendation API - Example Usage")
print("="*50)
# Check health
print("\nChecking API health...")
response = requests.get(f"{BASE_URL}/health")
if response.status_code == 200:
print(f"✓ API is healthy: {response.json()}")
else:
print(f"✗ API health check failed: {response.text}")
exit(1)
# Uncomment to create sample data
# create_sample_expenses()
# Get recommendations
get_recommendations()
# Get category averages
get_category_expenses()
|