irins commited on
Commit
1314ff5
·
verified ·
1 Parent(s): a936f65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -8
app.py CHANGED
@@ -22,24 +22,45 @@ def translate_text_hf(text):
22
 
23
  # Recipe search function using Spoonacular API
24
  def get_recipes(ingredients, max_calories=None, min_protein=None, max_fat=None, max_carbs=None):
 
25
  translated_ingredients = translate_text_hf(ingredients)
26
  url = "https://api.spoonacular.com/recipes/complexSearch"
 
 
27
  params = {
28
  "query": translated_ingredients,
29
  "apiKey": spoonacular_api_key,
30
- "maxCalories": max_calories,
31
- "minProtein": min_protein,
32
- "maxFat": max_fat,
33
- "maxCarbs": max_carbs,
34
- "number": 5
35
  }
36
- print("Request Params:", params) # Debugging: print request parameters
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  response = requests.get(url, params=params)
38
- print("Response Status:", response.status_code) # Debugging: print response status
 
 
 
 
39
  if response.status_code == 200:
40
- print("API Response:", response.json()) # Debugging: print raw response
 
 
 
41
  recipes = response.json().get('results', [])
42
  if recipes:
 
43
  return "\n".join(
44
  [f"Recipe: {recipe['title']} (Link: https://spoonacular.com/recipes/{recipe['id']})"
45
  for recipe in recipes]
@@ -47,6 +68,7 @@ def get_recipes(ingredients, max_calories=None, min_protein=None, max_fat=None,
47
  else:
48
  return "No suitable recipes found."
49
  else:
 
50
  return f"Error fetching recipes: {response.status_code}"
51
 
52
  # Gradio Interface
 
22
 
23
  # Recipe search function using Spoonacular API
24
  def get_recipes(ingredients, max_calories=None, min_protein=None, max_fat=None, max_carbs=None):
25
+ # Translate ingredients to English
26
  translated_ingredients = translate_text_hf(ingredients)
27
  url = "https://api.spoonacular.com/recipes/complexSearch"
28
+
29
+ # Base parameters for the API request
30
  params = {
31
  "query": translated_ingredients,
32
  "apiKey": spoonacular_api_key,
33
+ "number": 5 # Return up to 5 recipes
 
 
 
 
34
  }
35
+
36
+ # Add optional parameters only if they are provided
37
+ if max_calories is not None:
38
+ params["maxCalories"] = max_calories
39
+ if min_protein is not None:
40
+ params["minProtein"] = min_protein
41
+ if max_fat is not None:
42
+ params["maxFat"] = max_fat
43
+ if max_carbs is not None:
44
+ params["maxCarbs"] = max_carbs
45
+
46
+ # Debugging: Print request parameters
47
+ print("Request Params:", params)
48
+
49
+ # Send the API request
50
  response = requests.get(url, params=params)
51
+
52
+ # Debugging: Print response status
53
+ print("Response Status:", response.status_code)
54
+
55
+ # Check if the request was successful
56
  if response.status_code == 200:
57
+ # Debugging: Print the full API response
58
+ print("API Response:", response.json())
59
+
60
+ # Extract recipes from the API response
61
  recipes = response.json().get('results', [])
62
  if recipes:
63
+ # Format and return the list of recipes
64
  return "\n".join(
65
  [f"Recipe: {recipe['title']} (Link: https://spoonacular.com/recipes/{recipe['id']})"
66
  for recipe in recipes]
 
68
  else:
69
  return "No suitable recipes found."
70
  else:
71
+ # Handle API errors
72
  return f"Error fetching recipes: {response.status_code}"
73
 
74
  # Gradio Interface