Upload 4 files
Browse files- README.md +12 -0
- app.py +86 -0
- recipes.csv +0 -0
- requirements.txt +5 -0
README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: LeftOver
|
| 3 |
+
emoji: 🐨
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
sdk_version: 1.31.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 3 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 4 |
+
import joblib
|
| 5 |
+
import re
|
| 6 |
+
import streamlit as st
|
| 7 |
+
|
| 8 |
+
# Load the recipe dataset
|
| 9 |
+
recipes_df = pd.read_csv('recipes.csv')
|
| 10 |
+
|
| 11 |
+
# Clean the dataset by removing unnecessary columns
|
| 12 |
+
recipes_df.drop(columns=["prep_time", "cook_time", "total_time", "yield", "rating",
|
| 13 |
+
"cuisine_path", "nutrition"], inplace=True)
|
| 14 |
+
|
| 15 |
+
# Create a bag of words for the ingredients column using only food names
|
| 16 |
+
cv = CountVectorizer(stop_words='english', token_pattern=r'\b[A-Za-z]+\b')
|
| 17 |
+
ingredients_matrix = cv.fit_transform(recipes_df['ingredients'])
|
| 18 |
+
|
| 19 |
+
# Calculate the cosine similarity between the ingredients matrix
|
| 20 |
+
cosine_sim = cosine_similarity(ingredients_matrix)
|
| 21 |
+
|
| 22 |
+
def get_recipe_recommendations(leftover_ingredients):
|
| 23 |
+
if leftover_ingredients is None:
|
| 24 |
+
return []
|
| 25 |
+
|
| 26 |
+
# Transform the leftover ingredients to contain only food names
|
| 27 |
+
leftover_ingredients = ', '.join(re.findall(r'\b[A-Za-z]+\b', leftover_ingredients))
|
| 28 |
+
|
| 29 |
+
# Transform the recipes ingredients to contain only food names
|
| 30 |
+
recipes_df['ingredients'] = recipes_df['ingredients'].apply(
|
| 31 |
+
lambda x: ', '.join(re.findall(r'\b[A-Za-z]+\b', x))
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Calculate cosine similarity
|
| 35 |
+
ingredients_matrix = cv.transform(recipes_df['ingredients'])
|
| 36 |
+
cosine_similarities = cosine_similarity(ingredients_matrix, cv.transform([leftover_ingredients]))
|
| 37 |
+
|
| 38 |
+
# Add cosine_sim column to recipes_df
|
| 39 |
+
recipes_df['cosine_sim'] = cosine_similarities.flatten()
|
| 40 |
+
|
| 41 |
+
# Get recipe recommendations
|
| 42 |
+
sorted_df = recipes_df.sort_values('cosine_sim', ascending=False).reset_index()
|
| 43 |
+
sorted_df['rank'] = sorted_df.index + 1 # Add rank column starting from 1
|
| 44 |
+
recommendations = sorted_df[['rank', 'recipe_name', 'cosine_sim', 'url', 'img_src']].head(10)
|
| 45 |
+
|
| 46 |
+
return recommendations.to_dict(orient='records')
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
st.title('Recipe Recommendation System')
|
| 50 |
+
st.text('Provide ingredients name by commas separated')
|
| 51 |
+
ingred = st.text_input(
|
| 52 |
+
'Enter the leftover ingredients separated by commas: ')
|
| 53 |
+
if st.button('Recommend'):
|
| 54 |
+
recommendations = get_recipe_recommendations(ingred)
|
| 55 |
+
|
| 56 |
+
# Convert the list of dictionaries into a DataFrame
|
| 57 |
+
df = pd.DataFrame(recommendations)
|
| 58 |
+
|
| 59 |
+
# Display the DataFrame in Streamlit
|
| 60 |
+
|
| 61 |
+
# Pre-process the DataFrame
|
| 62 |
+
df['cosine_sim'] = df['cosine_sim'] * 100
|
| 63 |
+
|
| 64 |
+
# st.dataframe(df)
|
| 65 |
+
st.data_editor(df, column_config={
|
| 66 |
+
'rank': st.column_config.NumberColumn(
|
| 67 |
+
"Rank", format="%.0f"),
|
| 68 |
+
'recipe_name': st.column_config.TextColumn(
|
| 69 |
+
"Recipe Name"),
|
| 70 |
+
'cosine_sim': st.column_config.NumberColumn(
|
| 71 |
+
"Similarity", format="%.2f%%"), # Modify the format to display as percentage
|
| 72 |
+
'img_src': st.column_config.ImageColumn(
|
| 73 |
+
"Preview Image"),
|
| 74 |
+
'url': st.column_config.LinkColumn(
|
| 75 |
+
"Link", display_text="Open Recipe's link")
|
| 76 |
+
})
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# # Take user input for leftover ingredients
|
| 81 |
+
# leftover_ingredients = input("Enter the leftover ingredients separated by commas: ")
|
| 82 |
+
# recommendations = get_recipe_recommendations(leftover_ingredients)
|
| 83 |
+
# print(recommendations)
|
| 84 |
+
|
| 85 |
+
# # Save the model
|
| 86 |
+
# joblib.dump(cosine_sim, 'recipe_rec.joblib')
|
recipes.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
numpy
|
| 3 |
+
scikit-learn
|
| 4 |
+
flask
|
| 5 |
+
joblib
|