Spaces:
Sleeping
Sleeping
Commit
·
be9b3a5
1
Parent(s):
c73eb48
Upload app.py
Browse filesAlterations to add explainer and images
app.py
CHANGED
@@ -1,9 +1,15 @@
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import openai
|
3 |
import gradio
|
4 |
import json
|
5 |
from pathlib import Path
|
6 |
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
7 |
|
8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
9 |
|
@@ -39,42 +45,76 @@ static_dir.mkdir(parents=True, exist_ok=True)
|
|
39 |
|
40 |
|
41 |
|
42 |
-
def CustomChatGPT(recipe):
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
|
|
|
49 |
|
50 |
-
|
51 |
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
ingredients = [item.text.strip() for item in ingredients_html]
|
57 |
-
ingredients_str = "\n".join(ingredients)
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
-
return recipe_name_str, ingredients_str, steps_str, "<a href='file=static/output.html'>Import</a>"
|
68 |
|
|
|
|
|
|
|
69 |
|
70 |
demo = gradio.Interface(
|
71 |
fn=CustomChatGPT,
|
72 |
-
inputs=
|
73 |
-
outputs=[
|
74 |
-
title="Recipe Cleaner"
|
|
|
75 |
)
|
76 |
|
77 |
-
demo.launch()
|
78 |
-
|
79 |
|
80 |
|
|
|
1 |
+
# https://stackoverflow.com/questions/50951955/pytesseract-tesseractnotfound-error-tesseract-is-not-installed-or-its-not-i
|
2 |
+
# https://pysource.com/2020/04/23/text-recognition-ocr-with-tesseract-and-opencv/
|
3 |
+
|
4 |
import os
|
5 |
import openai
|
6 |
import gradio
|
7 |
import json
|
8 |
from pathlib import Path
|
9 |
from bs4 import BeautifulSoup
|
10 |
+
import pytesseract
|
11 |
+
from PIL import Image
|
12 |
+
import numpy as np
|
13 |
|
14 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
15 |
|
|
|
45 |
|
46 |
|
47 |
|
48 |
+
def CustomChatGPT(html_explainer, recipe, image):
|
49 |
+
recipe_name_str=""
|
50 |
+
ingredients_str=""
|
51 |
+
steps_str=""
|
52 |
+
import_link_str = ""
|
53 |
+
|
54 |
+
|
55 |
+
if image is not None and bool(image):
|
56 |
+
recipe = pytesseract.image_to_string(Image.open(image))
|
57 |
+
|
58 |
+
if recipe is not None and bool(recipe):
|
59 |
+
messages.append({"role": "user", "content": recipe})
|
60 |
+
|
61 |
+
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
|
62 |
+
ChatGPT_reply = response["choices"][0]["message"]["content"]
|
63 |
+
|
64 |
+
file_name = "output.html"
|
65 |
+
file_path = static_dir / file_name
|
66 |
+
|
67 |
+
soup = BeautifulSoup(ChatGPT_reply, 'html.parser')
|
68 |
+
|
69 |
+
recipe_name = soup.find(attrs={"itemprop": "name"})
|
70 |
+
recipe_name_str = recipe_name.text
|
71 |
+
|
72 |
+
ingredients_html = soup.find_all(attrs={"itemprop": "recipeIngredient"})
|
73 |
+
ingredients = [item.text.strip() for item in ingredients_html]
|
74 |
+
ingredients_str = "\n".join(ingredients)
|
75 |
|
76 |
+
steps_html = soup.find_all(attrs={"itemprop": "recipeInstructions"})
|
77 |
+
steps = [item.text.strip() for item in steps_html]
|
78 |
+
steps_str = "\n".join(steps)
|
79 |
|
80 |
+
import_link_str = "<a href='file=static/output.html'>Import</a>"
|
81 |
|
82 |
+
with open(file_path, "w") as file:
|
83 |
+
file.write(ChatGPT_reply)
|
84 |
|
85 |
+
return recipe_name_str, ingredients_str, steps_str, import_link_str
|
|
|
|
|
86 |
|
87 |
+
html_explainer = '''
|
88 |
+
<h1>This is the Recipe Cleaner:</h1>
|
89 |
+
<ol>
|
90 |
+
<li>Take a picture of the text of your recipe (from a magazine example)</li>
|
91 |
+
<li>Using the text recognition feature of your phone, copy the text</li>
|
92 |
+
<li>Paste the text on the field: Recipe Text</li>
|
93 |
+
<li>Click on Submit</li>
|
94 |
+
<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>
|
95 |
+
</ol>
|
96 |
+
<p>You might be inclined to upload the image you took. You can do it! But I warn you that this functionality is not working perfectly yet, and it's resulting in funny recipes.</p>
|
97 |
+
'''
|
98 |
|
99 |
|
100 |
+
gradio_input_html_explainer= gradio.HTML(html_explainer)
|
101 |
+
gradio_txt_input_recipe_content = gradio.Textbox(label="Recipe Text", lines=2, placeholder="If you don't have an image, you can add here the text of the picture of your recipe...")
|
102 |
+
gradio_input_image = gradio.Image(shape=(400, 300),type="filepath", label="Recipe Image")
|
103 |
|
|
|
104 |
|
105 |
+
gradio_txt_output_recipe_name = gradio.Textbox(label="Recipe Name", lines=1, placeholder="Recipe Name...")
|
106 |
+
gradio_txt_output_ingredients = gradio.Textbox(label="Ingredients", lines=2, placeholder="Ingredients...")
|
107 |
+
gradio_txt_output_steps = gradio.Textbox(label="Preparation Steps", lines=2, placeholder="Steps...")
|
108 |
|
109 |
demo = gradio.Interface(
|
110 |
fn=CustomChatGPT,
|
111 |
+
inputs=[gradio_input_html_explainer, gradio_txt_input_recipe_content, gradio_input_image],
|
112 |
+
outputs=[gradio_txt_output_recipe_name, gradio_txt_output_ingredients,gradio_txt_output_steps,gradio.HTML()],
|
113 |
+
title="Recipe Cleaner",
|
114 |
+
allow_flagging="never"
|
115 |
)
|
116 |
|
117 |
+
# demo.launch()
|
118 |
+
demo.launch(share=True)
|
119 |
|
120 |
|