Spaces:
Sleeping
Sleeping
cyberosa
commited on
Commit
·
bfc9852
1
Parent(s):
a1fd0ab
trying to return just the ingredients
Browse files- tools/calories_checker.py +12 -6
tools/calories_checker.py
CHANGED
|
@@ -2,23 +2,24 @@ import requests
|
|
| 2 |
import os
|
| 3 |
from typing import Any, Optional, List, Dict
|
| 4 |
from smolagents.tools import Tool
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
class CaloriesCheckerTool(Tool):
|
| 8 |
name = "calories_check"
|
| 9 |
-
description = "
|
|
|
|
| 10 |
inputs = {
|
| 11 |
"query": {
|
| 12 |
"type": "string",
|
| 13 |
-
"description": "The query with the food you want to check.",
|
| 14 |
}
|
| 15 |
}
|
| 16 |
-
output_type = "
|
| 17 |
|
| 18 |
def __init__(self, *args, **kwargs):
|
| 19 |
self.is_initialized = False
|
| 20 |
self.api_key = os.environ.get("CALORIES_API_KEY", None)
|
| 21 |
-
print(self.api_key)
|
| 22 |
|
| 23 |
def forward(self, query: str) -> list[dict[str, Any]]:
|
| 24 |
api_url = "https://api.calorieninjas.com/v1/nutrition?query="
|
|
@@ -26,12 +27,17 @@ class CaloriesCheckerTool(Tool):
|
|
| 26 |
api_url + query, headers={"X-Api-Key": str(self.api_key)}
|
| 27 |
)
|
| 28 |
if response.status_code == requests.codes.ok:
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
return
|
| 32 |
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|
| 35 |
query = "bread with fried green peppers"
|
| 36 |
tool = CaloriesCheckerTool()
|
|
|
|
| 37 |
print(tool.forward(query=query))
|
|
|
|
| 2 |
import os
|
| 3 |
from typing import Any, Optional, List, Dict
|
| 4 |
from smolagents.tools import Tool
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
|
| 8 |
class CaloriesCheckerTool(Tool):
|
| 9 |
name = "calories_check"
|
| 10 |
+
description = """Checks the calories and other components of food given n a query with the food you want to check,
|
| 11 |
+
it returns a dictionary with the calories and other nutritional parameters of the food"""
|
| 12 |
inputs = {
|
| 13 |
"query": {
|
| 14 |
"type": "string",
|
| 15 |
+
"description": "The query with the food and ingredients you want to check.",
|
| 16 |
}
|
| 17 |
}
|
| 18 |
+
output_type = "string"
|
| 19 |
|
| 20 |
def __init__(self, *args, **kwargs):
|
| 21 |
self.is_initialized = False
|
| 22 |
self.api_key = os.environ.get("CALORIES_API_KEY", None)
|
|
|
|
| 23 |
|
| 24 |
def forward(self, query: str) -> list[dict[str, Any]]:
|
| 25 |
api_url = "https://api.calorieninjas.com/v1/nutrition?query="
|
|
|
|
| 27 |
api_url + query, headers={"X-Api-Key": str(self.api_key)}
|
| 28 |
)
|
| 29 |
if response.status_code == requests.codes.ok:
|
| 30 |
+
# this is a list of dictionaries
|
| 31 |
+
data = json.loads(response.text)
|
| 32 |
+
items = data["items"]
|
| 33 |
+
response_json = json.dumps(items, indent=4)
|
| 34 |
+
return response_json
|
| 35 |
|
| 36 |
+
return "Error: " + str(response.status_code) + "\n\n".join(response.text)
|
| 37 |
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
query = "bread with fried green peppers"
|
| 41 |
tool = CaloriesCheckerTool()
|
| 42 |
+
print("result")
|
| 43 |
print(tool.forward(query=query))
|