Whitepaper
Docs
Sign In
Function
Function
filter
v1.0.0
Recipe Scraper
Function ID
recipe_scraper
Creator
@willsargent
Downloads
49+
This filter scrapes a recipe from a URL and adds it to the chat history.
Get
README
No README available
Function Code
Show
""" title: Recipe Scraper author: wsargent author_url: https://github.com/wsargent funding_url: https://github.com/open-webui version: 1.0.0 required_open_webui_version: 0.5.7 description: This filter scrapes a recipe from a URL and adds it to the chat history. tags: [recipe, scraper, markdown, webui] requirements: recipe-scrapers, validators """ from recipe_scrapers import scrape_html from urllib.request import urlopen import validators import logging from pydantic import BaseModel from typing import Optional class Filter: # Valves: Configuration options for the filter class Valves(BaseModel): pass def __init__(self): # Initialize valves (optional configuration for the Filter) self.valves = self.Valves() def inlet(self, body: dict) -> dict: # This is where you manipulate user inputs. last_message = body["messages"][-1]["content"] # if the last message is a standalone URL, then replace it with the recipe if validators.url(last_message): url = last_message logging.debug(f"url found: {url}") # If there's a recipe, then add it to the context recipe_markdown = scrape_recipe_to_markdown(url) if recipe_markdown: logging.debug(f"recipe extracted: {recipe_markdown}") body["messages"][-1]["content"] = recipe_markdown return body def outlet(self, body: dict) -> None: # This is where you manipulate model outputs. print(f"outlet called: {body}") def scrape_recipe_to_markdown(url: str) -> Optional[str]: try: html = urlopen(url).read().decode("utf-8") scraper = scrape_html(html, org_url=url) # Extracting the necessary information title = scraper.title() ingredients = scraper.ingredients() instructions = scraper.instructions() total_time = scraper.total_time() if scraper.total_time() else "Not specified" yields = scraper.yields() if scraper.yields() else "Not specified" # Constructing the Markdown string markdown_recipe = f"# {title}\n\n" markdown_recipe += f"**Total Time:** {total_time} minutes\n" markdown_recipe += f"**Yields:** {yields}\n\n" markdown_recipe += "## Ingredients\n" markdown_recipe += ( "\n".join(f"- {ingredient}" for ingredient in ingredients) + "\n\n" ) markdown_recipe += "## Instructions\n" markdown_recipe += instructions return markdown_recipe except Exception as e: logging.error(f"Error scraping recipe from {url}: {str(e)}") return None # Example usage within a pipe function def pipe_function(prompt): # Assuming the prompt contains a URL url = prompt.strip() try: return scrape_recipe_to_markdown(url) except Exception as e: return f"Failed to scrape recipe: {str(e)}"