Spaces:
Sleeping
Sleeping
File size: 1,489 Bytes
6155246 f52ad11 6155246 bfc9852 6155246 bfc9852 6155246 bfc9852 6155246 bfc9852 6155246 f52ad11 a1fd0ab 6155246 bfc9852 6155246 bfc9852 a1fd0ab bfc9852 a1fd0ab |
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 |
import requests
import os
from typing import Any, Optional, List, Dict
from smolagents.tools import Tool
import json
class CaloriesCheckerTool(Tool):
name = "calories_check"
description = """Checks the calories and other components of food given n a query with the food you want to check,
it returns a dictionary with the calories and other nutritional parameters of the food"""
inputs = {
"query": {
"type": "string",
"description": "The query with the food and ingredients you want to check.",
}
}
output_type = "string"
def __init__(self, *args, **kwargs):
self.is_initialized = False
self.api_key = os.environ.get("CALORIES_API_KEY", None)
def forward(self, query: str) -> list[dict[str, Any]]:
api_url = "https://api.calorieninjas.com/v1/nutrition?query="
response = requests.get(
api_url + query, headers={"X-Api-Key": str(self.api_key)}
)
if response.status_code == requests.codes.ok:
# this is a list of dictionaries
data = json.loads(response.text)
items = data["items"]
response_json = json.dumps(items, indent=4)
return response_json
return "Error: " + str(response.status_code) + "\n\n".join(response.text)
if __name__ == "__main__":
query = "bread with fried green peppers"
tool = CaloriesCheckerTool()
print("result")
print(tool.forward(query=query))
|