Spaces:
Runtime error
Runtime error
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,229 +1,238 @@
|
|
| 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 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
###
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
```
|
| 189 |
-
|
| 190 |
-
#
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
##
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
- Review the debug logs for detailed error information
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: FaceMatch Azure Dev
|
| 3 |
+
emoji: π¨
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# FaceMatch FastAPI
|
| 11 |
+
|
| 12 |
+
A face matching and recommendation system built with FastAPI, InsightFace, and Azure Blob Storage. This application provides personalized face recommendations based on user preferences and similarity matching.
|
| 13 |
+
|
| 14 |
+
## Features
|
| 15 |
+
|
| 16 |
+
- **Face Detection & Embedding**: Uses InsightFace for robust face detection and embedding extraction
|
| 17 |
+
- **Similarity Matching**: Finds similar faces using cosine similarity on face embeddings
|
| 18 |
+
- **Personalized Recommendations**: Learns from user likes/dislikes to provide personalized matches
|
| 19 |
+
- **Gender Filtering**: Filter recommendations by gender (male, female, or all)
|
| 20 |
+
- **Azure Integration**: Stores images and embeddings in Azure Blob Storage
|
| 21 |
+
- **FastAPI**: Modern, fast web framework with automatic API documentation
|
| 22 |
+
|
| 23 |
+
## API Endpoints
|
| 24 |
+
|
| 25 |
+
### Core Endpoints
|
| 26 |
+
|
| 27 |
+
- `GET /` - Health check and welcome message
|
| 28 |
+
- `POST /api/init_user` - Initialize a new user session
|
| 29 |
+
- `GET /api/get_training_images` - Get training images for user preference learning
|
| 30 |
+
- `POST /api/record_preference` - Record user like/dislike preferences
|
| 31 |
+
- `POST /api/get_matches` - Get personalized matches based on user preferences
|
| 32 |
+
- `POST /api/get_recommendations` - Get recommendations based on query images
|
| 33 |
+
- `POST /api/extract_embeddings` - Extract embeddings from all images (admin)
|
| 34 |
+
|
| 35 |
+
### API Documentation
|
| 36 |
+
|
| 37 |
+
Visit `/docs` for interactive Swagger UI documentation when running locally.
|
| 38 |
+
|
| 39 |
+
## Local Setup
|
| 40 |
+
|
| 41 |
+
### Prerequisites
|
| 42 |
+
|
| 43 |
+
- Python 3.8+
|
| 44 |
+
- Azure Blob Storage account
|
| 45 |
+
- Azure credentials
|
| 46 |
+
|
| 47 |
+
### Installation
|
| 48 |
+
|
| 49 |
+
1. **Clone the repository**
|
| 50 |
+
```bash
|
| 51 |
+
git clone <your-repo-url>
|
| 52 |
+
cd Facematch_Dev
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
2. **Install dependencies**
|
| 56 |
+
```bash
|
| 57 |
+
pip install -r requirements.txt
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
3. **Configure Azure credentials**
|
| 61 |
+
|
| 62 |
+
Set your Azure credentials as environment variables:
|
| 63 |
+
```bash
|
| 64 |
+
export AZURE_STORAGE_CONNECTION_STRING="your_connection_string"
|
| 65 |
+
export AZURE_CONTAINER_NAME="your_container_name"
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
Or create a `config.py` file with your credentials.
|
| 69 |
+
|
| 70 |
+
4. **Run the application**
|
| 71 |
+
```bash
|
| 72 |
+
python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
5. **Access the API**
|
| 76 |
+
- API: http://localhost:8000
|
| 77 |
+
- Documentation: http://localhost:8000/docs
|
| 78 |
+
|
| 79 |
+
## Usage Examples
|
| 80 |
+
|
| 81 |
+
### Get Recommendations
|
| 82 |
+
|
| 83 |
+
**Direct Format:**
|
| 84 |
+
```bash
|
| 85 |
+
curl -X POST "http://localhost:8000/api/get_recommendations" \
|
| 86 |
+
-H "Content-Type: application/json" \
|
| 87 |
+
-d '{
|
| 88 |
+
"query_images": [
|
| 89 |
+
"https://your-azure-url/image1.jpg",
|
| 90 |
+
"https://your-azure-url/image2.jpg"
|
| 91 |
+
],
|
| 92 |
+
"gender": "female",
|
| 93 |
+
"top_n": 5
|
| 94 |
+
}'
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
**Hugging Face Format:**
|
| 98 |
+
```bash
|
| 99 |
+
curl -X POST "http://localhost:8000/api/get_recommendations" \
|
| 100 |
+
-H "Content-Type: application/json" \
|
| 101 |
+
-d '{
|
| 102 |
+
"inputs": {
|
| 103 |
+
"query_images": [
|
| 104 |
+
"https://your-azure-url/image1.jpg",
|
| 105 |
+
"https://your-azure-url/image2.jpg"
|
| 106 |
+
],
|
| 107 |
+
"gender": "female",
|
| 108 |
+
"top_n": 5
|
| 109 |
+
}
|
| 110 |
+
}'
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
### Initialize User Session
|
| 114 |
+
```bash
|
| 115 |
+
curl -X POST "http://localhost:8000/api/init_user"
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
### Record Preferences
|
| 119 |
+
```bash
|
| 120 |
+
curl -X POST "http://localhost:8000/api/record_preference" \
|
| 121 |
+
-H "Content-Type: application/json" \
|
| 122 |
+
-d '{
|
| 123 |
+
"user_id": "your_user_id",
|
| 124 |
+
"image_url": "https://your-azure-url/image.jpg",
|
| 125 |
+
"preference": "like"
|
| 126 |
+
}'
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
## Hugging Face Spaces Deployment
|
| 130 |
+
|
| 131 |
+
### 1. Create a Hugging Face Space
|
| 132 |
+
|
| 133 |
+
1. Go to [Hugging Face Spaces](https://huggingface.co/spaces)
|
| 134 |
+
2. Click "Create new Space"
|
| 135 |
+
3. Choose "FastAPI" as the SDK
|
| 136 |
+
4. Set visibility (public or private)
|
| 137 |
+
5. Create the space
|
| 138 |
+
|
| 139 |
+
### 2. Configure Secrets
|
| 140 |
+
|
| 141 |
+
In your Hugging Face Space settings, add these secrets:
|
| 142 |
+
|
| 143 |
+
- `AZURE_STORAGE_CONNECTION_STRING`: Your Azure connection string
|
| 144 |
+
- `AZURE_CONTAINER_NAME`: Your Azure container name
|
| 145 |
+
|
| 146 |
+
### 3. Upload Files
|
| 147 |
+
|
| 148 |
+
Upload these files to your Hugging Face Space:
|
| 149 |
+
|
| 150 |
+
- `main.py` - FastAPI application
|
| 151 |
+
- `handler.py` - Face matching logic
|
| 152 |
+
- `requirements.txt` - Dependencies
|
| 153 |
+
- `config.py` - Configuration (if using file-based config)
|
| 154 |
+
|
| 155 |
+
### 4. Deploy
|
| 156 |
+
|
| 157 |
+
The space will automatically build and deploy your FastAPI application.
|
| 158 |
+
|
| 159 |
+
### 5. Access Your API
|
| 160 |
+
|
| 161 |
+
Your API will be available at:
|
| 162 |
+
```
|
| 163 |
+
https://your-username-your-space-name.hf.space
|
| 164 |
+
```
|
| 165 |
+
|
| 166 |
+
## Azure Setup
|
| 167 |
+
|
| 168 |
+
### Required Azure Resources
|
| 169 |
+
|
| 170 |
+
1. **Storage Account**: For storing images and embeddings
|
| 171 |
+
2. **Blob Container**: Organized with folders:
|
| 172 |
+
- `ai-images/men/` - Training images for men
|
| 173 |
+
- `ai-images/women/` - Training images for women
|
| 174 |
+
- `profile-media/` - Images to search for matches
|
| 175 |
+
|
| 176 |
+
### Configuration
|
| 177 |
+
|
| 178 |
+
The application expects these Azure settings:
|
| 179 |
+
|
| 180 |
+
```python
|
| 181 |
+
# In config.py or environment variables
|
| 182 |
+
AZURE_STORAGE_CONNECTION_STRING = "your_connection_string"
|
| 183 |
+
AZURE_CONTAINER_NAME = "your_container_name"
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
## File Structure
|
| 187 |
+
|
| 188 |
+
```
|
| 189 |
+
Facematch_Dev/
|
| 190 |
+
βββ main.py # FastAPI application
|
| 191 |
+
βββ handler.py # Face matching logic
|
| 192 |
+
βββ config.py # Configuration
|
| 193 |
+
βββ requirements.txt # Dependencies
|
| 194 |
+
βββ README.md # This file
|
| 195 |
+
βββ templates/ # HTML templates (if needed)
|
| 196 |
+
βββ user_preferences.json # User preferences storage
|
| 197 |
+
```
|
| 198 |
+
|
| 199 |
+
## Performance Notes
|
| 200 |
+
|
| 201 |
+
- **Local Development**: Runs on CPU, suitable for testing
|
| 202 |
+
- **Hugging Face Spaces**: Runs on GPU, much faster for production
|
| 203 |
+
- **Embedding Extraction**: Run `/api/extract_embeddings` after uploading new images
|
| 204 |
+
- **Caching**: Embeddings are cached in Azure for faster subsequent queries
|
| 205 |
+
|
| 206 |
+
## Troubleshooting
|
| 207 |
+
|
| 208 |
+
### Common Issues
|
| 209 |
+
|
| 210 |
+
1. **Face Detection Fails**: Some images may not contain detectable faces
|
| 211 |
+
2. **Azure Connection**: Ensure credentials are correctly set
|
| 212 |
+
3. **Memory Issues**: Large image collections may require more memory on Hugging Face
|
| 213 |
+
|
| 214 |
+
### Debug Mode
|
| 215 |
+
|
| 216 |
+
Enable debug logging by setting environment variable:
|
| 217 |
+
```bash
|
| 218 |
+
export DEBUG=1
|
| 219 |
+
```
|
| 220 |
+
|
| 221 |
+
## Contributing
|
| 222 |
+
|
| 223 |
+
1. Fork the repository
|
| 224 |
+
2. Create a feature branch
|
| 225 |
+
3. Make your changes
|
| 226 |
+
4. Test thoroughly
|
| 227 |
+
5. Submit a pull request
|
| 228 |
+
|
| 229 |
+
## License
|
| 230 |
+
|
| 231 |
+
[Add your license information here]
|
| 232 |
+
|
| 233 |
+
## Support
|
| 234 |
+
|
| 235 |
+
For issues and questions:
|
| 236 |
+
- Create an issue on GitHub
|
| 237 |
+
- Check the API documentation at `/docs`
|
| 238 |
- Review the debug logs for detailed error information
|