LogicGoInfotechSpaces commited on
Commit
26bdcce
·
2 Parent(s): 05ed0ff 00c5fd3

Merge branch 'main' of https://huggingface.co/spaces/LogicGoInfotechSpaces/SMART_BUDGET_RECOMMENDATION

Browse files
Files changed (3) hide show
  1. HUGGINGFACE_DEPLOYMENT.md +0 -156
  2. QUICKSTART.md +0 -84
  3. README.md +3 -156
HUGGINGFACE_DEPLOYMENT.md DELETED
@@ -1,156 +0,0 @@
1
- # Hugging Face Deployment Guide
2
-
3
- This guide will help you deploy the Smart Budget Recommendation API to Hugging Face Spaces.
4
-
5
- ## Prerequisites
6
-
7
- 1. A Hugging Face account (sign up at https://huggingface.co)
8
- 2. MongoDB connection string ready
9
- 3. Git installed (optional, you can also use the web interface)
10
-
11
- ## Step-by-Step Deployment
12
-
13
- ### Step 1: Create a New Space
14
-
15
- 1. Go to https://huggingface.co/spaces
16
- 2. Click "Create new Space"
17
- 3. Fill in the details:
18
- - **Space name**: `smart-budget-recommendation` (or your preferred name)
19
- - **SDK**: Select **Docker**
20
- - **Visibility**: Choose Public or Private
21
- 4. Click "Create Space"
22
-
23
- ### Step 2: Add secrets (MongoDB and optional OpenAI)
24
-
25
- 1. In your Space, go to **Settings** (gear icon)
26
- 2. Navigate to **Variables and secrets** section
27
- 3. Click **New secret**
28
- 4. Add the following secrets:
29
- - **Name**: `MONGODB_URI`
30
- - **Value**: `mongodb://expenseuser:Kem_6o%3F%3F@165.227.69.221:27017/expense?authSource=admin`
31
- - **Name**: `OPENAI_API_KEY` *(optional, enables GPT-based recommendations)*
32
- - **Value**: `sk-...`
33
- 5. Click **Add secret** after each entry
34
-
35
- ### Step 3: Upload Files
36
-
37
- You can upload files in two ways:
38
-
39
- #### Option A: Using Git (Recommended)
40
-
41
- 1. Clone your Space repository:
42
- ```bash
43
- git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
44
- cd YOUR_SPACE_NAME
45
- ```
46
-
47
- 2. Copy all files from this project:
48
- - `app/` directory (all Python files)
49
- - `Dockerfile`
50
- - `requirements.txt`
51
- - `.dockerignore`
52
- - `README.md` (optional)
53
-
54
- 3. Commit and push:
55
- ```bash
56
- git add .
57
- git commit -m "Initial deployment"
58
- git push
59
- ```
60
-
61
- #### Option B: Using Web Interface
62
-
63
- 1. In your Space, click **Files and versions** tab
64
- 2. Click **Add file** → **Upload files**
65
- 3. Upload all files:
66
- - All files from `app/` directory
67
- - `Dockerfile`
68
- - `requirements.txt`
69
- - `.dockerignore`
70
-
71
- ### Step 4: Wait for Build
72
-
73
- 1. Hugging Face will automatically detect the `Dockerfile` and start building
74
- 2. You can monitor the build progress in the **Logs** tab
75
- 3. The build typically takes 2-5 minutes
76
-
77
- ### Step 5: Verify Deployment
78
-
79
- Once the build completes:
80
-
81
- 1. Your API will be available at: `https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space`
82
- 2. Test the health endpoint: `https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/health`
83
- 3. View API documentation: `https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/docs`
84
-
85
- ## Testing Your Deployment
86
-
87
- ### Using cURL
88
-
89
- ```bash
90
- # Health check
91
- curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/health
92
-
93
- # Get recommendations (replace user123 with actual user_id)
94
- curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/recommendations/user123
95
- ```
96
-
97
- ### Using Python
98
-
99
- ```python
100
- import requests
101
-
102
- BASE_URL = "https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space"
103
-
104
- # Health check
105
- response = requests.get(f"{BASE_URL}/health")
106
- print(response.json())
107
-
108
- # Get recommendations
109
- response = requests.get(f"{BASE_URL}/recommendations/user123")
110
- print(response.json())
111
- ```
112
-
113
- ## Important Notes
114
-
115
- 1. **Port Configuration**: Hugging Face Spaces expect apps to serve on port **7860**, so the Dockerfile exposes and binds to that port.
116
-
117
- 2. **Environment Variables**: Secrets such as `MONGODB_URI` (and optional `OPENAI_API_KEY`) are injected automatically into the container environment.
118
-
119
- 3. **Cold Starts**: The first request after inactivity may take longer (cold start). Subsequent requests will be faster
120
-
121
- 4. **Resource Limits**: Free Hugging Face Spaces have resource limits. For production use, consider upgrading
122
-
123
- 5. **Custom Domain**: You can add a custom domain in Space settings if needed
124
-
125
- ## Troubleshooting
126
-
127
- ### Build Fails
128
-
129
- - Check the **Logs** tab for error messages
130
- - Ensure `Dockerfile` is in the root directory
131
- - Verify all required files are uploaded
132
-
133
- ### Connection Errors
134
-
135
- - Verify `MONGODB_URI` secret is set correctly
136
- - Check MongoDB server is accessible from Hugging Face's servers
137
- - Review connection string encoding (special characters should be URL-encoded)
138
-
139
- ### API Not Responding
140
-
141
- - Check the **Logs** tab for runtime errors
142
- - Verify the application starts successfully
143
- - Test the `/health` endpoint first
144
-
145
- ## Updating Your Deployment
146
-
147
- 1. Make changes to your code locally
148
- 2. Commit and push to your Space repository
149
- 3. Hugging Face will automatically rebuild and redeploy
150
-
151
- ## Support
152
-
153
- For issues specific to:
154
- - **Hugging Face Spaces**: Check [Hugging Face Documentation](https://huggingface.co/docs/hub/spaces)
155
- - **This Application**: Check the main README.md file
156
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
QUICKSTART.md DELETED
@@ -1,84 +0,0 @@
1
- # Quick Start Guide
2
-
3
- ## Local Development
4
-
5
- 1. **Install dependencies:**
6
- ```bash
7
- pip install -r requirements.txt
8
- ```
9
-
10
- 2. **Set environment variable:**
11
- ```bash
12
- # Windows PowerShell
13
- $env:MONGODB_URI="mongodb://expenseuser:Kem_6o%3F%3F@165.227.69.221:27017/expense?authSource=admin"
14
-
15
- # Linux/Mac
16
- export MONGODB_URI="mongodb://expenseuser:Kem_6o%3F%3F@165.227.69.221:27017/expense?authSource=admin"
17
- ```
18
-
19
- 3. **Run the application:**
20
- ```bash
21
- uvicorn app.main:app --reload
22
- ```
23
-
24
- 4. **Access the API:**
25
- - API: http://localhost:8000
26
- - Docs: http://localhost:8000/docs
27
- - Health: http://localhost:8000/health
28
-
29
- ## Test the Smart Budget Recommendation
30
-
31
- 1. **Create some sample expenses:**
32
- ```bash
33
- curl -X POST "http://localhost:8000/expenses" \
34
- -H "Content-Type: application/json" \
35
- -d '{
36
- "user_id": "user123",
37
- "amount": 3800,
38
- "category": "Groceries",
39
- "description": "Monthly groceries",
40
- "date": "2024-01-15T00:00:00",
41
- "type": "expense"
42
- }'
43
- ```
44
-
45
- 2. **Get recommendations:**
46
- ```bash
47
- curl "http://localhost:8000/recommendations/user123?month=2&year=2024"
48
- ```
49
-
50
- Expected response:
51
- ```json
52
- [
53
- {
54
- "category": "Groceries",
55
- "average_expense": 3800.0,
56
- "recommended_budget": 4000.0,
57
- "reason": "Your average monthly grocery expense is Rs.3,800. We suggest setting your budget to Rs.4,000 for next month (includes a 5% buffer for variability).",
58
- "confidence": 0.85
59
- }
60
- ]
61
- ```
62
-
63
- ## Docker Deployment
64
-
65
- 1. **Build the image:**
66
- ```bash
67
- docker build -t smart-budget-recommendation .
68
- ```
69
-
70
- 2. **Run the container:**
71
- ```bash
72
- docker run -p 8000:8000 -e MONGODB_URI="your_mongodb_uri" smart-budget-recommendation
73
- ```
74
-
75
- ## Hugging Face Deployment
76
-
77
- See `HUGGINGFACE_DEPLOYMENT.md` for detailed instructions.
78
-
79
- Quick steps:
80
- 1. Create a new Docker Space on Hugging Face
81
- 2. Add `MONGODB_URI` as a secret
82
- 3. Upload all files
83
- 4. Wait for automatic build and deployment
84
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -3,161 +3,8 @@ license: mit
3
  title: SMART_BUDGET_RECOMMENDATION
4
  sdk: docker
5
  emoji: ⚡
6
- colorFrom: blue
7
- colorTo: pink
 
8
  ---
9
  # Smart Budget Recommendation API
10
-
11
- A FastAPI-based service that provides intelligent budget recommendations based on past spending behavior. This service analyzes historical expense data and suggests personalized budgets for each category.
12
-
13
- ## Features
14
-
15
- - **Smart Budget Recommendations**: Analyzes past spending patterns and recommends personalized budgets
16
- - **Category-based Analysis**: Provides recommendations for each expense category
17
- - **Confidence Scoring**: Each recommendation includes a confidence score based on data quality
18
- - **MongoDB Integration**: Stores expenses and budgets in MongoDB
19
- - **RESTful API**: Clean API endpoints for expense tracking and budget management
20
-
21
- ## Example Recommendation
22
-
23
- ```json
24
- {
25
- "category": "Groceries",
26
- "average_expense": 3800.0,
27
- "recommended_budget": 4000.0,
28
- "reason": "Your average monthly grocery expense is Rs.3,800. We suggest setting your budget to Rs.4,000 for next month (includes a 5% buffer for variability).",
29
- "confidence": 0.85
30
- }
31
- ```
32
-
33
- ## API Endpoints
34
-
35
- ### Health Check
36
- - `GET /health` - Check API and database connection status
37
-
38
- ### Expenses
39
- - `POST /expenses` - Create a new expense
40
- - `GET /expenses?user_id={user_id}` - Get expenses for a user
41
-
42
- ### Budgets
43
- - `POST /budgets` - Create a new budget
44
- - `GET /budgets?user_id={user_id}` - Get budgets for a user
45
-
46
- ### Recommendations
47
- - `GET /recommendations/{user_id}?month={month}&year={year}` - Get smart budget recommendations
48
- - `GET /category-expenses/{user_id}?months={months}` - Get average expenses by category
49
-
50
- ## Local Development
51
-
52
- ### Prerequisites
53
- - Python 3.11+
54
- - MongoDB connection string
55
-
56
- ### Setup
57
-
58
- 1. Install dependencies:
59
- ```bash
60
- pip install -r requirements.txt
61
- ```
62
-
63
- 2. Set environment variables:
64
- ```bash
65
- export MONGODB_URI="mongodb://expenseuser:Kem_6o%3F%3F@165.227.69.221:27017/expense?authSource=admin"
66
- # optional
67
- export OPENAI_API_KEY="your_openai_key"
68
- ```
69
-
70
- 3. Run the application:
71
- ```bash
72
- uvicorn app.main:app --reload
73
- ```
74
-
75
- The API will be available at `http://localhost:8000`
76
-
77
- ## Docker Deployment
78
-
79
- ### Build Docker Image
80
- ```bash
81
- docker build -t smart-budget-recommendation .
82
- ```
83
-
84
- ### Run Docker Container
85
- ```bash
86
- docker run -p 7860:7860 -e MONGODB_URI="your_mongodb_uri" smart-budget-recommendation
87
- ```
88
-
89
- ## Hugging Face Deployment
90
-
91
- ### Prerequisites
92
- 1. Hugging Face account
93
- 2. Docker installed
94
- 3. MongoDB connection string stored as a secret in Hugging Face
95
-
96
- ### Steps
97
-
98
- 1. **Store secrets in Hugging Face**:
99
- - Go to your Hugging Face Space settings
100
- - Navigate to "Variables and secrets"
101
- - Add `MONGODB_URI` with your MongoDB connection string
102
- - *(Optional)* Add `OPENAI_API_KEY` if you want GPT-powered recommendations
103
-
104
- 2. **Create Hugging Face Space**:
105
- - Create a new Space on Hugging Face
106
- - Choose "Docker" as the SDK
107
-
108
- 3. **Upload Files**:
109
- - Upload all files from this repository to your Space
110
- - Ensure `Dockerfile` is in the root directory
111
-
112
- 4. **Configure Space**:
113
- - The Dockerfile will automatically build and run the application
114
- - The app will use the `MONGODB_URI` secret from Hugging Face environment variables
115
-
116
- ### Hugging Face Space Configuration
117
-
118
- The Dockerfile is configured to:
119
- - Use Python 3.11
120
- - Install all dependencies
121
- - Expose port 7860 (Hugging Face default)
122
- - Run the FastAPI application with Uvicorn
123
-
124
- Hugging Face will automatically:
125
- - Build the Docker image
126
- - Set environment variables from secrets
127
- - Deploy the container
128
- - Provide a public URL
129
-
130
- ## API Documentation
131
-
132
- Once deployed, access interactive API documentation at:
133
- - Swagger UI: `http://your-url/docs`
134
- - ReDoc: `http://your-url/redoc`
135
-
136
- ## Algorithm Details
137
-
138
- The Smart Budget Recommendation engine:
139
-
140
- 1. **Data Collection**: Analyzes expenses from the past 6 months
141
- 2. **Category Analysis**: Groups expenses by category and calculates:
142
- - Average monthly spending
143
- - Standard deviation (variability)
144
- - Number of transactions
145
- - Months of data available
146
- 3. **Budget Calculation**:
147
- - Base: Average monthly expense
148
- - Buffer: 5-10% based on spending variability
149
- - Rounding: Rounds to nearest 100 for cleaner numbers
150
- 4. **Confidence Scoring**:
151
- - Based on data quality (months analyzed, transaction count, consistency)
152
- - Returns score from 0-1
153
- 5. **(Optional) GPT Refinement**:
154
- - If `OPENAI_API_KEY` is set, GPT reviews the history and suggests increase/decrease/keep along with a natural-language reason.
155
-
156
- ## Environment Variables
157
-
158
- - `MONGODB_URI`: MongoDB connection string (required)
159
- - `OPENAI_API_KEY`: Optional. If set, GPT-based recommendations augment the rule-based engine.
160
-
161
- ## License
162
-
163
- Copyright © 2025 WalletSync. All Rights Reserved
 
3
  title: SMART_BUDGET_RECOMMENDATION
4
  sdk: docker
5
  emoji: ⚡
6
+ colorFrom: green
7
+ colorTo: yellow
8
+ pinned: true
9
  ---
10
  # Smart Budget Recommendation API