Spaces:
Sleeping
Sleeping
File size: 4,123 Bytes
be9b3a5 4d77a7f 8dccf26 4d77a7f 8dccf26 be9b3a5 8dccf26 be9b3a5 4d77a7f be9b3a5 4d77a7f be9b3a5 4d77a7f be9b3a5 4d77a7f be9b3a5 4d77a7f be9b3a5 4d77a7f be9b3a5 94dcc69 be9b3a5 4d77a7f be9b3a5 4d77a7f 8dccf26 be9b3a5 4d77a7f 8dccf26 4d77a7f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# 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)
|