Spaces:
Sleeping
Sleeping
File size: 7,297 Bytes
d922512 cf50ce0 1e1ad73 a273045 1e1ad73 a273045 1e1ad73 33933ea 1e1ad73 6dd2598 33933ea 1e1ad73 33933ea 1e1ad73 d1dc439 1e1ad73 cb057e8 1e1ad73 d1dc439 1e1ad73 cb057e8 fc0daf6 cb057e8 fc0daf6 6dd2598 1e1ad73 d1dc439 1e1ad73 d1dc439 eaf37da a273045 acdab73 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
import gradio as gr
from flask import Flask, request, jsonify
# Flask app setup
app = Flask(__name__)
# Corrected Food Database with consistent food item names
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"]
NONVEG_TYPES = ["Chicken", "Fish", "Mutton"]
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):
# Step 1: If initial step (asks if user wants to proceed)
if chat_state.step == "initial":
if "yes" in message.lower() or "yeah" in message.lower():
chat_state.step = "category"
return "Great! Please select your preferred category: Vegetarian, Non-Vegetarian."
return "I'm here to help you customize your food. Would you like to proceed? (Yes/No)"
# Step 2: Category selection
elif chat_state.step == "category":
chat_state.category = message.lower()
chat_state.step = "ingredient"
if "vegetarian" in message.lower():
return f"Great! What main ingredient would you like? Available options: {', '.join(VEG_INGREDIENTS)}"
elif "non-vegetarian" in message.lower():
return f"Great! What type of meat would you prefer? Available options: {', '.join(NONVEG_TYPES)}"
return "Please select either Vegetarian or Non-Vegetarian."
# Step 3: Ingredient selection
elif chat_state.step == "ingredient":
chat_state.ingredient = message.lower().strip() # Ensure no extra spaces or case issues
# Determine if the ingredient belongs to Veg or Non-Veg category
category = "nonveg" if "non" in chat_state.category else "veg"
# Check if the ingredient exists in the food database
if chat_state.ingredient not in FOOD_DATABASE[category]:
chat_state.step = "initial" # Reset for new conversation
return f"Sorry, I don't have any data for {message}. Please select another ingredient."
# List available food items based on ingredient
food_items = FOOD_DATABASE[category][chat_state.ingredient]
food_item_names = [item['name'] for nutrition in food_items for item in food_items[nutrition]]
# Show available food items and move to nutrition selection
chat_state.step = "nutrition"
return f"Great! Here are the available food items for {message}:\n{', '.join(food_item_names)}\nNow, select your nutrition preference: {', '.join(NUTRITION_OPTIONS)}"
# Step 4: Nutrition preference
elif chat_state.step == "nutrition":
nutrition = message.lower().replace(" ", "").strip() # Remove spaces and make lowercase
chat_state.nutrition = nutrition
category = "nonveg" if "non" in chat_state.category else "veg"
ingredient = chat_state.ingredient
# Try to fetch the food items from the correct category, ingredient, and nutrition preference
try:
food_items = FOOD_DATABASE[category][ingredient][nutrition]
if not food_items:
return f"Sorry, no {nutrition.replace('_', ' ').title()} dishes are available for {ingredient}."
response = f"Here are some {nutrition.replace('_', ' ').title()} {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"Sorry, I don't have any dishes for this combination."
return "I'm not sure how to help with that. Would you like to start over?"
# Gradio interface for the chatbot
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")
# Function for user message input and history handling
def user(user_message, history):
return "", history + [[user_message, None]]
# Function to handle bot's response
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
# Start the Gradio interface and Flask app together
if __name__ == "__main__":
demo = create_gradio_interface()
demo.launch(share=True, inline=True)
# Run Flask app
app.run(debug=True, use_reloader=False)
|