|
| 1 | +import json |
| 2 | +import re |
| 3 | +import os |
| 4 | + |
| 5 | +from litellm import completion |
| 6 | +from recipe_scrapers import scrape_html |
| 7 | +from recipe_scrapers._exceptions import SchemaOrgException |
| 8 | +from app.service.ingredient_parsing import parseIngredients |
| 9 | +from app.models import Recipe, Item, Household |
| 10 | +from app.config import SUPPORTED_LANGUAGES |
| 11 | + |
| 12 | +LLM_MODEL = os.getenv("LLM_MODEL") |
| 13 | +LLM_API_URL = os.getenv("LLM_API_URL") |
| 14 | + |
| 15 | + |
| 16 | +def scrapeHTML(url: str, html: str, household: Household) -> dict | None: |
| 17 | + try: |
| 18 | + scraper = scrape_html(html, url, supported_only=False) |
| 19 | + except Exception: |
| 20 | + return None |
| 21 | + recipe = Recipe() |
| 22 | + try: |
| 23 | + recipe.name = scraper.title().strip()[:128] |
| 24 | + except ( |
| 25 | + NotImplementedError, |
| 26 | + ValueError, |
| 27 | + TypeError, |
| 28 | + AttributeError, |
| 29 | + SchemaOrgException, |
| 30 | + ): |
| 31 | + return None # Unsupported if title cannot be scraped |
| 32 | + try: |
| 33 | + recipe.time = int(scraper.total_time()) |
| 34 | + except ( |
| 35 | + NotImplementedError, |
| 36 | + ValueError, |
| 37 | + TypeError, |
| 38 | + AttributeError, |
| 39 | + SchemaOrgException, |
| 40 | + ): |
| 41 | + pass |
| 42 | + try: |
| 43 | + recipe.cook_time = int(scraper.cook_time()) |
| 44 | + except ( |
| 45 | + NotImplementedError, |
| 46 | + ValueError, |
| 47 | + TypeError, |
| 48 | + AttributeError, |
| 49 | + SchemaOrgException, |
| 50 | + ): |
| 51 | + pass |
| 52 | + try: |
| 53 | + recipe.prep_time = int(scraper.prep_time()) |
| 54 | + except ( |
| 55 | + NotImplementedError, |
| 56 | + ValueError, |
| 57 | + TypeError, |
| 58 | + AttributeError, |
| 59 | + SchemaOrgException, |
| 60 | + ): |
| 61 | + pass |
| 62 | + try: |
| 63 | + yields = re.search(r"\d*", scraper.yields()) |
| 64 | + if yields: |
| 65 | + recipe.yields = int(yields.group()) |
| 66 | + except ( |
| 67 | + NotImplementedError, |
| 68 | + ValueError, |
| 69 | + TypeError, |
| 70 | + AttributeError, |
| 71 | + SchemaOrgException, |
| 72 | + ): |
| 73 | + pass |
| 74 | + description = "" |
| 75 | + try: |
| 76 | + description = scraper.description() + "\n\n" |
| 77 | + except ( |
| 78 | + NotImplementedError, |
| 79 | + ValueError, |
| 80 | + TypeError, |
| 81 | + AttributeError, |
| 82 | + SchemaOrgException, |
| 83 | + ): |
| 84 | + pass |
| 85 | + try: |
| 86 | + description = description + scraper.instructions() |
| 87 | + except ( |
| 88 | + NotImplementedError, |
| 89 | + ValueError, |
| 90 | + TypeError, |
| 91 | + AttributeError, |
| 92 | + SchemaOrgException, |
| 93 | + ): |
| 94 | + pass |
| 95 | + recipe.description = description |
| 96 | + recipe.photo = scraper.image() |
| 97 | + recipe.source = url |
| 98 | + items = {} |
| 99 | + for ingredient in parseIngredients(scraper.ingredients(), household.language): |
| 100 | + name = ingredient.name if ingredient.name else ingredient.originalText or "" |
| 101 | + item = Item.find_name_starts_with(household.id, name) |
| 102 | + if item: |
| 103 | + items[ingredient.originalText] = item.obj_to_dict() | { |
| 104 | + "description": ingredient.description, |
| 105 | + "optional": False, |
| 106 | + } |
| 107 | + else: |
| 108 | + items[ingredient.originalText] = None |
| 109 | + return { |
| 110 | + "recipe": recipe.obj_to_dict(), |
| 111 | + "items": items, |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | +def scrapeHTMLLLM(url: str, html: str, household: Household) -> dict | None: |
| 116 | + if not LLM_MODEL: |
| 117 | + return None |
| 118 | + |
| 119 | + systemMessage = """ |
| 120 | +You are a tool that returns only JSON and nothing else. You get a html page of a recipe as an input. You return the json in the form {"name": "recipe name", "photo": "url to photo", "description": "description with instructions in markdown", "yields": "servings count", "time": integer total cooking time in minutes, "ingredients" ["list of string ingrediends including amount name and description"]}. |
| 121 | +
|
| 122 | +For example a result in English: |
| 123 | +{ |
| 124 | + "name": "Pumpkin Soup", |
| 125 | + "photo": "https://recipes.com/photos/1283010293.jpg", |
| 126 | + "description": "Delicious soup. |
| 127 | +- First wash the pumpkin |
| 128 | +- Cut Pumpkin |
| 129 | +- Make soup", |
| 130 | + "yields": 4, |
| 131 | + "time": 20, |
| 132 | + "ingredients": [ |
| 133 | + "1 Pumpkin", |
| 134 | + "50g red Onions" |
| 135 | + ] |
| 136 | +} |
| 137 | +
|
| 138 | +Return only JSON and nothing else. |
| 139 | +""" |
| 140 | + |
| 141 | + messages = [ |
| 142 | + { |
| 143 | + "role": "system", |
| 144 | + "content": systemMessage, |
| 145 | + } |
| 146 | + ] |
| 147 | + if household.language in SUPPORTED_LANGUAGES: |
| 148 | + messages.append( |
| 149 | + { |
| 150 | + "role": "user", |
| 151 | + "content": f"Translate the response to {SUPPORTED_LANGUAGES[household.language]}. Translate the JSON content to {SUPPORTED_LANGUAGES[household.language]}. Your target language is {SUPPORTED_LANGUAGES[household.language]}. Respond in {SUPPORTED_LANGUAGES[household.language]} from the start.", |
| 152 | + } |
| 153 | + ) |
| 154 | + |
| 155 | + messages.append( |
| 156 | + { |
| 157 | + "role": "user", |
| 158 | + "content": html, |
| 159 | + } |
| 160 | + ) |
| 161 | + |
| 162 | + response = completion( |
| 163 | + model=LLM_MODEL, |
| 164 | + api_base=LLM_API_URL, |
| 165 | + # response_format={"type": "json_object"}, |
| 166 | + messages=messages, |
| 167 | + ) |
| 168 | + |
| 169 | + print(response.choices[0].message.content) |
| 170 | + try: |
| 171 | + llmResponse = json.loads(response.choices[0].message.content) |
| 172 | + except Exception as e: |
| 173 | + print(e) |
| 174 | + return None |
| 175 | + |
| 176 | + items = {} |
| 177 | + for ingredient in parseIngredients(llmResponse["ingredients"], household.language): |
| 178 | + name = ingredient.name if ingredient.name else ingredient.originalText or "" |
| 179 | + item = Item.find_name_starts_with(household.id, name) |
| 180 | + if item: |
| 181 | + items[ingredient.originalText] = item.obj_to_dict() | { |
| 182 | + "description": ingredient.description, |
| 183 | + "optional": False, |
| 184 | + } |
| 185 | + else: |
| 186 | + items[ingredient.originalText] = None |
| 187 | + |
| 188 | + return { |
| 189 | + "recipe": llmResponse |
| 190 | + | { |
| 191 | + "id": None, |
| 192 | + "public": False, |
| 193 | + "source": url, |
| 194 | + }, |
| 195 | + "items": items, |
| 196 | + } |
0 commit comments