File size: 6,196 Bytes
f953306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a5665b
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
230
231
232
233
234
235
236
237
238
---
title: FaceMatch Azure Dev
emoji: 🐨
colorFrom: red
colorTo: green
sdk: docker
pinned: false
---

# FaceMatch FastAPI

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.

## Features

- **Face Detection & Embedding**: Uses InsightFace for robust face detection and embedding extraction
- **Similarity Matching**: Finds similar faces using cosine similarity on face embeddings
- **Personalized Recommendations**: Learns from user likes/dislikes to provide personalized matches
- **Gender Filtering**: Filter recommendations by gender (male, female, or all)
- **Azure Integration**: Stores images and embeddings in Azure Blob Storage
- **FastAPI**: Modern, fast web framework with automatic API documentation

## API Endpoints

### Core Endpoints

- `GET /` - Health check and welcome message
- `POST /api/init_user` - Initialize a new user session
- `GET /api/get_training_images` - Get training images for user preference learning
- `POST /api/record_preference` - Record user like/dislike preferences
- `POST /api/get_matches` - Get personalized matches based on user preferences
- `POST /api/get_recommendations` - Get recommendations based on query images
- `POST /api/extract_embeddings` - Extract embeddings from all images (admin)

### API Documentation

Visit `/docs` for interactive Swagger UI documentation when running locally.

## Local Setup

### Prerequisites

- Python 3.8+
- Azure Blob Storage account
- Azure credentials

### Installation

1. **Clone the repository**
   ```bash
   git clone <your-repo-url>
   cd Facematch_Dev
   ```

2. **Install dependencies**
   ```bash
   pip install -r requirements.txt
   ```

3. **Configure Azure credentials**
   
   Set your Azure credentials as environment variables:
   ```bash
   export AZURE_STORAGE_CONNECTION_STRING="your_connection_string"
   export AZURE_CONTAINER_NAME="your_container_name"
   ```
   
   Or create a `config.py` file with your credentials.

4. **Run the application**
   ```bash
   python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000
   ```

5. **Access the API**
   - API: http://localhost:8000
   - Documentation: http://localhost:8000/docs

## Usage Examples

### Get Recommendations

**Direct Format:**
```bash
curl -X POST "http://localhost:8000/api/get_recommendations" \
  -H "Content-Type: application/json" \
  -d '{
    "query_images": [
      "https://your-azure-url/image1.jpg",
      "https://your-azure-url/image2.jpg"
    ],
    "gender": "female",
    "top_n": 5
  }'
```

**Hugging Face Format:**
```bash
curl -X POST "http://localhost:8000/api/get_recommendations" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": {
      "query_images": [
        "https://your-azure-url/image1.jpg",
        "https://your-azure-url/image2.jpg"
      ],
      "gender": "female",
      "top_n": 5
    }
  }'
```

### Initialize User Session
```bash
curl -X POST "http://localhost:8000/api/init_user"
```

### Record Preferences
```bash
curl -X POST "http://localhost:8000/api/record_preference" \
  -H "Content-Type: application/json" \
  -d '{
    "user_id": "your_user_id",
    "image_url": "https://your-azure-url/image.jpg",
    "preference": "like"
  }'
```

## Hugging Face Spaces Deployment

### 1. Create a Hugging Face Space

1. Go to [Hugging Face Spaces](https://huggingface.co/spaces)
2. Click "Create new Space"
3. Choose "FastAPI" as the SDK
4. Set visibility (public or private)
5. Create the space

### 2. Configure Secrets

In your Hugging Face Space settings, add these secrets:

- `AZURE_STORAGE_CONNECTION_STRING`: Your Azure connection string
- `AZURE_CONTAINER_NAME`: Your Azure container name

### 3. Upload Files

Upload these files to your Hugging Face Space:

- `main.py` - FastAPI application
- `handler.py` - Face matching logic
- `requirements.txt` - Dependencies
- `config.py` - Configuration (if using file-based config)

### 4. Deploy

The space will automatically build and deploy your FastAPI application.

### 5. Access Your API

Your API will be available at:
```
https://your-username-your-space-name.hf.space
```

## Azure Setup

### Required Azure Resources

1. **Storage Account**: For storing images and embeddings
2. **Blob Container**: Organized with folders:
   - `ai-images/men/` - Training images for men
   - `ai-images/women/` - Training images for women
   - `profile-media/` - Images to search for matches

### Configuration

The application expects these Azure settings:

```python
# In config.py or environment variables
AZURE_STORAGE_CONNECTION_STRING = "your_connection_string"
AZURE_CONTAINER_NAME = "your_container_name"
```

## File Structure

```
Facematch_Dev/
β”œβ”€β”€ main.py                 # FastAPI application
β”œβ”€β”€ handler.py              # Face matching logic
β”œβ”€β”€ config.py               # Configuration
β”œβ”€β”€ requirements.txt        # Dependencies
β”œβ”€β”€ README.md              # This file
β”œβ”€β”€ templates/             # HTML templates (if needed)
└── user_preferences.json  # User preferences storage
```

## Performance Notes

- **Local Development**: Runs on CPU, suitable for testing
- **Hugging Face Spaces**: Runs on GPU, much faster for production
- **Embedding Extraction**: Run `/api/extract_embeddings` after uploading new images
- **Caching**: Embeddings are cached in Azure for faster subsequent queries

## Troubleshooting

### Common Issues

1. **Face Detection Fails**: Some images may not contain detectable faces
2. **Azure Connection**: Ensure credentials are correctly set
3. **Memory Issues**: Large image collections may require more memory on Hugging Face

### Debug Mode

Enable debug logging by setting environment variable:
```bash
export DEBUG=1
```

## Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Test thoroughly
5. Submit a pull request

## License

[Add your license information here]

## Support

For issues and questions:
- Create an issue on GitHub
- Check the API documentation at `/docs`
- Review the debug logs for detailed error information