Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| # Food database (same as your provided code) | |
| FOOD_DATABASE = { | |
| "veg": { | |
| "paneer": { | |
| "lowCalorie": [ | |
| {"name": "Grilled Paneer Tikka π’", "description": "Marinated and grilled paneer cubes with spices", "calories": 180, "protein": 14}, | |
| {"name": "Paneer Bhurji π³", "description": "Scrambled paneer with vegetables", "calories": 200, "protein": 16} | |
| ], | |
| "highProtein": [ | |
| {"name": "Protein Paneer Bowl π₯", "description": "High protein paneer with quinoa", "calories": 320, "protein": 24}, | |
| {"name": "Paneer Steak π₯©", "description": "Thick cut paneer steak with herbs", "calories": 280, "protein": 22} | |
| ] | |
| }, | |
| "mushroom": { | |
| "lowCalorie": [ | |
| {"name": "Grilled Mushroom Caps π", "description": "Herb-stuffed mushroom caps", "calories": 120, "protein": 8}, | |
| {"name": "Mushroom Soup π²", "description": "Creamy mushroom soup", "calories": 150, "protein": 6} | |
| ] | |
| }, | |
| "aloo": { | |
| "lowCalorie": [ | |
| {"name": "Baked Potato Wedges π", "description": "Spiced and baked potato wedges", "calories": 160, "protein": 4}, | |
| {"name": "Aloo Tikki π₯", "description": "Spiced potato patties", "calories": 180, "protein": 3} | |
| ] | |
| } | |
| }, | |
| "nonveg": { | |
| "chicken": { | |
| "highProtein": [ | |
| {"name": "Grilled Chicken Breast π", "description": "Herb-marinated grilled chicken", "calories": 250, "protein": 30}, | |
| {"name": "Chicken Tikka π", "description": "Tandoori spiced chicken pieces", "calories": 280, "protein": 32} | |
| ] | |
| }, | |
| "fish": { | |
| "lowCalorie": [ | |
| {"name": "Grilled Salmon π", "description": "Lemon herb grilled salmon", "calories": 220, "protein": 25}, | |
| {"name": "Steamed Fish π ", "description": "Ginger-garlic steamed fish", "calories": 180, "protein": 22} | |
| ] | |
| }, | |
| "mutton": { | |
| "balanced": [ | |
| {"name": "Mutton Curry π", "description": "Traditional spiced mutton curry", "calories": 350, "protein": 28}, | |
| {"name": "Grilled Lamb Chops π", "description": "Herb-crusted lamb chops", "calories": 380, "protein": 32} | |
| ] | |
| } | |
| } | |
| } | |
| VEG_INGREDIENTS = ["Paneer π§", "Mushroom π", "Aloo π₯", "Brinjal π", "Palak πΏ"] | |
| NONVEG_TYPES = ["Chicken π", "Fish π", "Mutton π", "Pork π·"] | |
| NUTRITION_OPTIONS = ["Low Calorie π", "High Protein πͺ", "Low Carb π₯", "Balanced π₯", "Gluten Free π«π"] | |
| class ChatState: | |
| def __init__(self): | |
| self.step = "initial" | |
| self.category = "" | |
| self.ingredient = "" | |
| self.nutrition = "" | |
| chat_state = ChatState() | |
| def process_message(message, history): | |
| if chat_state.step == "initial": | |
| if "yes" in message.lower() or "yeah" in message.lower(): | |
| chat_state.step = "category" | |
| return "Welcome to Chef Bot! π¨βπ³ Let's create your perfect meal! Select your food type: Vegeterian π± or Non-Vegetarian π." | |
| return "Let's get started! Do you want to proceed with meal customization? (Yes/No)" | |
| elif chat_state.step == "category": | |
| chat_state.category = message.lower() | |
| chat_state.step = "ingredient" | |
| if "vegetarian" in message.lower(): | |
| return f"Great choice! π± Vegetarian food is healthy and environmentally friendly. What main ingredient would you like? Available options: {', '.join(VEG_INGREDIENTS)}" | |
| elif "non-vegetarian" in message.lower(): | |
| return f"Great choice! π Non-vegetarian food is delicious! What type of meat would you prefer? Available options: {', '.join(NONVEG_TYPES)}" | |
| return "Please choose either Vegetarian π± or Non-Vegetarian π" | |
| elif chat_state.step == "ingredient": | |
| chat_state.ingredient = message.lower() | |
| chat_state.step = "nutrition" | |
| return f"Perfect! β¨ Now, select your nutrition preference: {', '.join(NUTRITION_OPTIONS)}" | |
| elif chat_state.step == "nutrition": | |
| nutrition = message.lower().replace(" ", "") | |
| category = "nonveg" if "non" in chat_state.category else "veg" | |
| ingredient = chat_state.ingredient | |
| try: | |
| food_items = FOOD_DATABASE[category][ingredient][nutrition] | |
| response = f"Here are some {message} {ingredient} dishes for you π½οΈ:\n" | |
| for item in food_items: | |
| response += f"\nβ’ {item['name']} ({item['calories']} cal, {item['protein']}g protein)\n {item['description']}" | |
| chat_state.step = "initial" # Reset for new conversation | |
| return response | |
| except KeyError: | |
| chat_state.step = "initial" # Reset for new conversation | |
| return f"Oops! No dishes found for this combination π . Let's try again!" | |
| return "I'm not sure how to help with that. Would you like to start over? π€" | |
| def create_gradio_interface(): | |
| with gr.Blocks() as demo: | |
| chatbot = gr.Chatbot([]) | |
| msg = gr.Textbox(show_label=False, placeholder="Enter text and press enter") | |
| clear = gr.Button("Clear π") | |
| def user(user_message, history): | |
| return "", history + [[user_message, None]] | |
| def bot(history): | |
| user_message = history[-1][0] | |
| bot_message = process_message(user_message, history) | |
| history[-1][1] = bot_message | |
| return history | |
| msg.submit(user, [msg, chatbot], [msg, chatbot]).then(bot, chatbot, chatbot) | |
| clear.click(lambda: None, None, chatbot) | |
| return demo | |
| # Launch the Gradio app | |
| if __name__ == "__main__": | |
| demo = create_gradio_interface() | |
| demo.launch(share=True) | |