victorialslocum commited on
Commit
dfc433d
1 Parent(s): 0b93d5b

add features

Browse files
Files changed (2) hide show
  1. .gitignore +2 -1
  2. app.py +55 -12
.gitignore CHANGED
@@ -1 +1,2 @@
1
- .env
 
 
1
+ .env
2
+ test.py
app.py CHANGED
@@ -3,6 +3,7 @@ import requests
3
  from decouple import config
4
  import gradio as gr
5
  import spacy
 
6
 
7
  # for generating requirements.txt, use:
8
  # pip install pipreqs
@@ -10,7 +11,7 @@ import spacy
10
 
11
  RAPIDAPI_KEY = config('RAPIDAPI_KEY')
12
 
13
- nlp = gr.Blocks.load(name='models/victorialslocum/en_reciparse_model')
14
 
15
  def get_recipe_data(recipe_url):
16
  rapid_api_url = "https://mycookbook-io1.p.rapidapi.com/recipes/rapidapi"
@@ -41,29 +42,64 @@ def get_recipe_data(recipe_url):
41
 
42
  return instructions, ingredients, recipe_title
43
 
44
- def get_ingredient_data(instructions, ingredients):
45
- doc = nlp(instructions)
46
- print(doc)
 
47
 
48
  ingredients_list = []
49
 
50
- # TODO: add lemmatizer
51
  for item in doc:
52
  if item[1] == 'INGREDIENT':
53
- if item[0] not in ingredients_list:
54
- ingredients_list.append(item[0])
 
55
 
56
- ingredients = ""
57
 
58
  for i in range(len(ingredients_list)):
59
- ingredients += f'{i+1}. {ingredients_list[i]} \n'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- return ingredients
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  with gr.Blocks() as demo:
64
  gr.Markdown("# Reciparse Visualizer")
65
  gr.Markdown("### Powered by spaCy and Gradio")
66
  with gr.Box():
 
67
  with gr.Column():
68
  gr.Markdown("## Recipe Info")
69
  with gr.Row():
@@ -78,9 +114,16 @@ with gr.Blocks() as demo:
78
  with gr.Column():
79
  gr.Markdown("## Ingredient Info")
80
  ingredient_btn = gr.Button("Get the list of ingredients")
81
- ingredient_list = gr.Textbox(label="Ingredients in recipe")
 
 
 
 
 
82
 
83
  recipe_btn.click(fn=get_recipe_data, inputs=recipe_link, outputs=[instructions, ingredients, recipe_title])
84
- ingredient_btn.click(fn=get_ingredient_data, inputs=[instructions, ingredients], outputs=[ingredient_list])
 
 
85
 
86
  demo.launch()
 
3
  from decouple import config
4
  import gradio as gr
5
  import spacy
6
+ import re
7
 
8
  # for generating requirements.txt, use:
9
  # pip install pipreqs
 
11
 
12
  RAPIDAPI_KEY = config('RAPIDAPI_KEY')
13
 
14
+ ner = gr.Blocks.load(name='models/victorialslocum/en_reciparse_model')
15
 
16
  def get_recipe_data(recipe_url):
17
  rapid_api_url = "https://mycookbook-io1.p.rapidapi.com/recipes/rapidapi"
 
42
 
43
  return instructions, ingredients, recipe_title
44
 
45
+ def get_ingredient_data(ingredients):
46
+ nlp = spacy.load("en_core_web_sm")
47
+
48
+ doc = ner(ingredients)
49
 
50
  ingredients_list = []
51
 
 
52
  for item in doc:
53
  if item[1] == 'INGREDIENT':
54
+ item_lemma = ' ' .join([tok.lemma_ for tok in nlp(item[0])])
55
+ if item_lemma not in ingredients_list:
56
+ ingredients_list.append(item_lemma)
57
 
58
+ ingredients_out = ""
59
 
60
  for i in range(len(ingredients_list)):
61
+ ingredients_out += f'{i+1}. {ingredients_list[i]} \n'
62
+
63
+ return ingredients_out
64
+
65
+ def get_instruction_data(instructions):
66
+ nlp = spacy.load("en_core_web_sm")
67
+
68
+ doc = ner(instructions)
69
+
70
+ ingredients_list = []
71
+
72
+ for item in doc:
73
+ if item[1] == 'INGREDIENT':
74
+ item_lemma = ' ' .join([tok.lemma_ for tok in nlp(item[0])])
75
+ if item_lemma not in ingredients_list:
76
+ ingredients_list.append(item_lemma)
77
 
78
+ ingredients_out = ""
79
+
80
+ for i in range(len(ingredients_list)):
81
+ ingredients_out += f'{i+1}. {ingredients_list[i]} \n'
82
+
83
+ return ingredients_out
84
+
85
+ def final_ingredients(inst_ingredients_list, ingr_ingredients_list):
86
+ instructions_guess = [re.sub(r'[0-9]+', '', item)[2:-1] for item in inst_ingredients_list.split('\n')if len(item) > 3]
87
+ ingredients_guess = [re.sub(r'[0-9]+', '', item)[2:-1] for item in ingr_ingredients_list.split('\n')if len(item) > 3]
88
+
89
+ final_guess_list = [item for item in instructions_guess if item in ingredients_guess]
90
+
91
+ final_guess = ""
92
+
93
+ for i in range(len(final_guess_list)):
94
+ final_guess += f'{i+1}. {final_guess_list[i]} \n'
95
+
96
+ return final_guess
97
 
98
  with gr.Blocks() as demo:
99
  gr.Markdown("# Reciparse Visualizer")
100
  gr.Markdown("### Powered by spaCy and Gradio")
101
  with gr.Box():
102
+ ingredients_list = gr.Variable()
103
  with gr.Column():
104
  gr.Markdown("## Recipe Info")
105
  with gr.Row():
 
114
  with gr.Column():
115
  gr.Markdown("## Ingredient Info")
116
  ingredient_btn = gr.Button("Get the list of ingredients")
117
+ final_btn = gr.Button("Create the final list")
118
+ with gr.Row():
119
+ ingr_ingredients_list = gr.Textbox(label="Ingredients in recipe based off of ingredients list")
120
+ inst_ingredients_list = gr.Textbox(label="Ingredients in recipe based off of instructions")
121
+ final_ingredients_list = gr.Textbox(label="Final ingredient guess")
122
+
123
 
124
  recipe_btn.click(fn=get_recipe_data, inputs=recipe_link, outputs=[instructions, ingredients, recipe_title])
125
+ ingredient_btn.click(fn=get_ingredient_data, inputs=[ingredients], outputs=[ingr_ingredients_list])
126
+ ingredient_btn.click(fn=get_instruction_data, inputs=[instructions], outputs=[inst_ingredients_list])
127
+ final_btn.click(fn=final_ingredients, inputs=[inst_ingredients_list, ingr_ingredients_list], outputs=[final_ingredients_list])
128
 
129
  demo.launch()