Dieu-Sang commited on
Commit
c2584b2
·
1 Parent(s): c1bdb4c

fix edamam api

Browse files
Files changed (3) hide show
  1. api/edamam_api.py +67 -0
  2. api/spoonacular_api.py +13 -15
  3. app.py +20 -26
api/edamam_api.py CHANGED
@@ -20,6 +20,73 @@ from typing import List
20
  # my_edamam_app_id = read_json_to_dict(file_path=f"{CURRENT_DIR}/api_key.json")['edamam_app_id']
21
  # my_edamam_app_key = read_json_to_dict(file_path=f"{CURRENT_DIR}/api_key.json")['edamam_app_key']
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  @dataclass
24
  class Recipe:
25
  name: str
 
20
  # my_edamam_app_id = read_json_to_dict(file_path=f"{CURRENT_DIR}/api_key.json")['edamam_app_id']
21
  # my_edamam_app_key = read_json_to_dict(file_path=f"{CURRENT_DIR}/api_key.json")['edamam_app_key']
22
 
23
+ DietChoices = [
24
+ None,
25
+ "balanced",
26
+ "high-fiber",
27
+ "high-protein",
28
+ "low-carb",
29
+ "low-fat",
30
+ "low-sodium"]
31
+ HealthChoices = [
32
+ None,
33
+ "alcohol-cocktail",
34
+ "alcohol-free",
35
+ "celery-free",
36
+ "crustacean-free",
37
+ "dairy-free",
38
+ "DASH",
39
+ "egg-free",
40
+ "fish-free",
41
+ "fodmap-free",
42
+ "gluten-free",
43
+ "immuno-supportive",
44
+ "keto-friendly",
45
+ "kidney-friendly",
46
+ "kosher",
47
+ "low-fat-abs",
48
+ "low-potassium",
49
+ "low-sugar",
50
+ "lupine-free",
51
+ "Mediterranean",
52
+ "mollusk-free",
53
+ "mustard-free",
54
+ "no-oil-added",
55
+ "paleo",
56
+ "peanut-free",
57
+ "pescatarian",
58
+ "pork-free",
59
+ "red-meat-free",
60
+ "sesame-free",
61
+ "shellfish-free",
62
+ "soy-free",
63
+ "sugar-conscious",
64
+ "sulfite-free",
65
+ "tree-nut-free",
66
+ "vegan",
67
+ "vegetarian",
68
+ "wheat-free"]
69
+ CuisineTypeChoices = [
70
+ None,
71
+ "American",
72
+ "Asian",
73
+ "British",
74
+ "Caribbean",
75
+ "Central Europe",
76
+ "Chinese",
77
+ "Eastern Europe",
78
+ "French",
79
+ "Indian",
80
+ "Italian",
81
+ "Japanese",
82
+ "Kosher",
83
+ "Mediterranean",
84
+ "Mexican",
85
+ "Middle Eastern",
86
+ "Nordic",
87
+ "South American",
88
+ "South East Asian"]
89
+
90
  @dataclass
91
  class Recipe:
92
  name: str
api/spoonacular_api.py CHANGED
@@ -2,22 +2,20 @@ import requests
2
  import json
3
  import os
4
 
5
- CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
6
-
7
- """Read key json file"""
8
- def read_json_to_dict(file_path) -> dict:
9
- try:
10
- with open(file_path, 'r') as file:
11
- data = json.load(file)
12
- return data
13
- except FileNotFoundError:
14
- print("File not found at the specified path.")
15
- return {}
16
- except json.JSONDecodeError:
17
- print("Error decoding JSON file.")
18
- return {}
19
  # my_spoonacular_api_key = read_json_to_dict(file_path=f"{CURRENT_DIR}/api_key.json")['spoonacular_api_key']
20
- my_spoonacular_api_key = ""
21
 
22
  def get_recipes_by_macronutrients(spoonacular_api_key: str, calories: int, protein_grams: int, carbs_grams: int, fat_grams: int, nb_recipes: int) -> list:
23
  if not spoonacular_api_key:
 
2
  import json
3
  import os
4
 
5
+ """Read private api key from json file"""
6
+ # CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
7
+ # def read_json_to_dict(file_path) -> dict:
8
+ # try:
9
+ # with open(file_path, 'r') as file:
10
+ # data = json.load(file)
11
+ # return data
12
+ # except FileNotFoundError:
13
+ # print("File not found at the specified path.")
14
+ # return {}
15
+ # except json.JSONDecodeError:
16
+ # print("Error decoding JSON file.")
17
+ # return {}
 
18
  # my_spoonacular_api_key = read_json_to_dict(file_path=f"{CURRENT_DIR}/api_key.json")['spoonacular_api_key']
 
19
 
20
  def get_recipes_by_macronutrients(spoonacular_api_key: str, calories: int, protein_grams: int, carbs_grams: int, fat_grams: int, nb_recipes: int) -> list:
21
  if not spoonacular_api_key:
app.py CHANGED
@@ -1,9 +1,10 @@
1
  import gradio as gr
2
  import api.edamam_api as edamam_api
 
3
 
4
- def separate_text_to_string_list(input_text) -> list:
5
  if input_text:
6
- input_list = input_text.split(',')
7
  output_list = [string.strip() for string in input_list if string.strip()]
