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/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) ingredients_list = [] # TODO: add lemmatizer for ent in doc.ents: if ent not in ingredients_list: ingredients_list.append(ent) 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()