import gradio as gr # Expanded recipe database with categories recipes = [ { "name": "Classic Frappuccino", "description": "A refreshing blended coffee drink topped with whipped cream", "ingredients": ["coffee", "milk", "ice cream", "sugar", "whipped cream"], "image": "https://images.unsplash.com/photo-1461023058943-07fcbe16d735?auto=format&fit=crop&q=80&w=1000", "category": "beverages" }, { "name": "Mocha Frappe", "description": "Rich chocolate and coffee blend topped with chocolate drizzle", "ingredients": ["coffee", "chocolate", "milk", "ice cream", "whipped cream"], "image": "https://images.unsplash.com/photo-1572490122747-3968b75cc699?auto=format&fit=crop&q=80&w=1000", "category": "beverages" }, { "name": "Cold Brew Coffee", "description": "Smooth, less acidic cold coffee perfect for hot days", "ingredients": ["coffee", "water", "ice"], "image": "https://images.unsplash.com/photo-1461023058943-07fcbe16d735?auto=format&fit=crop&q=80&w=1000", "category": "beverages" }, { "name": "Bellam Paysam", "description": "A traditional sweet rice pudding made with jaggery", "ingredients": ["rice", "milk", "jaggery", "cardamom", "cashews"], "image": "https://images.unsplash.com/photo-1633383718081-22ac93e3db65?auto=format&fit=crop&q=80&w=1000", "category": "desserts" }, { "name": "Rice Kheer", "description": "Creamy rice pudding with cardamom and nuts", "ingredients": ["rice", "milk", "sugar", "cardamom", "almonds", "raisins"], "image": "https://images.unsplash.com/photo-1639461161574-c11f0d2cca49?auto=format&fit=crop&q=80&w=1000", "category": "desserts" }, { "name": "Masala Dosa", "description": "Crispy fermented crepe filled with spiced potatoes", "ingredients": ["rice", "urad dal", "potatoes", "onions", "mustard seeds", "turmeric"], "image": "https://images.unsplash.com/photo-1589301760014-d929f3979dbc?auto=format&fit=crop&q=80&w=1000", "category": "main course" }, { "name": "Vegetable Biryani", "description": "Aromatic rice dish with mixed vegetables and spices", "ingredients": ["basmati rice", "mixed vegetables", "onions", "yogurt", "biryani masala", "saffron"], "image": "https://images.unsplash.com/photo-1563379091339-03b21ab4a4f8?auto=format&fit=crop&q=80&w=1000", "category": "main course" } ] # Function to find similar recipes based on user input def find_similar_recipes(input_text): ingredients = [i.strip().lower() for i in input_text.split('+')] scored_recipes = [] for recipe in recipes: matching_ingredients = [ri for ri in recipe['ingredients'] if any(i in ri.lower() for i in ingredients)] score = len(matching_ingredients) / len(ingredients) if score >= 0.3: scored_recipes.append({**recipe, "matchScore": round(score * 100)}) scored_recipes.sort(key=lambda x: x["matchScore"], reverse=True) return scored_recipes # Function to format recipes into a message for display def display_recipes(input_text): similar_recipes = find_similar_recipes(input_text) if similar_recipes: result = "✨ Here are some recipes you might love based on your ingredients:\n" result += "\n".join([f"{recipe['name']} ({recipe['matchScore']}% match)\n{recipe['description']}" for recipe in similar_recipes]) else: result = "I couldn't find any recipes with those exact ingredients. Try some alternatives!" return result # Gradio interface setup iface = gr.Interface( fn=display_recipes, inputs=gr.Textbox(label="Enter Ingredients (separated by '+')", placeholder="e.g., coffee + milk"), outputs=gr.Textbox(label="Recipes"), title="Recipe Assistant", description="Enter ingredients you have, and I will suggest some recipes for you!" ) if __name__ == "__main__": iface.launch()