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_data(data): nlp = spacy.load("en_core_web_sm") doc = ner(data) 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 = [] for ingr_item in ingredients_guess: for inst_item in instructions_guess: if inst_item in ingr_item and ingr_item not in final_guess_list: final_guess_list.append(ingr_item) 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_data, inputs=[ingredients], outputs=[ingr_ingredients_list]) ingredient_btn.click(fn=get_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()