File size: 4,480 Bytes
a6b7b32
 
 
 
 
dfc433d
a6b7b32
 
 
 
 
 
 
dfc433d
3e1c1d6
a6b7b32
 
 
 
 
 
 
 
 
 
 
 
3b41222
3e1c1d6
a6b7b32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfc433d
 
 
 
a6b7b32
 
 
0b93d5b
 
dfc433d
 
 
a6b7b32
dfc433d
a6b7b32
 
dfc433d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6b7b32
dfc433d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6b7b32
 
 
62c6339
a6b7b32
dfc433d
a6b7b32
 
 
 
 
 
 
 
 
 
 
 
62c6339
a6b7b32
dfc433d
 
 
 
 
 
a6b7b32
 
dfc433d
 
 
a6b7b32
 
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
119
120
121
122
123
124
125
126
127
128
129
from dis import Instruction
import requests
from decouple import config
import gradio as gr
import spacy
import re

# for generating requirements.txt, use:
# pip install pipreqs
# pipreqs ./ --force

RAPIDAPI_KEY = config('RAPIDAPI_KEY')

ner = gr.Blocks.load(name='models/victorialslocum/en_reciparse_model')

def get_recipe_data(recipe_url):
    rapid_api_url = "https://mycookbook-io1.p.rapidapi.com/recipes/rapidapi"

    headers = {
        "content-type": "text/plain",
        "X-RapidAPI-Host": "mycookbook-io1.p.rapidapi.com",
        "X-RapidAPI-Key": RAPIDAPI_KEY
    }

    response = requests.request("POST", rapid_api_url,
                                data=recipe_url, headers=headers).json()

    # print(response)

    instructions_list = response[0]['instructions'][0]['steps']
    ingredients_list = response[0]['ingredients']
    recipe_title = response[0]['name']

    instructions = ""
    ingredients = ""

    for i in range(len(instructions_list)):
        instructions += f'{i+1}. {instructions_list[i]} \n'

    for i in range(len(ingredients_list)):
        ingredients += f'{i+1}. {ingredients_list[i]} \n'

    return instructions, ingredients, recipe_title

def get_ingredient_data(ingredients):
    nlp = spacy.load("en_core_web_sm")

    doc = ner(ingredients)
    
    ingredients_list = []

    for item in doc:
        if item[1] == 'INGREDIENT':
            item_lemma = ' ' .join([tok.lemma_ for tok in nlp(item[0])])
            if item_lemma not in ingredients_list:
                ingredients_list.append(item_lemma)

    ingredients_out = ""

    for i in range(len(ingredients_list)):
        ingredients_out += f'{i+1}. {ingredients_list[i]} \n'

    return ingredients_out

def get_instruction_data(instructions):
    nlp = spacy.load("en_core_web_sm")

    doc = ner(instructions)
    
    ingredients_list = []

    for item in doc:
        if item[1] == 'INGREDIENT':
            item_lemma = ' ' .join([tok.lemma_ for tok in nlp(item[0])])
            if item_lemma not in ingredients_list:
                ingredients_list.append(item_lemma)

    ingredients_out = ""

    for i in range(len(ingredients_list)):
        ingredients_out += f'{i+1}. {ingredients_list[i]} \n'

    return ingredients_out

def final_ingredients(inst_ingredients_list, ingr_ingredients_list):
    instructions_guess = [re.sub(r'[0-9]+', '', item)[2:-1] for item in inst_ingredients_list.split('\n')if len(item) > 3]
    ingredients_guess = [re.sub(r'[0-9]+', '', item)[2:-1] for item in ingr_ingredients_list.split('\n')if len(item) > 3]

    final_guess_list = [item for item in instructions_guess if item in ingredients_guess]

    final_guess = ""

    for i in range(len(final_guess_list)):
        final_guess += f'{i+1}. {final_guess_list[i]} \n'

    return final_guess

with gr.Blocks() as demo:
    gr.Markdown("# Reciparse Visualizer")
    gr.Markdown("### Powered by spaCy and Gradio")
    with gr.Box():
        ingredients_list = gr.Variable()
        with gr.Column():
            gr.Markdown("## Recipe Info")
            with gr.Row():
                recipe_link = gr.Textbox(label="Recipe link", placeholder="Input your recipe link")
                recipe_title = gr.Textbox(label="Recipe title")
            with gr.Row():
                instructions = gr.Textbox(label="Instructions")
                ingredients = gr.Textbox(label="Ingredients")
            recipe_btn = gr.Button("Get recipe info")
    
    with gr.Box():
        with gr.Column():
            gr.Markdown("## Ingredient Info")
            ingredient_btn = gr.Button("Get the list of ingredients")
            final_btn = gr.Button("Create the final list")
            with gr.Row():
                ingr_ingredients_list = gr.Textbox(label="Ingredients in recipe based off of ingredients list")
                inst_ingredients_list = gr.Textbox(label="Ingredients in recipe based off of instructions")
                final_ingredients_list = gr.Textbox(label="Final ingredient guess")


    recipe_btn.click(fn=get_recipe_data, inputs=recipe_link, outputs=[instructions, ingredients, recipe_title])
    ingredient_btn.click(fn=get_ingredient_data, inputs=[ingredients], outputs=[ingr_ingredients_list])
    ingredient_btn.click(fn=get_instruction_data, inputs=[instructions], outputs=[inst_ingredients_list])
    final_btn.click(fn=final_ingredients, inputs=[inst_ingredients_list, ingr_ingredients_list], outputs=[final_ingredients_list])

demo.launch()