Closed devansh-shah-11 closed 3 months ago
0b2bf4e2dc
)[!TIP] I can email you next time I complete a pull request if you set up your email here!
Here are the GitHub Actions logs prior to making any changes:
91e83d1
Checking API/database.py for syntax errors... ✅ API/database.py has no syntax errors!
1/1 ✓Checking API/database.py for syntax errors... ✅ API/database.py has no syntax errors!
Sandbox passed on the latest main
, so sandbox checks will be enabled for this issue.
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
API/database.py
✓ https://github.com/devansh-shah-11/FaceRec/commit/d6366ebfcc133c30f5e069c0508a89b52686ba57 Edit
Modify API/database.py with contents:
• Add a new method named `find_similar_vectors` in the `Database` class. This method should accept two parameters: `embedding_vector`, which is the vector for which we want to find similar vectors, and `n`, which is the number of top similar vectors to return.
• Inside this method, use MongoDB's aggregation framework to perform the vector similarity search. Since MongoDB does not natively support Euclidean distance calculations for vector similarity out of the box, you will need to manually implement this logic. One approach is to store the embedding vectors in a collection with a schema that includes the vector and a unique identifier. Then, use an aggregation pipeline to calculate the Euclidean distance between the input vector and the vectors stored in the database, sort the results by this calculated distance in ascending order, and limit the results to the top n entries.
• The method should return the top n most similar vectors from the MongoDB database.
• Note: This task assumes MongoDB does not have built-in support for vector similarity search based on Euclidean distance. If MongoDB introduces such a feature, the implementation should leverage that instead.
--- +++ @@ -22,3 +22,31 @@ def update_one(self, collection, query, update): return self.db[collection].update_one(query, update) + def find_similar_vectors(self, collection, embedding_vector, n): + """ + Find the top n most similar vectors in the database to the given embedding_vector. + This method uses the Euclidean distance for similarity measure. + + :param collection: The MongoDB collection to search within. + :param embedding_vector: The embedding vector to find similar vectors for. + :param n: The number of top similar vectors to return. + :return: The top n most similar vectors from the MongoDB database. + """ + pipeline = [ + { + "$addFields": { + "distance": { + "$sqrt": { + "$reduce": { + "input": {"$zip": {"inputs": ["$vector", embedding_vector]}}, + "initialValue": 0, + "in": {"$add": ["$$value", {"$pow": [{"$subtract": ["$$this.0", "$$this.1"]}, 2]}]} + } + } + } + } + }, + {"$sort": {"distance": 1}}, + {"$limit": n} + ] + return list(self.db[collection].aggregate(pipeline))
API/database.py
✓ Edit
Check API/database.py with contents:
Ran GitHub Actions for d6366ebfcc133c30f5e069c0508a89b52686ba57:
API/route.py
✓ https://github.com/devansh-shah-11/FaceRec/commit/7b8ca4e13c930240c7aef7d25b09dd19d42e82df Edit
Modify API/route.py with contents:
• Add a new endpoint in the `route.py` file for the `recognise_face` functionality. This endpoint should accept an embedding vector and a parameter n from the user, and use the `find_similar_vectors` method from the `Database` class to find and return the top n most similar vectors.
• The endpoint should extract the embedding vector and the value of n from the request, call the `find_similar_vectors` method with these parameters, and return the result to the client.
• Ensure proper error handling is in place for cases where the input data is invalid or the database operation fails.
--- +++ @@ -267,3 +267,23 @@ client.find_one_and_delete(collection, {"EmployeeCode": EmployeeCode}) return {"Message": "Successfully Deleted"} +@router.post("/recognise_face") +async def recognise_face(embedding: List[float], n: int): + """ + Recognise a face by finding the most similar face embeddings in the database. + + Args: + embedding (List[float]): The embedding vector of the face to be recognised. + n (int): The number of top similar vectors to return. + + Returns: + dict: A dictionary containing the top n most similar face embeddings. + + """ + logging.info("Recognising face") + try: + similar_faces = client.find_similar_vectors(collection, embedding, n) + return {"similar_faces": similar_faces} + except Exception as e: + logging.error(f"Error recognising face: {str(e)}") + raise HTTPException(status_code=500, detail="Internal server error")
API/route.py
✓ Edit
Check API/route.py with contents:
Ran GitHub Actions for 7b8ca4e13c930240c7aef7d25b09dd19d42e82df:
I have finished reviewing the code for completeness. I did not find errors for sweep/utility_function_for_vector_similarity_s_0cb05
.
💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.Something wrong? Let us know.
This is an automated message generated by Sweep AI.
Description We need a new utility function in Database.py that performs a vector similarity search. This function should take an embedding vector as input and return the most similar vectors from the MongoDB Atlas database using Euclidean distance as the similarity measure.
This utility function will be used by the recognise_face() endpoint to find the most similar face in the database.
Expected Behavior
The endpoint should take n as input from the user and return the top n most similar vectors from MongoDB Database
Benefits This feature will automate the finding of top n most similar vectors to the given face to help identify the employee
Tasks Explore the MongoDB vector search tutorial Write a function to return the most similar vectors
Checklist
- [X] Modify `API/database.py` ✓ https://github.com/devansh-shah-11/FaceRec/commit/d6366ebfcc133c30f5e069c0508a89b52686ba57 [Edit](https://github.com/devansh-shah-11/FaceRec/edit/sweep/utility_function_for_vector_similarity_s_0cb05/API/database.py#L22-L22) - [X] Running GitHub Actions for `API/database.py` ✓ [Edit](https://github.com/devansh-shah-11/FaceRec/edit/sweep/utility_function_for_vector_similarity_s_0cb05/API/database.py#L22-L22) - [X] Modify `API/route.py` ✓ https://github.com/devansh-shah-11/FaceRec/commit/7b8ca4e13c930240c7aef7d25b09dd19d42e82df [Edit](https://github.com/devansh-shah-11/FaceRec/edit/sweep/utility_function_for_vector_similarity_s_0cb05/API/route.py#L176-L220) - [X] Running GitHub Actions for `API/route.py` ✓ [Edit](https://github.com/devansh-shah-11/FaceRec/edit/sweep/utility_function_for_vector_similarity_s_0cb05/API/route.py#L176-L220)