Laghs commited on
Commit
87f6064
·
verified ·
1 Parent(s): ab46851

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -0
app.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import gradio as gr
3
+ import re
4
+ import random
5
+ # Load the dataset
6
+ df = pd.read_csv('IndianFoodDatasetCSV.csv')
7
+
8
+ # Ensure the 'Ingredients' column is a string type
9
+ df['Ingredients'] = df['Ingredients'].astype(str)
10
+
11
+
12
+ def search_recipe(ingredient, diet):
13
+ # Convert ingredient to lowercase for case-insensitive search
14
+ ingredient = ingredient.lower()
15
+
16
+ # Filter the dataframe based on the ingredient and diet
17
+ filtered_df = df[df['Ingredients'].str.lower().str.contains(ingredient, na=False) &
18
+ (df['Diet'] == diet.title())]
19
+
20
+ if filtered_df.empty:
21
+ return "No matching recipes found."
22
+
23
+ # Get the first matching recipe
24
+ recipe = filtered_df.iloc[random.randint(0, len(filtered_df) - 1)]
25
+
26
+ # Construct the response
27
+ response = f"I found a recipe for you:\n\n"
28
+ response += f"Recipe: {recipe['RecipeName']}\n\n"
29
+ response += f"Ingredients: {recipe['Ingredients']}\n\n"
30
+ response += f"Instructions: {recipe['Instructions']}\n\n"
31
+ response += f"Diet: {recipe['Diet']}\n"
32
+ response += f"Course: {recipe['Course']}\n"
33
+ response += f"Prep Time: {recipe['PrepTimeInMins']}\n"
34
+ response += f"Cook Time: {recipe['CookTimeInMins']}\n"
35
+ response += f"Total Time: {recipe['TotalTimeInMins']}\n"
36
+ response += f"Servings: {recipe['Servings']}\n"
37
+ response += "\nWould you like to search any other recipe?"
38
+
39
+ return response
40
+
41
+
42
+ def chatbot(message, history):
43
+ # Check if the user is asking for a recipe
44
+
45
+ if re.search(r'\bbye\b', message, re.IGNORECASE):
46
+ return "Goodbye! Have a great day!"
47
+
48
+ recipe_request = re.search(r'recipe for (.*) that is (.*)', message, re.IGNORECASE)
49
+
50
+ if recipe_request:
51
+ ingredient = recipe_request.group(1).strip()
52
+ diet = recipe_request.group(2).strip()
53
+
54
+ # Validate diet input
55
+ valid_diets = ['Diabetic Friendly', 'Vegetarian', 'High Protein Vegetarian', 'Non Vegetarian', 'Vegan', 'Eggetarian']
56
+ valid_diets_lower = ['diabetic friendly', 'vegetarian', 'high protein vegetarian', 'non vegeterian', 'non vegetarian', 'vegan', 'eggetarian']
57
+ if diet.lower()=="non vegetarian":
58
+ diet= "non vegeterian"
59
+ if diet.strip().lower() not in valid_diets_lower:
60
+ return f"I'm sorry, but '{diet}' is not a valid diet option. Please choose from: {', '.join(valid_diets)}."
61
+
62
+ return search_recipe(ingredient, diet)
63
+ else:
64
+ 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'."
65
+
66
+
67
+ custom_css = """
68
+ body {
69
+ background-color: #000000;
70
+ font-family: Arial, sans-serif;
71
+ }
72
+
73
+ .gradio-container {
74
+ border: 2px solid #000080;
75
+ border-radius: 15px;
76
+ padding: 20px;
77
+ background-color: #ffffff;
78
+ }
79
+
80
+ .gradio-title {
81
+ color: #FF6347;
82
+ font-weight: bold;
83
+ text-align: center;
84
+ }
85
+
86
+ .gradio-description {
87
+ color: #0074AB;
88
+ text-align: center;
89
+ }
90
+
91
+ .gradio-textbox textarea {
92
+ border: 2px solid #000080;
93
+ border-radius: 10px;
94
+ padding: 10px;
95
+ }
96
+
97
+ .gradio-chatbox {
98
+ border: 2px solid #000080;
99
+ border-radius: 15px;
100
+ padding: 10px;
101
+ background-color: #E2E2FF;
102
+ }
103
+
104
+ .gradio-button {
105
+ background-color: #000080;
106
+ color: #FF6347;
107
+ border: none;
108
+ border-radius: 10px;
109
+ padding: 10px 20px;
110
+ cursor: pointer;
111
+ }
112
+
113
+ .gradio-button:hover {
114
+ background-color: #0000cd;
115
+ }
116
+ """
117
+ my_theme = gr.Theme.from_hub("gradio/seafoam")
118
+ iface = gr.ChatInterface(
119
+ chatbot,
120
+ chatbot=gr.Chatbot(height=600,default_message="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"),
121
+ textbox=gr.Textbox(placeholder="Ask for a recipe. For example: 'Recipe for tomato that is Vegetarian'",
122
+ container=False, scale=7),
123
+ title="Recipe Search Chatbot",
124
+ 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'",
125
+ theme="Monochrome",
126
+ examples=[
127
+ "Recipe for tomato that is Vegetarian",
128
+ "Recipe for chicken that is Non Vegeterian",
129
+ "Recipe for spinach that is High Protein Vegetarian",
130
+ "Recipe for oats that is Diabetic Friendly",
131
+
132
+ "Recipe for eggs that is Eggetarian"
133
+ ],
134
+ cache_examples=False,
135
+
136
+ )
137
+
138
+ def greet_user():
139
+ 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."
140
+
141
+
142
+ if __name__ == "__main__":
143
+ iface.launch()