├── requirements.txt ├── src ├── main.py ├── utils.py ├── recipe.py └── test.ipynb ├── README.md ├── .vscode └── settings.json ├── LICENSE └── .gitignore /requirements.txt: -------------------------------------------------------------------------------- 1 | Requests==2.31.0 2 | -------------------------------------------------------------------------------- /src/main.py: -------------------------------------------------------------------------------- 1 | from recipe import RecipeGenerator 2 | 3 | 4 | 5 | 6 | if __name__ == "__main__": 7 | """ 8 | Test RecipeGenerator class without creating an image of the dish. 9 | """ 10 | 11 | gen = RecipeGenerator() 12 | recipe = gen.generate_recipe() 13 | print(recipe) -------------------------------------------------------------------------------- /src/utils.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | import requests 4 | 5 | 6 | def call_llama2(model, prompt, stream=False): 7 | url = "http://localhost:11434/api/generate" 8 | data = { 9 | 'model' : model, 10 | 'prompt' : prompt, 11 | 'stream' : stream 12 | } 13 | json_data = json.dumps(data) 14 | response = requests.post(url, json_data, headers={'Content-Type': 'application/json'}) 15 | if response.status_code == 200: 16 | return response.json()['response'] 17 | 18 | return f'Error code is: {response.status_code}' -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # recipe-creator-llama 🦙 2 | ![img](https://images.nightcafe.studio/jobs/6BaR2m3Net6xYb2AaHTR/6BaR2m3Net6xYb2AaHTR--1--npvcq_6x.jpg?tr=w-1600,c-at_max) 3 | 4 | ## Description 5 | This is a recipe creator app that allow user to input ingredient to create a recipe along with the instruction. The user can also search for recipes that are already created by other users. 6 | 7 | ## Installation 8 | To install necessary dependencies, run the following command: install required dependencies 9 | ```python 10 | pip install -r requirements.txt 11 | ``` 12 | 13 | ## Usage 14 | 1. clone the repo 15 | 2. go to the directory of the repo where you cloned 16 | 3. run the command: `python main.py` 17 | 4. Enjoy! 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "workbench.colorCustomizations": { 3 | "activityBar.activeBackground": "#fbed80", 4 | "activityBar.background": "#fbed80", 5 | "activityBar.foreground": "#15202b", 6 | "activityBar.inactiveForeground": "#15202b99", 7 | "activityBarBadge.background": "#06b9a5", 8 | "activityBarBadge.foreground": "#15202b", 9 | "commandCenter.border": "#15202b99", 10 | "sash.hoverBorder": "#fbed80", 11 | "statusBar.background": "#f9e64f", 12 | "statusBar.foreground": "#15202b", 13 | "statusBarItem.hoverBackground": "#f7df1e", 14 | "statusBarItem.remoteBackground": "#f9e64f", 15 | "statusBarItem.remoteForeground": "#15202b", 16 | "titleBar.activeBackground": "#f9e64f", 17 | "titleBar.activeForeground": "#15202b", 18 | "titleBar.inactiveBackground": "#f9e64f99", 19 | "titleBar.inactiveForeground": "#15202b99" 20 | }, 21 | "peacock.color": "#f9e64f" 22 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Shirin Yamani 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/recipe.py: -------------------------------------------------------------------------------- 1 | from utils import * 2 | 3 | class RecipeGenerator: 4 | def __init__(self): 5 | 6 | self.list_of_ingredients = self.ask_for_ingredients() 7 | 8 | @staticmethod 9 | def ask_for_ingredients(): 10 | list_of_ingredients = [] 11 | while True: 12 | ingredient = input("Enter an ingredient (or type 'done' to finish): ") 13 | if ingredient.lower() == "done": 14 | break 15 | list_of_ingredients.append(ingredient) 16 | print(f"Your ingredients are: {', '.join(list_of_ingredients)}") 17 | return list_of_ingredients 18 | 19 | def generate_recipe(self): 20 | prompt = RecipeGenerator.create_recipe_prompt(self.list_of_ingredients) 21 | if RecipeGenerator._verify_prompt(prompt): 22 | response = RecipeGenerator.generate(prompt) 23 | return response 24 | print("Sorry about that!") 25 | 26 | @staticmethod 27 | def create_recipe_prompt(list_of_ingredients): 28 | prompt = f"Create a detailed recipe based on only the following ingredients: {', '.join(list_of_ingredients)}." 29 | return prompt 30 | 31 | @staticmethod 32 | def _verify_prompt(prompt): 33 | print(prompt) 34 | response = input("Are you happy with the prompt? (y/n)") 35 | if response.upper() == "Y": 36 | return True 37 | return False 38 | 39 | @staticmethod 40 | def generate(prompt): 41 | response = call_llama2( 42 | model='llama2', 43 | prompt=prompt, 44 | stream=False) 45 | return response 46 | 47 | def store_recipe(self, recipe, filename): 48 | with open(filename, "w") as f: 49 | f.write(recipe) 50 | 51 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /src/test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 20, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import openai\n", 10 | "import os\n", 11 | "import requests\n", 12 | "import json " 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 17, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "def call_llama2(model, prompt, stream=False):\n", 22 | " url = \"http://localhost:11434/api/generate\"\n", 23 | " data = {\n", 24 | " 'model' : model,\n", 25 | " 'prompt' : prompt,\n", 26 | " 'stream' : stream\n", 27 | " }\n", 28 | " json_data = json.dumps(data)\n", 29 | " response = requests.post(url, json_data, headers={'Content-Type': 'application/json'})\n", 30 | " if response.status_code == 200:\n", 31 | " return response.json()['response']\n", 32 | " \n", 33 | " return f'Error code is: {response.status_code}'" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 17, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "data": { 43 | "text/plain": [ 44 | "'Shirin Yamini is a British-Iranian actress, singer, and model. She was born on February 17, 1985, in Tehran, Iran. Yamini began her career as a model before transitioning into acting and singing. She has appeared in several films and television shows, including the BBC series \"Our Zoo\" and the film \"The Tag.\"\\n\\nYamini is known for her versatility as an actress, able to play a wide range of roles. She has also been recognized for her activism, particularly in regards to women\\'s rights and social justice. In 2018, she was named as one of the \"100 Most Influential People in the Middle East\" by Arabian Business.\\n\\nYamini is also a talented singer, and has released several songs and music videos. Her music often deals with themes of love, social justice, and personal empowerment. She has been praised for her powerful voice and captivating stage presence.\\n\\nIn addition to her acting and singing career, Yamini is also a philanthropist and humanitarian. She has worked with several organizations to support women\\'s rights and education initiatives around the world.\\n\\nOverall, Shirin Yamini is a talented and multifaceted artist who has made a significant impact in the entertainment industry and beyond.'" 45 | ] 46 | }, 47 | "execution_count": 17, 48 | "metadata": {}, 49 | "output_type": "execute_result" 50 | } 51 | ], 52 | "source": [ 53 | "call_llama2(model='llama2', prompt='who is shirin yamani', stream=False)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 35, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 37, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "Create a detailed Recipe based on ONLY the following ingredient:egg, bread, eggplant.\n", 73 | "Additionally assign a tittle starting with 'Recipe Title:' to this recipe\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "\n" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 8, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "def user_input():\n", 88 | " user_input= input(\"Enter your ingredients:\")\n", 89 | " return user_input" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 12, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "'onion 2 muchroom '" 101 | ] 102 | }, 103 | "execution_count": 12, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "user_input()" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 33, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "class RecipeGenerator:\n", 119 | " def __init__(self):\n", 120 | " self.list_ingredients = self.ask_ingredients_user()\n", 121 | " \n", 122 | " @staticmethod\n", 123 | " def ask_ingredients_user():\n", 124 | " list_ingredient = []\n", 125 | " while True:\n", 126 | " ingredient = input(\"Enter your ingredient (or type 'done' to finish):\")\n", 127 | " if ingredient.lower() == \"done\":\n", 128 | " break\n", 129 | " elif ingredient.isdigit():\n", 130 | " print('Invalid input, Try again!')\n", 131 | " \n", 132 | " list_ingredient.append(ingredient)\n", 133 | " print(f\"your ingredients are: {','.join(list_ingredient)}\")\n", 134 | " return list_ingredient\n", 135 | "\n", 136 | " \n", 137 | " def generate_recipe(self):\n", 138 | " prompt = RecipeGenerator.create_recipe_prompt(self.list_ingredients)\n", 139 | " if RecipeGenerator.verify_prompt(prompt):\n", 140 | " response = RecipeGenerator.generate_call(prompt)\n", 141 | " return response\n", 142 | " raise ValueError(\"prompt not accepted.\")\n", 143 | "\n", 144 | "\n", 145 | " @staticmethod\n", 146 | " def create_recipe_prompt(list_ingredient):\n", 147 | " prompt = f\"Create a detailed recipe based on these ingredients: {','.join(list_ingredient)}\"\n", 148 | " return prompt\n", 149 | " \n", 150 | " @staticmethod\n", 151 | " def verify_prompt(prompt):\n", 152 | " print(prompt)\n", 153 | " response = input(\"This is your prompt, are you happy with it? y/n\")\n", 154 | " if response.isdigit():\n", 155 | " print('Invalid response, Plese choice from Y/N')\n", 156 | " if response.upper() == \"Y\":\n", 157 | " return True\n", 158 | " return False \n", 159 | " \n", 160 | " @staticmethod\n", 161 | " def generate_call(prompt):\n", 162 | " response = call_llama2(\n", 163 | " model='llama2', \n", 164 | " prompt=prompt, \n", 165 | " stream=False\n", 166 | " )\n", 167 | " return response\n", 168 | " \n", 169 | " def store_recipe(self, recipe, filename):\n", 170 | " with open(filename, \"w\") as f:\n", 171 | " f.write(recipe)\n", 172 | "\n", 173 | "\n" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "- get the ingridents from user \n", 181 | "- verify the ingridents\n", 182 | "- create recipe prompt for the model\n", 183 | "- call model to generate the recipe base on the input ingredients + get the response of the model \n", 184 | "- save the recipe as file \n" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": null, 190 | "metadata": {}, 191 | "outputs": [], 192 | "source": [ 193 | "\n" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 43, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "Your ingredients are: egg, eggplant\n", 206 | "Create a detailed recipe based on only the following ingredients: egg, eggplant.\n", 207 | "Sorry about that!\n", 208 | "None\n" 209 | ] 210 | } 211 | ], 212 | "source": [ 213 | "gen = RecipeGenerator()\n", 214 | "recipe = gen.generate_recipe()\n", 215 | "print(recipe)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "metadata": {}, 222 | "outputs": [], 223 | "source": [] 224 | } 225 | ], 226 | "metadata": { 227 | "kernelspec": { 228 | "display_name": "ali-ml", 229 | "language": "python", 230 | "name": "python3" 231 | }, 232 | "language_info": { 233 | "codemirror_mode": { 234 | "name": "ipython", 235 | "version": 3 236 | }, 237 | "file_extension": ".py", 238 | "mimetype": "text/x-python", 239 | "name": "python", 240 | "nbconvert_exporter": "python", 241 | "pygments_lexer": "ipython3", 242 | "version": "3.10.12" 243 | } 244 | }, 245 | "nbformat": 4, 246 | "nbformat_minor": 2 247 | } 248 | --------------------------------------------------------------------------------