Spaces:
Sleeping
Sleeping
Update tools/recipe_tools.py
Browse files- tools/recipe_tools.py +44 -15
tools/recipe_tools.py
CHANGED
|
@@ -49,29 +49,58 @@ def parse_recipe(url: str) -> Dict[str, Any]:
|
|
| 49 |
'servings': None
|
| 50 |
}
|
| 51 |
|
| 52 |
-
# Get the webpage content
|
| 53 |
-
|
| 54 |
|
| 55 |
-
if
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
# Look for
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
text = element.get_text().strip()
|
| 69 |
-
if
|
| 70 |
-
recipe_data['
|
|
|
|
| 71 |
|
| 72 |
return recipe_data
|
| 73 |
|
| 74 |
except Exception as e:
|
|
|
|
| 75 |
return {'error': f"Error parsing recipe: {str(e)}"}
|
| 76 |
|
| 77 |
@tool
|
|
|
|
| 49 |
'servings': None
|
| 50 |
}
|
| 51 |
|
| 52 |
+
# Get the webpage content using visit_webpage tool
|
| 53 |
+
webpage_content = visit_webpage.forward(url)
|
| 54 |
|
| 55 |
+
if webpage_content:
|
| 56 |
+
# Parse with BeautifulSoup
|
| 57 |
+
soup = BeautifulSoup(webpage_content, 'html.parser')
|
| 58 |
|
| 59 |
+
# Common recipe website classes/IDs to look for
|
| 60 |
+
ingredient_patterns = [
|
| 61 |
+
'ingredient', 'ingredients',
|
| 62 |
+
'recipe-ingredients', 'recipe__ingredient',
|
| 63 |
+
'ingredients-list', 'recipe-ingreds'
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
instruction_patterns = [
|
| 67 |
+
'instruction', 'instructions',
|
| 68 |
+
'recipe-instructions', 'recipe__instruction',
|
| 69 |
+
'preparation', 'prep-steps',
|
| 70 |
+
'recipe-steps', 'recipe-directions'
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
# Look for ingredients
|
| 74 |
+
for pattern in ingredient_patterns:
|
| 75 |
+
elements = soup.find_all(['li', 'div', 'span', 'p'],
|
| 76 |
+
class_=re.compile(pattern, re.I))
|
| 77 |
+
for element in elements:
|
| 78 |
+
text = element.get_text().strip()
|
| 79 |
+
if text and len(text) < 200 and text not in recipe_data['ingredients']:
|
| 80 |
+
recipe_data['ingredients'].append(text)
|
| 81 |
+
|
| 82 |
+
# Look for instructions
|
| 83 |
+
for pattern in instruction_patterns:
|
| 84 |
+
elements = soup.find_all(['li', 'div', 'span', 'p'],
|
| 85 |
+
class_=re.compile(pattern, re.I))
|
| 86 |
+
for element in elements:
|
| 87 |
+
text = element.get_text().strip()
|
| 88 |
+
if text and len(text) < 500 and text not in recipe_data['instructions']:
|
| 89 |
+
recipe_data['instructions'].append(text)
|
| 90 |
|
| 91 |
+
# Look for servings
|
| 92 |
+
servings_elements = soup.find_all(['div', 'span', 'p'],
|
| 93 |
+
class_=re.compile('yield|servings|serves', re.I))
|
| 94 |
+
for element in servings_elements:
|
| 95 |
text = element.get_text().strip()
|
| 96 |
+
if 'serv' in text.lower():
|
| 97 |
+
recipe_data['servings'] = text
|
| 98 |
+
break
|
| 99 |
|
| 100 |
return recipe_data
|
| 101 |
|
| 102 |
except Exception as e:
|
| 103 |
+
print(f"Error parsing recipe: {str(e)}")
|
| 104 |
return {'error': f"Error parsing recipe: {str(e)}"}
|
| 105 |
|
| 106 |
@tool
|