Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +7 -0
- requirements.txt +2 -0
- tool.py +49 -0
app.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import launch_gradio_demo
|
| 2 |
+
from typing import Optional
|
| 3 |
+
from tool import SimpleTool
|
| 4 |
+
|
| 5 |
+
tool = SimpleTool()
|
| 6 |
+
|
| 7 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests
|
| 2 |
+
smolagents
|
tool.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from typing import Optional
|
| 3 |
+
|
| 4 |
+
class SimpleTool(Tool):
|
| 5 |
+
name = "get_recipe"
|
| 6 |
+
description = "Gets a recipe suggestion based on the provided ingredients and dietary preference."
|
| 7 |
+
inputs = {"ingredients":{"type":"string","description":"A comma-separated string of available ingredients."},"diet":{"type":"string","nullable":True,"description":"Dietary restrictions such as 'vegetarian', 'vegan', or 'gluten free'. Defaults to None."}}
|
| 8 |
+
output_type = "string"
|
| 9 |
+
|
| 10 |
+
def forward(self, ingredients: str, diet: Optional[str] = None) -> str:
|
| 11 |
+
"""
|
| 12 |
+
Gets a recipe suggestion based on the provided ingredients and dietary preference.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
ingredients: A comma-separated string of available ingredients.
|
| 16 |
+
diet: Dietary restrictions such as 'vegetarian', 'vegan', or 'gluten free'. Defaults to None.
|
| 17 |
+
"""
|
| 18 |
+
import os
|
| 19 |
+
import requests
|
| 20 |
+
|
| 21 |
+
# Retrieve your Spoonacular API key from the environment
|
| 22 |
+
api_key = "0cbb3d6a82f5497e818d1f63dc736218"
|
| 23 |
+
if not api_key:
|
| 24 |
+
return "Spoonacular API key not set. Please set the SPOONACULAR_API_KEY environment variable."
|
| 25 |
+
|
| 26 |
+
base_url = "https://api.spoonacular.com/recipes/findByIngredients"
|
| 27 |
+
params = {
|
| 28 |
+
"ingredients": ingredients,
|
| 29 |
+
"number": 1, # Retrieve one recipe suggestion
|
| 30 |
+
"ranking": 1,
|
| 31 |
+
"apiKey": api_key,
|
| 32 |
+
}
|
| 33 |
+
if diet:
|
| 34 |
+
params["diet"] = diet
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
response = requests.get(base_url, params=params)
|
| 38 |
+
response.raise_for_status()
|
| 39 |
+
data = response.json()
|
| 40 |
+
if not data:
|
| 41 |
+
return "No recipes found with these ingredients and dietary restrictions."
|
| 42 |
+
recipe = data[0]
|
| 43 |
+
title = recipe.get("title", "Untitled Recipe")
|
| 44 |
+
used_count = recipe.get("usedIngredientCount", 0)
|
| 45 |
+
missed_count = recipe.get("missedIngredientCount", 0)
|
| 46 |
+
return (f"Recipe suggestion: {title} "
|
| 47 |
+
f"(used {used_count} of your ingredients, missing {missed_count} additional ingredient(s)).")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error occurred: {e}"
|