File size: 3,692 Bytes
87f6064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bcf364
ede9a8c
87f6064
 
3bbebd5
87f6064
 
ad1438d
87f6064
6aa4ddc
87f6064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()