Spaces:
Runtime error
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 messagePOST /api/init_user- Initialize a new user sessionGET /api/get_training_images- Get training images for user preference learningPOST /api/record_preference- Record user like/dislike preferencesPOST /api/get_matches- Get personalized matches based on user preferencesPOST /api/get_recommendations- Get recommendations based on query imagesPOST /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
Clone the repository
git clone <your-repo-url> cd Facematch_DevInstall dependencies
pip install -r requirements.txtConfigure Azure credentials
Set your Azure credentials as environment variables:
export AZURE_STORAGE_CONNECTION_STRING="your_connection_string" export AZURE_CONTAINER_NAME="your_container_name"Or create a
config.pyfile with your credentials.Run the application
python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000Access the API
- API: http://localhost:8000
- Documentation: http://localhost:8000/docs
Usage Examples
Get Recommendations
Direct Format:
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:
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
curl -X POST "http://localhost:8000/api/init_user"
Record Preferences
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
- Go to Hugging Face Spaces
- Click "Create new Space"
- Choose "FastAPI" as the SDK
- Set visibility (public or private)
- Create the space
2. Configure Secrets
In your Hugging Face Space settings, add these secrets:
AZURE_STORAGE_CONNECTION_STRING: Your Azure connection stringAZURE_CONTAINER_NAME: Your Azure container name
3. Upload Files
Upload these files to your Hugging Face Space:
main.py- FastAPI applicationhandler.py- Face matching logicrequirements.txt- Dependenciesconfig.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
- Storage Account: For storing images and embeddings
- Blob Container: Organized with folders:
ai-images/men/- Training images for menai-images/women/- Training images for womenprofile-media/- Images to search for matches
Configuration
The application expects these Azure settings:
# 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_embeddingsafter uploading new images - Caching: Embeddings are cached in Azure for faster subsequent queries
Troubleshooting
Common Issues
- Face Detection Fails: Some images may not contain detectable faces
- Azure Connection: Ensure credentials are correctly set
- Memory Issues: Large image collections may require more memory on Hugging Face
Debug Mode
Enable debug logging by setting environment variable:
export DEBUG=1
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- 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