Spaces:
Sleeping
Sleeping
File size: 6,675 Bytes
87ccbe3 |
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 |
import os
import requests
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.servings = ["1 person", "2 people", "3 people", "4 people", "5 people", "6 people", "7 people", "8 people", "9 people", "10 people"]
self.ingredients = []
self._main_prompt = ""
self._health_condition_prompt = ""
self._intolerances_prompt = ""
self._include_facts_prompt = ""
self._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, 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
# 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}", title="Kokiku", 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 main():
kokiku = Kokiku()
app = 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.launch(favicon_path="/assets/chef.ico", debug=False, share=True)
if __name__ == "__main__":
main()
|