Azam-Rabiee/wealthy-estimator-api
0
1---2title: Wealth Potential Estimator3emoji: ๐ฐ4colorFrom: blue5colorTo: purple6sdk: docker7sdk_version: 1.0.08app_file: app/main.py9pinned: false10---11 12# Wealth Potential Estimator API13 14A machine learning service that estimates a user's potential net worth based on a submitted selfie image. The API analyzes facial features and compares them to a database of wealthy individuals to provide wealth potential estimates.15 16# Live URL of the Endpoint17* Repo (Github): https://github.com/AzamRabiee/wealthy_estimator18* Repo (HF): https://huggingface.co/spaces/Azam-Rabiee/wealthy-estimator-api/tree/main19* Swagger: https://azam-rabiee-wealthy-estimator-api.hf.space/docs20 21## ๐๏ธ Architecture22 23### Overview24This project implements a **Wealth Potential Estimator** using:25- **FastAPI** for the REST API framework26- **Hugging Face Transformers** for pre-trained vision models27- **PyTorch** for deep learning inference28- **Docker** for containerization and deployment29 30### Key Components31 321. **Vision Model**: Uses `microsoft/resnet-50` for feature extraction332. **Embedding Extraction**: Converts selfie images to high-dimensional feature vectors343. **Similarity Computation**: Uses cosine similarity to find similar wealthy profiles354. **Wealth Estimation**: Weighted average based on similarity scores36 37### Data Flow38```39Selfie Image โ Preprocessing โ Feature Extraction โ Similarity Search โ Wealth Estimation โ API Response40```41 42## ๐ Quick Start43 44### Prerequisites45- Docker and Docker Compose46- Python 3.9+ (for local development)47 48### Using Docker (Recommended)49 501. **Clone and build**:51```bash52git clone <repository-url>53cd wealth-estimator-api54docker-compose up --build55```56 572. **Access the API**:58- API Documentation: http://localhost:8000/docs59- Health Check: http://localhost:8000/health60- Predict Endpoint: http://localhost:8000/predict61 62### Local Development63 641. **Install dependencies**:65```bash66pip install -r requirements.txt67```68 692. **Run the application**:70```bash71uvicorn app.main:app --reload --host 0.0.0.0 --port 800072```73 74## ๐ก API Endpoints75 76### POST `/predict`77Predicts wealth potential from a selfie image.78 79**Request**:80- Method: `POST`81- Content-Type: `multipart/form-data`82- Body: Image file (JPEG, PNG, etc.)83 84**Response**:85```json86{87 "estimated_net_worth": 50000000,88 "currency": "USD",89 "similar_profiles": [90 {91 "name": "Elon Musk",92 "net_worth": 250000000000,93 "occupation": "Entrepreneur",94 "similarity_score": 0.8595 }96 ],97 "confidence_score": 0.7598}99```100 101### GET `/health`102Health check endpoint.103 104### GET `/`105Root endpoint with API information.106 107## ๐ง Configuration108 109Key configuration options in `app/config.py`:110 111- `MODEL_NAME`: Pre-trained model to use (default: "microsoft/resnet-50")112- `MAX_IMAGE_SIZE`: Input image size (default: 224)113- `DEVICE`: Computation device (default: "cpu")114- `TOP_K_SIMILAR`: Number of similar profiles to return (default: 3)115 116## ๐ง Model Details117 118### Model Selection119- **Model**: `microsoft/resnet-50`120- **Reasoning**: 121 - Pre-trained on ImageNet with strong feature extraction capabilities122 - Lightweight and fast for inference123 - Good balance between accuracy and performance124 125### Feature Extraction Process1261. **Image Preprocessing**: Resize to 224x224, normalize with ImageNet stats1272. **Feature Extraction**: Use ResNet-50's last hidden state1283. **Embedding Normalization**: L2 normalization for consistent similarity computation129 130### Similarity Computation131- **Metric**: Cosine Similarity132- **Reasoning**: 133 - Scale-invariant similarity measure134 - Works well with normalized embeddings135 - Computationally efficient136 137### Wealth Estimation Algorithm138```python139estimated_net_worth = ฮฃ(similarity_score ร profile_net_worth) / ฮฃ(similarity_scores)140```141 142## ๐ Dataset143 144The wealthy profiles dataset (`data/wealthy_profiles.json`) contains:145- 10 mock wealthy individuals146- Each profile includes: name, net worth, occupation, and pre-computed embeddings147- Net worth values range from $65B to $250B148 149**Note**: This is a demonstration dataset. In production, you would:150- Use real wealthy individual data151- Implement proper data privacy measures152- Use more sophisticated embedding generation153 154## ๐๏ธ Engineering Decisions155 156### Framework Choices157- **FastAPI**: Modern, fast, automatic API documentation158- **PyTorch**: Industry standard for deep learning159- **Transformers**: Easy access to pre-trained models160 161### Performance Optimizations162- Model loading at startup (not per request)163- Image preprocessing optimization164- Efficient similarity computation165 166### Error Handling167- Comprehensive input validation168- Graceful error responses169- Detailed logging170 171### Security Considerations172- Input file validation173- Non-root Docker user174- Health checks for monitoring175 176## ๐งช Testing177 178### Manual Testing1791. Use the interactive API docs at `/docs`1802. Upload a selfie image1813. Verify the response format and values182 183### Example cURL Request184```bash185curl -X POST "http://localhost:8000/predict" \186 -H "accept: application/json" \187 -H "Content-Type: multipart/form-data" \188 -F "file=@your_selfie.jpg"189```190 191## ๐ Deployment192 193### Docker Deployment194```bash195# Build and run196docker-compose up --build197 198# Run in background199docker-compose up -d200```201 202### Cloud Deployment Options203- **Heroku**: Use the provided Dockerfile204- **AWS ECS**: Deploy using docker-compose205- **Google Cloud Run**: Container-based deployment206- **Azure Container Instances**: Serverless container deployment207 208## ๐ Assumptions209 2101. **Model Accuracy**: This is a demonstration project - accuracy is secondary to robustness2112. **Wealth Correlation**: Assumes facial features correlate with wealth potential (for demo purposes)2123. **Dataset**: Uses mock wealthy profiles - not real data2134. **Privacy**: No actual personal data is stored or processed beyond the request2145. **Scalability**: Designed for moderate traffic - can be scaled with load balancers215 216## ๐ฎ Future Improvements217 2181. **Model Enhancement**: 219 - Fine-tune on wealth-related datasets220 - Use ensemble models for better accuracy221 2222. **Data Pipeline**:223 - Real-time wealthy profile updates224 - More diverse dataset225 2263. **API Features**:227 - Batch processing228 - Authentication and rate limiting229 - Caching for similar requests230 2314. **Monitoring**:232 - Request/response logging233 - Performance metrics234 - Error tracking235 236## ๐ License237 238This project is for demonstration purposes only.239 240## ๐ค Contributing241 242This is a demonstration project for a coding interview. For production use, please implement proper security, privacy, and ethical considerations. 