Spaces:
Sleeping
Sleeping
Download model/recommender.py from nutripath/nutripath-ml: direct link, hf CLI and curl.
- Browser
- Download file 721 Bytes
-
https://huggingface.co/spaces/nutripath/nutripath-ml/resolve/main/model/recommender.py
- Command line
-
hf download hf://spaces/nutripath/nutripath-ml/model/recommender.py
-
curl -L -o recommender.py https://huggingface.co/spaces/nutripath/nutripath-ml/resolve/main/model/recommender.py
721 Bytes
| from sklearn.metrics.pairwise import cosine_similarity | |
| def compute_similarity(matrix): | |
| return cosine_similarity(matrix) | |
| def recommend(food_index, similarity_matrix, df, top_k=5): | |
| scores = list(enumerate(similarity_matrix[food_index])) | |
| scores = sorted(scores, key=lambda x: x[1], reverse=True) | |
| scores = scores[1:top_k + 1] | |
| results = [] | |
| for i, score in scores: | |
| row = df.iloc[i] | |
| results.append({ | |
| 'index': i, | |
| 'score': round(score, 4), | |
| 'calories': row['calories'], | |
| 'protein': row['protein_g'], | |
| 'carbs': row['carbs_g'], | |
| 'fat': row['fat_g'], | |
| 'class': row['calorie_class'] | |
| }) | |
| return results |