import os import requests import random import json import gradio as gr import openai as ai class Kokiku: def __init__(self): self.include_facts = True self.cooking_skill = ["Beginner", "Intermediate", "Advanced", "Expert"] self.cooking_time = ["< 30 minutes", "30 - 60 minutes", "> 60 minutes"] self.cuisine = ["Worldwide", "Korean", "Japanese", "Chinese", "Indonesian", "Indian", "Italian", "Mexican", "French", "American", "Thai", "Mediterranean", "Vietnamese", "Middle Eastern", "Spanish", "Greek", "German", "Nordic", "Caribbean", "British", "Irish", "Moroccan", "Russian", "Jewish", "Cajun", "Portuguese", "Hawaiian", "Hungarian", "Filipino", "Brazilian"] self.health_condition = ["None", "Weight Loss", "Diabetes", "High Cholesterol", "High Blood Pressure", "Kidney Disease", "Gout", "Anemia", "Celiac Disease", "GERD", "Crohn's Disease", "IBS", "Diverticulitis", "Pregnancy", "Lactation", "Gallstones", "Hepatitis", "HIV/AIDS", "Cancer", "PCOS"] self.intolerances = ["None", "Dairy", "Egg", "Gluten", "Grain", "Peanut", "Seafood", "Sesame", "Shellfish", "Soy", "Sulfite", "Tree Nut", "Wheat", "Mustard", "Celery", "Lupin", "Mollusk", "Alcohol", "Red Meat", "White Meat", "FODMAP"] self.eating_time = ["Breakfast", "Lunch", "Dinner", "Brunch"] self.plan_duration = ["1 day", "2 days", "3 days", "4 days", "5 days", "6 days", "1 week"] self.meals_per_day = ["1 meal", "2 meals", "3 meals"] self.servings = ["1 person", "2 people", "3 people", "4 people", "5 people", "6 people", "7 people", "8 people", "9 people", "10 people"] self.cooking_tools = ["Pan", "Oven", "Air Fryer", "Microwave", "Griller", "Steamer", "Rice Cooker", "Blender"] self.countries = ["South Korea", "United States", "Indonesia", "United Kingdom", "Singapore", "Japan", "Australia"] self.merchant = ["McDonalds", "Subway", "Starbucks", "KFC", "Pizza Hut", "Burger King", "Dunkin Donuts", "Domino's Pizza", "Pizza Hut", "KFC", "Krispy Kreme", "Papa's John", "Domino Pizza", "Taco Bell"] self.activity_level = ["Sedentary", "Lightly Active", "Moderately Active", "Very Active", "Extra Active"] self.age = 1 self.ingredients = [] self.include_shopping_list = True # Main Prompt self._main_prompt = "" self._health_condition_prompt = "" self._intolerances_prompt = "" self._include_facts_prompt = "" self._final_prompt = "" # Meal Planner Prompt self._meal_planner_prompt = "" self._meal_planner_intolerances_prompt = "" self._meal_planner_include_facts_prompt = "" self._meal_planner_shopping_list_prompt = "" self._meal_planner_final_prompt = "" # Order Recommendation Prompt self._order_recommendation_prompt = "" self._order_recommendation_health_condition_prompt = "" self._order_recommendation_intolerances_prompt = "" self._order_recommendation_include_facts_prompt = "" self._order_recommendation_shopping_list_prompt = "" self._order_recommendation_final_prompt = "" self._openai_key = os.getenv("OPENAI_KEY") # ai.api_key = self._openai_key def prompt_additional(self, ingredients, cuisine, cooking_skill, cooking_time, eating_time, servings, health_condition, intolerances, include_facts): # Check if the input is complete try: assert ingredients != None assert cuisine != None assert cooking_skill != None assert cooking_time != None assert eating_time != None assert servings != None assert health_condition != None assert intolerances != None # Preprocess the input cuisine = cuisine.lower() cooking_skill = cooking_skill.lower() cooking_time = cooking_time.lower() eating_time = eating_time.lower() servings = servings.lower() health_condition = health_condition.lower() intolerances = intolerances.lower() self._health_condition_prompt = f"Consider the {health_condition} health condition. " self._intolerances_prompt = f"Exclude {intolerances}. " self._include_facts_prompt = "Provide the nutritions fact for recipe's one portion. " if ingredients != "": punct = "," + ";" self.ingredients = "".join(ch for ch in ingredients if ch not in punct) assert self.ingredients != [], "Please input your ingredients first" self.ingredients = self.ingredients.lower() self._main_prompt = f"Give me 3 ideas on what to cook for {cuisine} dish {eating_time} using {self.ingredients} only with instructions and ingredients, for {servings} with {cooking_skill} cooking skill during {cooking_time} of cooking time. " else: return "","**Please input your available ingredients first!**" # Initialize the final prompt self._final_prompt = self._main_prompt if health_condition != "none": self._final_prompt += self._health_condition_prompt if intolerances != "none": self._final_prompt += self._intolerances_prompt if include_facts: self._final_prompt += self._include_facts_prompt self._final_prompt += " Give the condensed yet detailed answer" # response = ai.ChatCompletion.create( # model="gpt-3.5-turbo-0613", # temperature=0.9, # messages = [{"role": "user", "content": "What's the weather like in Boston?"}] # ) # print(response.json()) return "", self._final_prompt except AssertionError: return "","**Please complete the information first!**" def prompt_meal_plan(self, ingredients, plan_duration, meals_per_day, cuisine, cooking_skill, cooking_time, intolerances, include_facts, shopping_list, servings): # Check if the input is complete try: assert ingredients != None assert cuisine != None assert cooking_skill != None assert cooking_time != None assert servings != None assert intolerances != None # Preprocess the input plan_duration = plan_duration.lower() meals_per_day = meals_per_day.lower() cuisine = cuisine.lower() cooking_skill = cooking_skill.lower() cooking_time = cooking_time.lower() intolerances = intolerances.lower() self._meal_planner_intolerances_prompt = f"Exclude {intolerances}. " self._meal_planner_include_facts_prompt = "Provide the nutritions fact for recipe's one portion. " self._meal_planner_shopping_list_prompt = f"Make a shopping list for {servings} according to it. " if ingredients != "": punct = "," + ";" self.ingredients = "".join(ch for ch in ingredients if ch not in punct) assert self.ingredients != [], "Please input your ingredients first" self.ingredients = self.ingredients.lower() self._meal_planner_prompt = f"Make {plan_duration} meal plan ({meals_per_day}/day) consist of {cuisine} food using {self.ingredients}, which can be cooked with {cooking_skill} skill during {cooking_time} of cooking time. " else: return "","**Please input the ingredients first!**" # Initialize the final prompt self._final_prompt = self._meal_planner_prompt if intolerances != "none": self._meal_planner_final_prompt += self._meal_planner_intolerances_prompt if include_facts: self._meal_planner_final_prompt += self._meal_planner_shopping_list_prompt if shopping_list: self._meal_planner_final_prompt += self._meal_planner_shopping_list_prompt self._final_prompt += " Give the condensed yet detailed answer" # response = ai.ChatCompletion.create( # model="gpt-3.5-turbo-0613", # temperature=0.9, # messages = [{"role": "user", "content": "What's the weather like in Boston?"}] # ) # print(response.json()) return "", self._final_prompt except AssertionError: return "","**Please complete the information first!**" def prompt_order_recommendation(self, eating_time, merchant, countries, activity_level, ingredients, age, intolerances): # Check if the input is complete try: assert ingredients != None assert merchant != None assert countries != None assert activity_level != None assert intolerances != None assert age != None assert eating_time != None # Preprocess the input eating_time = eating_time.lower() merchant = merchant.lower() countries = countries.lower() activity_level = activity_level.lower() intolerances = intolerances.lower() self._order_recommendation_intolerances_prompt = f"Exclude {intolerances}. " if ingredients != "": punct = "," + ";" self.ingredients = "".join(ch for ch in ingredients if ch not in punct) assert self.ingredients != [], "Please input your ingredients first" self.ingredients = self.ingredients.lower() self._order_recommendation_prompt = f"Suggest me what to order for {eating_time} at {merchant} {countries}, which contain {ingredients} " else: return "","**Please input the ingredients first!**" if age.isnumeric(): age = int(age) if self.is_integer(age) and age > 0 and age < 105: self._order_recommendation_prompt += f"and enough nutrient for age {age} with {activity_level} activity level. " else: return "","**Please input the age first within the range (1 <= age <= 105 years)!**" else: return "","**Please input the age first with the correct value!**" # Initialize the final prompt self._final_prompt = self._order_recommendation_prompt if intolerances != "none": self._meal_planner_final_prompt += self._meal_planner_intolerances_prompt self._final_prompt += " Give the condensed yet detailed answer" # response = ai.ChatCompletion.create( # model="gpt-3.5-turbo-0613", # temperature=0.9, # messages = [{"role": "user", "content": "What's the weather like in Boston?"}] # ) # print(response.json()) return "", self._final_prompt except AssertionError: return "","**Please complete the information first!**" def generate_recipe(self, cuisine, cooking_skill, cooking_time, eating_time, servings, health_condition, intolerances, include_facts): interface = gr.Interface(self.prompt_additional, css="footer{display:none !important}", show_label=True, theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange"), inputs=[gr.Textbox(input="text", label="Ingredients", info="What's inside your fridge?", placeholder="Chicken, Goguma, Chili etc"), gr.Dropdown(cuisine, value=cuisine[0], label="Cuisine", info="What Cuisine that you crave now?"), gr.Radio(cooking_skill, value=cooking_skill[0], label="Cooking Skill", info="What is your cooking skill?"), gr.Radio(cooking_time, value=cooking_time[0], label="Cooking Time", info="How much time do you have to cook?"), gr.Radio(eating_time, value=eating_time[0], label="Eating Time", info="When do you want to eat?"), gr.Dropdown(servings, value=servings[0], label="Servings", info="How many people will eat?"), gr.Dropdown(health_condition, value=health_condition[0], label="Health Condition", info="Is there any special health condition?"), gr.Dropdown(intolerances, value=intolerances[0], label="Intolerances", info="Is there any intolerances?"), gr.Checkbox(include_facts, label="Include Nutrition Facts?")], outputs=[gr.Radio(label="Kokiku's Recipe", placeholder="Complete the information to see our brilliant and tasty recipe recommendations", default="Complete the information to see our brilliant and tasty recipe recommendations", readonly=True, info="Our recipe recommendation will be shown here!"), gr.Markdown()]) return interface def generate_meal_plan(self, plan_duration, meals_per_day, cuisine, cooking_skill, cooking_time, intolerances, servings, include_shopping_list, include_facts): interface = gr.Interface(self.prompt_meal_plan, css="footer{display:none !important}", show_label=True, theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange"), inputs=[gr.Textbox(input="text", label="Ingredients", info="What ingredients you want to use?", placeholder="Chicken, Goguma, Chili etc"), gr.Dropdown(plan_duration, value=plan_duration[0], label="Plan Duration", info="How long is the plan?"), gr.Dropdown(meals_per_day, value=meals_per_day[0], label="Meals per Day", info="How many times a day do you eat?"), gr.Dropdown(cuisine, value=cuisine[0], label="Cuisine", info="What Cuisine that you crave now?"), gr.Radio(cooking_skill, value=cooking_skill[0], label="Cooking Skill", info="What is your cooking skill?"), gr.Radio(cooking_time, value=cooking_time[0], label="Cooking Time", info="How much time do you have to cook?"), gr.Dropdown(intolerances, value=intolerances[0], label="Intolerances", info="Is there any intolerances?"), gr.Dropdown(servings, value=servings[0], label="Servings", info="How many people will eat?"), gr.Checkbox(include_shopping_list, label="Include the shopping lists for your plan?"), gr.Checkbox(include_facts, label="Include Nutrition Facts?")], outputs=[gr.Radio(label="Kokiku's Meal Plan", placeholder="Complete the information to see our meal plans recommendations", default="Complete the information to see our meal plans recommendations", readonly=True, info="Our meal plans recommendation will be shown here!"), gr.Markdown()]) return interface def generate_order_recommendaton(self, eating_time, merchant, countries, activity_level, intolerances): interface = gr.Interface(self.prompt_order_recommendation, css="footer{display:none !important}", show_label=True, theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange"), inputs=[gr.Radio(eating_time, value=eating_time[0], label="Eating Time", info="When do you want to eat?"), gr.Dropdown(merchant, value=merchant[0], label="Merchant", info="Where are you going to eat?"), gr.Dropdown(countries, value=countries[0], label="Country", info="Which country?"), gr.Dropdown(activity_level, value=activity_level[0], label="Activty Level", info="How active are you?"), gr.Textbox(input="text", label="Ingredients", info="What ingredients to include in your food?", placeholder="Beef, Celery, Meatballs"), gr.Textbox(input="text", label="Age", info="How old are you?", placeholder="20 (numbers only)"), gr.Dropdown(intolerances, value=intolerances[0], label="Intolerances", info="Is there any intolerances?")], outputs=[gr.Radio(label="Kokiku's order suggestion", placeholder="Complete the information to see our order suggestions", default="Complete the information to see our order suggestions", readonly=True, info="Our food order recommendations will be shown here!"), gr.Markdown()]) return interface @staticmethod def is_integer(n): try: float(n) except ValueError: return False else: return float(n).is_integer() def main(): kokiku = Kokiku() app_recipe = kokiku.generate_recipe(kokiku.cuisine, kokiku.cooking_skill, kokiku.cooking_time, kokiku.eating_time, kokiku.servings, kokiku.health_condition, kokiku.intolerances, kokiku.include_facts) app_plan = kokiku.generate_meal_plan(kokiku.plan_duration, kokiku.meals_per_day, kokiku.cuisine, kokiku.cooking_skill, kokiku.cooking_time, kokiku.intolerances, kokiku.servings, kokiku.include_shopping_list, kokiku.include_facts) app_order = kokiku.generate_order_recommendaton(kokiku.eating_time, kokiku.merchant, kokiku.countries, kokiku.activity_level, kokiku.intolerances) app_entire = gr.TabbedInterface([app_recipe, app_plan, app_order], tab_names=["Recipe Maker", "Meal Planner", "What to Order"], title="Kokiku", theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange"), css="footer{display:none !important}") app_entire.launch(favicon_path="/assets/chef.ico", debug=False) if __name__ == "__main__": main()