Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import api.edamam_api as edamam_api | |
| from typing import List | |
| def make_string_list(input_text, separate=',') -> list: | |
| if input_text: | |
| input_list = input_text.split(separate) | |
| output_list = [string.strip() for string in input_list if string.strip()] | |
| return output_list | |
| else: | |
| return [] | |
| def search_recipes( | |
| edamam_app_id: str, | |
| edamam_app_key: str, | |
| calories_min: float, | |
| calories_max: float, | |
| protein_min_percent: float, | |
| protein_max_percent: float, | |
| carbs_min_percent: float, | |
| carbs_max_percent: float, | |
| fat_min_percent: float, | |
| fat_max_percent: float, | |
| included: str | None, | |
| excluded: str | None, | |
| sugar_min_g: float | None = None, | |
| sugar_max_g: float | None = None, | |
| sodium_min_mg: float | None = None, | |
| sodium_max_mg: float | None = None, | |
| saturated_fat_min_g: float | None = None, | |
| saturated_fat_max_g: float | None = None, | |
| fiber_min_g: float | None = None, | |
| fiber_max_g: float | None = None, | |
| cuisine_type: List[str] | None = None, | |
| meal_type: List[str] | None = None, | |
| nb_recipes: int = 1 | |
| ) -> str: | |
| """Compute macronutrients in grams from percent""" | |
| calories = calories_max | |
| calories_div_100_div_4 = calories / 100.0 / 4.0 | |
| calories_div_100_div_9 = calories / 100.0 / 9.0 | |
| protein_min_g = round(protein_min_percent * calories_div_100_div_4) | |
| protein_max_g = round(protein_max_percent * calories_div_100_div_4) | |
| carbs_min_g = round(carbs_min_percent * calories_div_100_div_4) | |
| carbs_max_g = round(carbs_max_percent * calories_div_100_div_4) | |
| fat_min_g = round(fat_min_percent * calories_div_100_div_9) | |
| fat_max_g = round(fat_max_percent * calories_div_100_div_9) | |
| recipes = edamam_api.get_recipes( | |
| edamam_app_id=edamam_app_id, | |
| edamam_app_key=edamam_app_key, | |
| calories_min=calories_min, | |
| calories_max=calories_max, | |
| protein_min_g=protein_min_g, | |
| protein_max_g=protein_max_g, | |
| carbs_min_g=carbs_min_g, | |
| carbs_max_g=carbs_max_g, | |
| fat_min_g=fat_min_g, | |
| fat_max_g=fat_max_g, | |
| included=included, | |
| excluded=make_string_list(excluded), | |
| added_sugar_min_g=sugar_min_g, | |
| added_sugar_max_g=sugar_max_g, | |
| sodium_min_mg=sodium_min_mg, | |
| sodium_max_mg=sodium_max_mg, | |
| saturated_fat_min_g=saturated_fat_min_g, | |
| saturated_fat_max_g=saturated_fat_max_g, | |
| fiber_min_g=fiber_min_g, | |
| fiber_max_g=fiber_max_g, | |
| cuisine_type=cuisine_type, | |
| meal_type=meal_type, | |
| nb_recipes=nb_recipes | |
| ) | |
| recipes_text = f"" | |
| for recipe in recipes: | |
| recipes_text += "\n" | |
| recipes_text += edamam_api.print2string_recipe(recipe) | |
| return recipes_text | |
| with gr.Blocks(title="Get Your Recipes") as demo: | |
| with gr.Row(): | |
| with gr.Column(): | |
| with gr.Row(): | |
| app_id = gr.Textbox(label="Edamam app id") | |
| app_key = gr.Textbox(label="Edamam app key") | |
| with gr.Row(): | |
| calories_min = gr.Number(value=500, label="Calories min") | |
| calories_max = gr.Number(value=700, label="Calories max") | |
| protein_min_percent = gr.Slider(minimum=0, maximum=100, value=10, step=1, label="Protein min (%)") | |
| protein_max_percent = gr.Slider(minimum=0, maximum=100, value=35, step=1, label="Protein max (%)") | |
| with gr.Row(): | |
| carbs_min_percent = gr.Slider(minimum=0, maximum=100, value=45, step=1, label="Carbs min (%)") | |
| carbs_max_percent = gr.Slider(minimum=0, maximum=100, value=65, step=1, label="Carbs max (%)") | |
| fat_min_percent = gr.Slider(minimum=0, maximum=100, value=10, step=1, label="Fat min (%)") | |
| fat_max_percent = gr.Slider(minimum=0, maximum=100, value=25, step=1, label="Fat max (%)") | |
| with gr.Row(): | |
| added_sugar_min_g = gr.Number(value=lambda: None, label="Added sugar min (g)") | |
| added_sugar_max_g = gr.Number(value=lambda: None, label="Added sugar max (g)") | |
| sodium_min_mg = gr.Number(value=lambda: None, label="Sodium min (mg)") | |
| sodium_max_mg = gr.Number(value=lambda: None, label="Sodium max (mg)") | |
| with gr.Row(): | |
| saturated_fat_min_g = gr.Number(value=lambda: None, label="Saturated fat min (g)") | |
| saturated_fat_max_g = gr.Number(value=lambda: None, label="Saturated fat max (g)") | |
| fiber_min_g = gr.Number(value=lambda: None, label="Fiber min (g)") | |
| fiber_max_g = gr.Number(value=lambda: None, label="Fiber max (g)") | |
| with gr.Row(): | |
| included = gr.Textbox(value="chicken", label="Included ingredients (enter items separated by commas)") | |
| excluded = gr.Textbox(value="sesame", label="Excluded ingredients (enter items separated by commas)") | |
| with gr.Row(): | |
| cuisine_type = gr.Dropdown(choices=edamam_api.CuisineTypeChoices, multiselect=True, label="Cuisine type") | |
| meal_type = gr.Dropdown(choices=edamam_api.MealTypeChoices, multiselect=True, label="Meal type") | |
| nb_recipes = gr.Number(value=1, label="Number of recipes") | |
| search_button = gr.Button("Search") | |
| result = gr.Textbox(label="Result", lines=24, max_lines=24, show_copy_button=True) | |
| search_button.click( | |
| fn=search_recipes, | |
| inputs=[ | |
| app_id, | |
| app_key, | |
| calories_min, | |
| calories_max, | |
| protein_min_percent, | |
| protein_max_percent, | |
| carbs_min_percent, | |
| carbs_max_percent, | |
| fat_min_percent, | |
| fat_max_percent, | |
| included, | |
| excluded, | |
| added_sugar_min_g, | |
| added_sugar_max_g, | |
| sodium_min_mg, | |
| sodium_max_mg, | |
| saturated_fat_min_g, | |
| saturated_fat_max_g, | |
| fiber_min_g, | |
| fiber_max_g, | |
| cuisine_type, | |
| meal_type, | |
| nb_recipes | |
| ], | |
| outputs=result) | |
| demo.launch() |