| import pandas as pd |
| from sklearn.feature_extraction.text import CountVectorizer |
| from sklearn.metrics.pairwise import cosine_similarity |
| import joblib |
| import re |
| import streamlit as st |
|
|
| |
| recipes_df = pd.read_csv('recipes.csv') |
|
|
| |
| recipes_df.drop(columns=["prep_time", "cook_time", "total_time", "yield", "rating", |
| "cuisine_path", "nutrition"], inplace=True) |
|
|
| |
| cv = CountVectorizer(stop_words='english', token_pattern=r'\b[A-Za-z]+\b') |
| ingredients_matrix = cv.fit_transform(recipes_df['ingredients']) |
|
|
| |
| cosine_sim = cosine_similarity(ingredients_matrix) |
|
|
| def get_recipe_recommendations(leftover_ingredients): |
| if leftover_ingredients is None: |
| return [] |
|
|
| |
| leftover_ingredients = ', '.join(re.findall(r'\b[A-Za-z]+\b', leftover_ingredients)) |
|
|
| |
| recipes_df['ingredients'] = recipes_df['ingredients'].apply( |
| lambda x: ', '.join(re.findall(r'\b[A-Za-z]+\b', x)) |
| ) |
|
|
| |
| ingredients_matrix = cv.transform(recipes_df['ingredients']) |
| cosine_similarities = cosine_similarity(ingredients_matrix, cv.transform([leftover_ingredients])) |
|
|
| |
| recipes_df['cosine_sim'] = cosine_similarities.flatten() |
|
|
| |
| sorted_df = recipes_df.sort_values('cosine_sim', ascending=False).reset_index() |
| sorted_df['rank'] = sorted_df.index + 1 |
| recommendations = sorted_df[['rank', 'recipe_name', 'cosine_sim', 'url', 'img_src']].head(10) |
|
|
| return recommendations.to_dict(orient='records') |
|
|
|
|
| st.title('Recipe Recommendation System') |
| st.text('Provide ingredients name by commas separated') |
| ingred = st.text_input( |
| 'Enter the leftover ingredients separated by commas: ') |
| if st.button('Recommend'): |
| recommendations = get_recipe_recommendations(ingred) |
|
|
| |
| df = pd.DataFrame(recommendations) |
|
|
| |
|
|
| |
| df['cosine_sim'] = df['cosine_sim'] * 100 |
| |
| |
| st.data_editor(df, column_config={ |
| 'rank': st.column_config.NumberColumn( |
| "Rank", format="%.0f"), |
| 'recipe_name': st.column_config.TextColumn( |
| "Recipe Name"), |
| 'cosine_sim': st.column_config.NumberColumn( |
| "Similarity", format="%.2f%%"), |
| 'img_src': st.column_config.ImageColumn( |
| "Preview Image"), |
| 'url': st.column_config.LinkColumn( |
| "Link", display_text="Open Recipe's link") |
| }) |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|