├── recipe-parse-screenshot.png ├── README.md ├── requirements.txt ├── app.py └── .gitignore /recipe-parse-screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarforever/amazing-jo/main/recipe-parse-screenshot.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🥑 Amazing JO's Recipe 2 | 3 | This is the example application that shows you how to apply AI to your very sepcific feature set. 4 | 5 | In this application, we follow the workflow below to extract structured data from Jamie Oliver's recipes: 6 | 1. Fetch HTML markup from recipe URL. 7 | 2. Define `Pydantic` recipe schema. 8 | 3. Define model output parser 9 | 4. Define chat prompt template 10 | 5. Use OpenAI `gpt-3.5-turbo-16k` model to run the query 11 | 6. Parse the output to JSON data 12 | 13 | ## Deployment 14 | 15 | https://amazing-jo-recipe.streamlit.app/ 16 | 17 | ### Usage 18 | 19 | You need to pass the following parameters to parse a JO recipe: 20 | 1. OpenAI API Key 21 | 2. JO recipe URL, for example: [https://www.jamieoliver.com/recipes/liver-recipes/liver-bacon-onions/](https://www.jamieoliver.com/recipes/liver-recipes/liver-bacon-onions/) 22 | 23 | Now, please click `Parse Recipe` button to expect structured recipe data output. 24 | 25 | ![Screenshot](./recipe-parse-screenshot.png "Recipe Parse Screenshot") -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | aiohttp==3.8.5 2 | aiosignal==1.3.1 3 | altair==5.0.1 4 | annotated-types==0.5.0 5 | async-timeout==4.0.3 6 | attrs==23.1.0 7 | beautifulsoup4==4.12.2 8 | blinker==1.6.2 9 | cachetools==5.3.1 10 | certifi==2023.7.22 11 | charset-normalizer==3.2.0 12 | click==8.1.7 13 | dataclasses-json==0.5.14 14 | frozenlist==1.4.0 15 | gitdb==4.0.10 16 | GitPython==3.1.32 17 | google-api-core==2.11.1 18 | google-auth==2.22.0 19 | googleapis-common-protos==1.60.0 20 | idna==3.4 21 | importlib-metadata==6.8.0 22 | Jinja2==3.1.2 23 | jsonschema==4.19.0 24 | jsonschema-specifications==2023.7.1 25 | langchain==0.0.272 26 | langsmith==0.0.26 27 | markdown-it-py==3.0.0 28 | MarkupSafe==2.1.3 29 | marshmallow==3.20.1 30 | mdurl==0.1.2 31 | multidict==6.0.4 32 | mypy-extensions==1.0.0 33 | numexpr==2.8.5 34 | numpy==1.25.2 35 | openai==0.27.9 36 | packaging==23.1 37 | pandas==2.0.3 38 | Pillow==9.5.0 39 | protobuf==4.24.1 40 | pyarrow==13.0.0 41 | pyasn1==0.5.0 42 | pyasn1-modules==0.3.0 43 | pydantic==2.3.0 44 | pydantic_core==2.6.3 45 | pydeck==0.8.0 46 | Pygments==2.16.1 47 | Pympler==1.0.1 48 | python-dateutil==2.8.2 49 | pytz==2023.3 50 | pytz-deprecation-shim==0.1.0.post0 51 | PyYAML==6.0.1 52 | referencing==0.30.2 53 | requests==2.31.0 54 | rich==13.5.2 55 | rpds-py==0.9.2 56 | rsa==4.9 57 | six==1.16.0 58 | smmap==5.0.0 59 | soupsieve==2.4.1 60 | SQLAlchemy==2.0.20 61 | streamlit==1.25.0 62 | tenacity==8.2.3 63 | toml==0.10.2 64 | toolz==0.12.0 65 | tornado==6.3.3 66 | tqdm==4.66.1 67 | typing-inspect==0.9.0 68 | typing_extensions==4.7.1 69 | tzdata==2023.3 70 | tzlocal==4.3.1 71 | urllib3==1.26.16 72 | validators==0.21.2 73 | yarl==1.9.2 74 | zipp==3.16.2 75 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from langchain.output_parsers import PydanticOutputParser 2 | from langchain.prompts import ( 3 | PromptTemplate 4 | ) 5 | from langchain.schema import ( 6 | HumanMessage 7 | ) 8 | from langchain.chat_models import ChatOpenAI 9 | from pydantic import BaseModel, Field 10 | from typing import List 11 | from bs4 import BeautifulSoup 12 | import requests 13 | import streamlit as st 14 | 15 | class Ingredient(BaseModel): 16 | name: str = Field(description="The name of the ingredient") 17 | quantity: str = Field(description="The specific unit of measurement corresponding to the quantity, such as grams, ounces, liters, etc.") 18 | unit: str = Field(description="The amount of the ingredient required for the recipe. This can be represented using various units such as grams, cups, teaspoons, etc.") 19 | 20 | class Recipe(BaseModel): 21 | name: str = Field(description="The name of the recipe") 22 | ingredients: List[Ingredient] = Field(description="The list of ingredients for the recipe") 23 | 24 | PAGE_TITLE = "🥑 Amazing JO's Recipe" 25 | st.set_page_config(layout="centered", page_title=PAGE_TITLE) 26 | 27 | st.title(PAGE_TITLE) 28 | 29 | def get_recipe_html(url): 30 | response = requests.get(url) 31 | html_markup = '' 32 | if response.status_code == 200: 33 | html_markup = response.text 34 | soup = BeautifulSoup(html_markup, 'html.parser') 35 | 36 | # Find the element with id 'recipe-single' 37 | recipe_element = soup.find(id='recipe-single') 38 | 39 | if recipe_element: 40 | # Get the sanitized content within the 'recipe-single' element 41 | html_markup = str(recipe_element) 42 | 43 | return html_markup 44 | 45 | def parse_by_chatgpt(openai_api_key, html_markup): 46 | parser = PydanticOutputParser(pydantic_object=Recipe) 47 | prompt = PromptTemplate( 48 | template="Extract the recipe ingredients from the following HTML markup:\n{html}.\n{format_instructions}\n", 49 | input_variables=["html"], 50 | partial_variables={"format_instructions": parser.get_format_instructions()}, 51 | ) 52 | model = ChatOpenAI(model="gpt-3.5-turbo-16k", temperature=0.0, openai_api_key=openai_api_key) 53 | output = model([ HumanMessage(content=prompt.format_prompt(html=html_markup).to_string()) ]) 54 | 55 | recipe = parser.parse(output.content) 56 | return recipe 57 | 58 | with st.container(): 59 | openai_api_key = st.text_input("OpenAI API Key", type="password", key="openai_api_key") 60 | recipe_url = st.text_input("URL of a Jamie Oliver Recipe", key="recipe_url") 61 | clicked = st.button("Parse Recipe") 62 | if clicked: 63 | html_markup = get_recipe_html(recipe_url) 64 | if html_markup: 65 | recipe = parse_by_chatgpt(openai_api_key, html_markup) 66 | st.json(recipe.model_dump_json()) 67 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | --------------------------------------------------------------------------------