ML-services / README.md
subhan971's picture
Upload 8 files
ca22b37 verified
|
Raw
History Blame Contribute Delete
4.85 kB
---
title: ML Services
emoji: πŸ’
colorFrom: pink
colorTo: red
sdk: docker
pinned: false
---
# πŸ’ Soul Mate Matchmaking API
AI-powered partner recommendation using Supabase data and scikit-learn.
---
## 🎯 What It Does
1. Takes a **user ID (UID)** as input
2. Fetches user profile from **Supabase**
3. Compares with opposite-gender profiles using ML model
4. Returns **top compatible matches** (0-100% score)
---
## πŸš€ API Endpoint
### `GET /recommend/{user_id}`
Get AI-matched partners for a user.
**Query Parameters:**
- `top_n` (optional, default=10): number of matches to return
**Response:**
```json
{
"matches": [
{
"user_id": "uuid-here",
"name": "Fatima Ali",
"age": 26,
"city": "Karachi",
"compatibility_score": 87.5,
"photo_url": "https://..."
}
]
}
```
**Example:**
```bash
curl http://127.0.0.1:7860/recommend/44d81ff7-93d2-4805-a973-df9a62a99cf2?top_n=5
```
---
### `POST /feedback`
Record a like/reject action on a match.
**Body:**
```json
{
"user_id": "user-123",
"target_id": "user-456",
"action": "like"
}
```
`action`: `"like"` or `"reject"`
**Response:**
```json
{ "status": "ok" }
```
---
### `GET /health`
Check if model is loaded.
```json
{ "status": "running", "model": "loaded" }
```
---
## πŸ”§ Local Development
```bash
# Install dependencies
pip install -r requirements.txt
# Train model on Supabase data
python train.py
# Start server
python start.py
# or
uvicorn app:app --reload --port 7860
```
**Interactive docs:** http://localhost:7860/docs
---
## 🧠 Model Features
The model encodes **10 profile attributes**:
| Category | Columns |
|----------|---------|
| Numeric (1) | `age` |
| Categorical (6) | `religion`, `marital_status`, `qualification`, `country`, `maslak`, `region_caste` |
| Text (3) | `hobbies`, `personality_traits`, `preferred_partner_criteria` |
**Technique:**
- LabelEncoder for categoricals
- TF-IDF (max_features=20) for text fields
- MinMaxScaler for age
- Cosine similarity via NearestNeighbors
**Total feature dimensions:** 1 + 6 + (20Γ—3) = **67 features**
---
## 🚒 Deployment to HuggingFace Spaces
```bash
# 1. Train locally first
python train.py
# 2. Clone your HF space
git clone https://huggingface.co/spaces/subhan971/ML-services
cd ML-services
# 3. Copy files
xcopy /s /y "D:\soulmate\soul_mate_app_flutter_backup\recommendation_service\*" ".\"
# 4. Ensure model exists
if not exist "models\recommendation_model.pkl" (
echo Model missing! Run train.py first.
exit /b 1
)
# 5. Setup LFS
git lfs track "*.pkl"
git add .gitattributes
# 6. Commit & push
git add .
git commit -m "deploy: matchmaking model"
git push origin main
```
**Space URL:** https://subhan971-ml-services.hf.space
---
## βš™οΈ Environment Variables
| Variable | Description |
|----------|-------------|
| `SUPABASE_URL` | Your Supabase project URL |
| `SUPABASE_SERVICE_KEY` | Service role key (for read access) |
| `PORT` | Server port (default: 7860) |
Set **before** starting the server:
```bash
export SUPABASE_URL=https://xxxx.supabase.co
export SUPABASE_SERVICE_KEY=your-service-role-key
```
---
## πŸ“Š Training Data
Model is trained on all **active profiles** from Supabase `profiles` table.
To retrain after adding new users:
```bash
python train.py
```
The script:
1. Fetches all active profiles from Supabase
2. Preprocesses features
3. Trains NearestNeighbors model
4. Saves to `models/recommendation_model.pkl`
---
## πŸ”„ Flutter Integration
Flutter calls:
```dart
final response = await dio.get(
'https://subhan971-ml-services.hf.space/recommend/$userId',
queryParameters: {'top_n': 10}
);
```
Matches are displayed with:
- Name
- Age
- City
- Compatibility % score
- Photo
---
## πŸ“ Project Structure
```
recommendation_service/
β”œβ”€β”€ app.py # FastAPI server (2 endpoints)
β”œβ”€β”€ train.py # Training script (Supabase β†’ model)
β”œβ”€β”€ requirements.txt # Dependencies
β”œβ”€β”€ Dockerfile # HF Spaces container
β”œβ”€β”€ start.py # Launch with env vars
β”œβ”€β”€ models/
β”‚ └── recommendation_model.pkl # Trained model
β”œβ”€β”€ README.md
β”œβ”€β”€ .gitignore
└── .gitattributes # LFS tracking
```
---
## βœ… Requirements Met
- βœ… Fetch user data from Supabase using UID
- βœ… Preprocess: age, religion, preferred_partner_criteria, personality_traits, hobbies, qualification, marital_status, nationality, sect, region/caste, gender
- βœ… Train ML model (TF-IDF + LabelEncoder + NearestNeighbors)
- βœ… Save as `.pkl`
- βœ… Backend API: `/recommend/{user_id}` returns ranked matches
- βœ… Flutter integration: AI Match button β†’ API β†’ display results
- βœ… Only opposite-gender matches
- βœ… Compatibility score 0-100%
---
**Questions?** Check `/docs` when server is running.