# 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 = '''
Mom's World Famous Banana Bread
This classic banana bread recipe comes
from my mom.
3 or 4 ripe bananas, smashed
3/4 cup of sugar
1 - Preheat the oven to 350 degrees.
2 - Mix in the ingredients in a bowl.
'''
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 = "Import"
with open(file_path, "w") as file:
file.write(ChatGPT_reply)
return recipe_name_str, ingredients_str, steps_str, import_link_str
html_explainer = '''
This is the Recipe Cleaner:
- Take a picture of the text of your recipe (from a magazine example)
- Using the text recognition feature of your phone, copy the text
- Paste the text on the field: Recipe Text
- Click on Submit
- 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.
'''
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)