Spaces:
Sleeping
Sleeping
| # https://stackoverflow.com/questions/50951955/pytesseract-tesseractnotfound-error-tesseract-is-not-installed-or-its-not-i | |
| # https://pysource.com/2020/04/23/text-recognition-ocr-with-tesseract-and-opencv/ | |
| import os | |
| import openai | |
| import gradio | |
| import json | |
| from pathlib import Path | |
| from bs4 import BeautifulSoup | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| content_input = "Format the recipe, given the format provided. You must return an HTML:" | |
| html_template = ''' | |
| <div itemscope itemtype="https://schema.org/Recipe"> | |
| <span itemprop="name">Mom's World Famous Banana Bread</span> | |
| <img itemprop="image" src="https://encrypted-tbn0.gstatic.com/banana.jpg" /> | |
| <span itemprop="description">This classic banana bread recipe comes | |
| from my mom.</span> | |
| <span itemprop="recipeIngredient">3 or 4 ripe bananas, smashed</span> | |
| <span itemprop="recipeIngredient">3/4 cup of sugar</span> | |
| <span itemprop="recipeInstructions"> | |
| 1 - Preheat the oven to 350 degrees. | |
| </span> | |
| <span itemprop="recipeInstructions"> | |
| 2 - Mix in the ingredients in a bowl. | |
| </span> | |
| </div> | |
| ''' | |
| content_input+=html_template | |
| messages = [{"role": "system", "content": content_input}] | |
| # create a static directory to store the static files | |
| static_dir = Path('./static') | |
| static_dir.mkdir(parents=True, exist_ok=True) | |
| def CustomChatGPT(html_explainer, recipe): | |
| recipe_name_str="" | |
| ingredients_str="" | |
| steps_str="" | |
| import_link_str = "" | |
| # if image is not None and bool(image): | |
| # recipe = pytesseract.image_to_string(Image.open(image)) | |
| if recipe is not None and bool(recipe): | |
| messages.append({"role": "user", "content": recipe}) | |
| response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages) | |
| ChatGPT_reply = response["choices"][0]["message"]["content"] | |
| file_name = "output.html" | |
| file_path = static_dir / file_name | |
| soup = BeautifulSoup(ChatGPT_reply, 'html.parser') | |
| recipe_name = soup.find(attrs={"itemprop": "name"}) | |
| recipe_name_str = recipe_name.text | |
| ingredients_html = soup.find_all(attrs={"itemprop": "recipeIngredient"}) | |
| ingredients = [item.text.strip() for item in ingredients_html] | |
| ingredients_str = "\n".join(ingredients) | |
| steps_html = soup.find_all(attrs={"itemprop": "recipeInstructions"}) | |
| steps = [item.text.strip() for item in steps_html] | |
| steps_str = "\n".join(steps) | |
| import_link_str = "<a href='file=static/output.html'>Import</a>" | |
| with open(file_path, "w") as file: | |
| file.write(ChatGPT_reply) | |
| return recipe_name_str, ingredients_str, steps_str, import_link_str | |
| html_explainer = ''' | |
| <h1>This is the Recipe Cleaner:</h1> | |
| <ol> | |
| <li>Take a picture of the text of your recipe (from a magazine example)</li> | |
| <li>Using the text recognition feature of your phone, copy the text</li> | |
| <li>Paste the text on the field: Recipe Text</li> | |
| <li>Click on Submit</li> | |
| <li>If you click import after receiving the results, the recipe can be imported in a compatible format with your favorite recipe app's browser extension.</li> | |
| </ol> | |
| ''' | |
| gradio_input_html_explainer= gradio.HTML(html_explainer) | |
| gradio_txt_input_recipe_content = gradio.Textbox(label="Recipe Text", lines=2, placeholder="Add here the text of the picture of your recipe...") | |
| gradio_input_image = gradio.Image(shape=(400, 300),type="filepath", label="Recipe Image") | |
| gradio_txt_output_recipe_name = gradio.Textbox(label="Recipe Name", lines=1, placeholder="Recipe Name...") | |
| gradio_txt_output_ingredients = gradio.Textbox(label="Ingredients", lines=2, placeholder="Ingredients...") | |
| gradio_txt_output_steps = gradio.Textbox(label="Preparation Steps", lines=2, placeholder="Steps...") | |
| demo = gradio.Interface( | |
| fn=CustomChatGPT, | |
| inputs=[gradio_input_html_explainer, gradio_txt_input_recipe_content], | |
| outputs=[gradio_txt_output_recipe_name, gradio_txt_output_ingredients,gradio_txt_output_steps,gradio.HTML()], | |
| title="Recipe Cleaner", | |
| allow_flagging="never" | |
| ) | |
| demo.launch() | |
| # demo.launch(share=True) | |