File size: 2,719 Bytes
a6b7b32
 
 
 
 
 
 
 
 
 
 
 
dea317b
3e1c1d6
a6b7b32
 
 
 
 
 
 
 
 
 
 
 
3b41222
3e1c1d6
a6b7b32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b93d5b
a6b7b32
 
 
 
0b93d5b
 
 
 
a6b7b32
 
 
 
 
 
 
 
 
 
62c6339
a6b7b32
 
 
 
 
 
 
 
 
 
 
 
 
62c6339
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
from dis import Instruction
import requests
from decouple import config
import gradio as gr
import spacy

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

RAPIDAPI_KEY = config('RAPIDAPI_KEY')

nlp = 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(instructions, ingredients):
    doc = nlp(instructions)
    print(doc)
    
    ingredients_list = []

    # TODO: add lemmatizer
    for item in doc:
        if item[1] == 'INGREDIENT':
            if item[0] not in ingredients_list:
                ingredients_list.append(item[0])

    ingredients = ""

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

    return ingredients

with gr.Blocks() as demo:
    gr.Markdown("# Reciparse Visualizer")
    gr.Markdown("### Powered by spaCy and Gradio")
    with gr.Box():
        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")
            ingredient_list = gr.Textbox(label="Ingredients in recipe")

    recipe_btn.click(fn=get_recipe_data, inputs=recipe_link, outputs=[instructions, ingredients, recipe_title])
    ingredient_btn.click(fn=get_ingredient_data, inputs=[instructions, ingredients], outputs=[ingredient_list])

demo.launch()