8
  return output_list
9
  else:
@@ -16,9 +17,9 @@ def search_recipes(
16
  protein_calories: int,
17
  carbs_calories: int,
18
  fat_calories: int,
19
- diet: str | None,
20
- health: str | None,
21
- cuisine_type: str | None,
22
  excluded: str | None,
23
  nb_recipes: int
24
  ) -> str:
@@ -29,10 +30,10 @@ def search_recipes(
29
  protein_calories=protein_calories,
30
  carbs_calories=carbs_calories,
31
  fat_calories=fat_calories,
32
- diet=separate_text_to_string_list(diet),
33
- health=separate_text_to_string_list(health),
34
- cuisine_type=separate_text_to_string_list(cuisine_type),
35
- excluded=separate_text_to_string_list(excluded),
36
  nb_recipes=nb_recipes
37
  )
38
 
@@ -41,20 +42,21 @@ def search_recipes(
41
  recipes_text += "\n"
42
  recipes_text += edamam_api.print2string_recipe(recipe)
43
  return recipes_text
44
-
45
  inputs = [
46
  gr.Textbox(label="Edamam app id"),
47
  gr.Textbox(label="Edamam app key"),
48
 
49
- gr.Number(value=600, label="Calorie need (+/-50cal)"),
50
  gr.Number(value=150, label="Protein need in calories"),
51
  gr.Number(value=300, label="Carbs need in calories"),
52
  gr.Number(value=150, label="Fat need in calories"),
53
 
54
- gr.Textbox(value="", label="Diet (enter items separated by commas)"),
55
- gr.Textbox(value="", label="Health (enter items separated by commas)"),
56
- gr.Textbox(value="Asian", label="Cuisine type (enter items separated by commas)"),
57
- gr.Textbox(value="noodles", label="Excluded (enter items separated by commas)"),
 
58
 
59
  gr.Number(value=20, label="Number of recipes"),
60
 
@@ -62,15 +64,7 @@ inputs = [
62
 
63
  output = gr.Textbox(label="Result")
64
 
65
- title = "GET YOUR RECIPES"
66
- description = "Let's find the best recipes adapted to your calorie, protein, carbs, and fat needs"
67
- examples = [["your-edamam-app-id",
68
- "your-edamam-app-key",
69
- "600", "150", "300", "150",
70
- "balanced, high-fiber, high-protein, low-carb, low-fat, low-sodium",
71
- "alcohol-cocktail, alcohol-free, celery-free, crustacean-free, dairy-free",
72
- "American, Asian, British, Caribbean, Central Europe",
73
- "rice, egg"
74
- ]]
75
 
76
- gr.Interface(search_recipes, inputs, output, title=title, description=description, examples=examples).launch(share=False)
 
1
  import gradio as gr
2
  import api.edamam_api as edamam_api
3
+ from typing import List
4
 
5
+ def make_string_list(input_text, separate=',') -> list:
6
  if input_text:
7
+ input_list = input_text.split(separate)
8
  output_list = [string.strip() for string in input_list if string.strip()]
9
  return output_list
10
  else:
 
17
  protein_calories: int,
18
  carbs_calories: int,
19
  fat_calories: int,
20
+ diet: List[str] | None,
21
+ health: List[str] | None,
22
+ cuisine_type: List[str] | None,
23
  excluded: str | None,
24
  nb_recipes: int
25
  ) -> str:
 
30
  protein_calories=protein_calories,
31
  carbs_calories=carbs_calories,
32
  fat_calories=fat_calories,
33
+ diet=diet,
34
+ health=health,
35
+ cuisine_type=cuisine_type,
36
+ excluded=make_string_list(excluded),
37
  nb_recipes=nb_recipes
38
  )
39
 
 
42
  recipes_text += "\n"
43
  recipes_text += edamam_api.print2string_recipe(recipe)
44
  return recipes_text
45
+
46
  inputs = [
47
  gr.Textbox(label="Edamam app id"),
48
  gr.Textbox(label="Edamam app key"),
49
 
50
+ gr.Number(value=600, label="Calorie need"),
51
  gr.Number(value=150, label="Protein need in calories"),
52
  gr.Number(value=300, label="Carbs need in calories"),
53
  gr.Number(value=150, label="Fat need in calories"),
54
 
55
+ gr.Dropdown(choices=edamam_api.DietChoices, multiselect=True, label="Diet"),
56
+ gr.Dropdown(choices=edamam_api.HealthChoices, multiselect=True, label="Health"),
57
+ gr.Dropdown(choices=edamam_api.CuisineTypeChoices, multiselect=True, label="Cuisine type"),
58
+
59
+ gr.Textbox(value="noodles", label="Excluded (enter items separated by commas)", elem_id="textbox_id"),
60
 
61
  gr.Number(value=20, label="Number of recipes"),
62
 
 
64
 
65
  output = gr.Textbox(label="Result")
66
 
67
+ title = "Get Your Recipes"
68
+ description = "Let's find the best recipes adapted to your needs"
 
 
 
 
 
 
 
 
69
 
70
+ gr.Interface(search_recipes, inputs, output, title=title, description=description, css="#textbox_id textarea {color: red}").launch(share=False)