ChefBotRamsay / app.py
Laghs's picture
Update app.py
ede9a8c verified
Raw
History Blame Contribute Delete
3.69 kB
import pandas as pd
import gradio as gr
import re
import random
# Load the dataset
df = pd.read_csv('IndianFoodDatasetCSV.csv')
# Ensure the 'Ingredients' column is a string type
df['Ingredients'] = df['Ingredients'].astype(str)
def search_recipe(ingredient, diet):
# Convert ingredient to lowercase for case-insensitive search
ingredient = ingredient.lower()
# Filter the dataframe based on the ingredient and diet
filtered_df = df[df['Ingredients'].str.lower().str.contains(ingredient, na=False) &
(df['Diet'] == diet.title())]
if filtered_df.empty:
return "No matching recipes found."
# Get the first matching recipe
recipe = filtered_df.iloc[random.randint(0, len(filtered_df) - 1)]
# Construct the response
response = f"I found a recipe for you:\n\n"
response += f"Recipe: {recipe['RecipeName']}\n\n"
response += f"Ingredients: {recipe['Ingredients']}\n\n"
response += f"Instructions: {recipe['Instructions']}\n\n"
response += f"Diet: {recipe['Diet']}\n"
response += f"Course: {recipe['Course']}\n"
response += f"Prep Time: {recipe['PrepTimeInMins']}\n"
response += f"Cook Time: {recipe['CookTimeInMins']}\n"
response += f"Total Time: {recipe['TotalTimeInMins']}\n"
response += f"Servings: {recipe['Servings']}\n"
response += "\nWould you like to search any other recipe?"
return response
def chatbot(message, history):
# Check if the user is asking for a recipe
if re.search(r'\bbye\b', message, re.IGNORECASE):
return "Goodbye! Have a great day!"
recipe_request = re.search(r'recipe for (.*) that is (.*)', message, re.IGNORECASE)
if recipe_request:
ingredient = recipe_request.group(1).strip()
diet = recipe_request.group(2).strip()
# Validate diet input
valid_diets = ['Diabetic Friendly', 'Vegetarian', 'High Protein Vegetarian', 'Non Vegetarian', 'Vegan', 'Eggetarian']
valid_diets_lower = ['diabetic friendly', 'vegetarian', 'high protein vegetarian', 'non vegeterian', 'non vegetarian', 'vegan', 'eggetarian']
if diet.lower()=="non vegetarian":
diet= "non vegeterian"
if diet.strip().lower() not in valid_diets_lower:
return f"I'm sorry, but '{diet}' is not a valid diet option. Please choose from: {', '.join(valid_diets)}."
return search_recipe(ingredient, diet)
else:
return "I'm sorry, I didn't understand that. Please ask for a recipe in this format: 'Recipe for [ingredient] that is [diet]'. For example: 'Recipe for tomato that is Vegetarian'."
my_theme = gr.Theme.from_hub("ParityError/Anime")
iface = gr.ChatInterface(
chatbot,
chatbot=gr.Chatbot(height=600),
textbox=gr.Textbox(placeholder="Ask for a recipe. For example: 'Recipe for tomato that is Vegetarian'",
container=False, scale=7),
title="ChefBot Ramsay",
description="Hello! I can help you cook something amazing today. Just give me the ingredient name with diet. For example, 'Recipe for tomato that is Vegetarian'",
theme=my_theme,
examples=[
"Recipe for tomato that is Vegetarian",
"Recipe for chicken that is Non Vegeterian",
"Recipe for spinach that is High Protein Vegetarian",
"Recipe for oats that is Diabetic Friendly",
"Recipe for eggs that is Eggetarian"
],
cache_examples=False,
)
def greet_user():
return "Hello! I can help you cook something amazing today. Just give me the ingredient name with diet. For example, 'Recipe for tomato that is Vegetarian'. I will give you the yummiest recipe."
if __name__ == "__main__":
iface.launch()