import numpy as np import pandas as pd import subprocess import gradio as gr subprocess.call(["pip", "install", "scikit-learn"]) from sklearn.feature_extraction.text import CountVectorizer from sklearn.metrics.pairwise import cosine_similarity def p_data(df): """Clean and preprocess the recipe dataset.""" df = df.drop_duplicates('name') df = df.dropna() df['name'] = df['name'].str.split('-').str[0] # Extract full recipe name instead of ingredients return df def feature_extraction(df): """Use scikit-learn's CountVectorizer to convert the ingredient text into numerical feature vectors.""" vectorizer = CountVectorizer() ingredient_vectors = vectorizer.fit_transform(df['ingredients']) return ingredient_vectors, vectorizer def calculate_ingredient_similarity(ingredient_vectors): """Calculate the cosine similarity between all the recipe ingredient vectors.""" similarity_matrix = cosine_similarity(ingredient_vectors) return similarity_matrix def recommend_recipes(similarity_matrix, input_ingredients, vectorizer, df): """Given a set of input ingredients, calculate the similarity between these ingredients and all the recipe ingredient vectors. Return the top-rated recipe as a suggestion.""" ingredient_vector = vectorizer.transform([input_ingredients]) similarity_scores = cosine_similarity(ingredient_vectors, ingredient_vector) similarity_scores = np.squeeze(similarity_scores) # Convert to 1-dimensional array top_recipe_index = np.argmax(similarity_scores) recommended_recipe = df.iloc[top_recipe_index] if similarity_scores[top_recipe_index] == 0: return "No recipes found." else: recipe_name = recommended_recipe['name'] recipe_ingredients = recommended_recipe['ingredients'] return f"Name: {recipe_name}\nIngredients: {recipe_ingredients}" if __name__ == '__main__': df = pd.read_csv('indian_food2.csv') df = p_data(df) ingredient_vectors, vectorizer = feature_extraction(df) similarity_matrix = calculate_ingredient_similarity(ingredient_vectors) def recipe_recommender(ingredients): recommended_recipe = recommend_recipes(similarity_matrix, ingredients, vectorizer, df) return recommended_recipe iface = gr.Interface( fn=recipe_recommender, inputs="text", outputs="text", live=True, examples=[ ["rice, chicken, tomato"] ] ) iface.launch()