Spaces:
Sleeping
Sleeping
Create generate_recipe.py
Browse files- tools/generate_recipe.py +48 -0
tools/generate_recipe.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional
|
| 2 |
+
from smolagents.tools import Tool
|
| 3 |
+
import requests
|
| 4 |
+
import markdownify
|
| 5 |
+
import smolagents
|
| 6 |
+
|
| 7 |
+
class RecipeGeneratorTool(Tool):
|
| 8 |
+
name = "recipe_generator"
|
| 9 |
+
description = "Generates a high-protein recipe based on given parameters like calories, protein content, and ingredients."
|
| 10 |
+
inputs = {
|
| 11 |
+
'calories': {'type': 'number', 'description': 'The desired calorie content for the recipe.'},
|
| 12 |
+
'protein': {'type': 'number', 'description': 'The desired protein content (in grams) for the recipe.'},
|
| 13 |
+
'ingredients': {'type': 'list', 'description': 'List of ingredients to include in the recipe.'}
|
| 14 |
+
}
|
| 15 |
+
output_type = 'string'
|
| 16 |
+
|
| 17 |
+
def generate_recipe(self, calories: float, protein: float, ingredients: list):
|
| 18 |
+
# Placeholder for a real API request to a recipe database or calculation logic
|
| 19 |
+
recipe_data = {
|
| 20 |
+
"title": "Protein-Packed Chicken Salad",
|
| 21 |
+
"description": "A healthy, protein-rich salad with chicken, spinach, and quinoa.",
|
| 22 |
+
"ingredients": ["Chicken breast", "Spinach", "Quinoa", "Olive oil", "Lemon"],
|
| 23 |
+
"steps": [
|
| 24 |
+
"Cook the chicken breast until fully done.",
|
| 25 |
+
"Cook quinoa according to package instructions.",
|
| 26 |
+
"Mix cooked chicken and quinoa with spinach.",
|
| 27 |
+
"Add a drizzle of olive oil and a squeeze of lemon."
|
| 28 |
+
],
|
| 29 |
+
"calories": calories,
|
| 30 |
+
"protein": protein
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# Converting recipe data to markdown for better readability
|
| 34 |
+
recipe_markdown = f"# {recipe_data['title']}\n\n"
|
| 35 |
+
recipe_markdown += f"**Description**: {recipe_data['description']}\n\n"
|
| 36 |
+
recipe_markdown += "## Ingredients:\n"
|
| 37 |
+
for ingredient in recipe_data['ingredients']:
|
| 38 |
+
recipe_markdown += f"- {ingredient}\n"
|
| 39 |
+
recipe_markdown += "\n## Steps:\n"
|
| 40 |
+
for step in recipe_data['steps']:
|
| 41 |
+
recipe_markdown += f"1. {step}\n"
|
| 42 |
+
recipe_markdown += f"\n**Calories**: {recipe_data['calories']} kcal\n"
|
| 43 |
+
recipe_markdown += f"**Protein**: {recipe_data['protein']} g\n"
|
| 44 |
+
|
| 45 |
+
return markdownify.markdownify(recipe_markdown)
|
| 46 |
+
|
| 47 |
+
def forward(self, calories: float, protein: float, ingredients: list):
|
| 48 |
+
return self.generate_recipe(calories, protein, ingredients)
|