Spaces:
Runtime error
Runtime error
Add an alternative for st-tags
Browse files- .gitignore +3 -0
- config.json +207 -1
- images/chef-transformer-transparent.png +0 -0
- requirements.txt +1 -2
- server.py +44 -25
.gitignore
CHANGED
|
@@ -1 +1,4 @@
|
|
| 1 |
__pycache__/
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
__pycache__/
|
| 2 |
+
.idea
|
| 3 |
+
.DS_Store
|
| 4 |
+
.DS_store
|
config.json
CHANGED
|
@@ -1 +1,207 @@
|
|
| 1 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_name_or_path": "/Users/m3hrdadfi/Projects/HF/hfflax/hub/t5-recipe-generation",
|
| 3 |
+
"first_100": [
|
| 4 |
+
"allspice",
|
| 5 |
+
"almond extract",
|
| 6 |
+
"applesauce",
|
| 7 |
+
"avocado",
|
| 8 |
+
"balsamic vinegar",
|
| 9 |
+
"basil",
|
| 10 |
+
"bay leaf",
|
| 11 |
+
"beets",
|
| 12 |
+
"bread crumbs",
|
| 13 |
+
"bread flour",
|
| 14 |
+
"buns",
|
| 15 |
+
"catsup",
|
| 16 |
+
"cayenne",
|
| 17 |
+
"cherry tomatoes",
|
| 18 |
+
"chicken breasts",
|
| 19 |
+
"chives",
|
| 20 |
+
"chocolate cake",
|
| 21 |
+
"coconut milk",
|
| 22 |
+
"cold butter",
|
| 23 |
+
"cold milk",
|
| 24 |
+
"cooking oil",
|
| 25 |
+
"cornstarch",
|
| 26 |
+
"crab meat",
|
| 27 |
+
"crackers",
|
| 28 |
+
"cream of chicken soup",
|
| 29 |
+
"cream of tartar",
|
| 30 |
+
"cumin",
|
| 31 |
+
"cumin seeds",
|
| 32 |
+
"curry powder",
|
| 33 |
+
"egg yolk",
|
| 34 |
+
"extra-virgin olive oil",
|
| 35 |
+
"feta cheese",
|
| 36 |
+
"flaked coconut",
|
| 37 |
+
"flat leaf parsley",
|
| 38 |
+
"flour tortillas",
|
| 39 |
+
"fresh chives",
|
| 40 |
+
"fresh cilantro",
|
| 41 |
+
"fresh mint",
|
| 42 |
+
"fresh oregano",
|
| 43 |
+
"fresh rosemary",
|
| 44 |
+
"frozen strawberries",
|
| 45 |
+
"gingerroot",
|
| 46 |
+
"green olives",
|
| 47 |
+
"ground allspice",
|
| 48 |
+
"ground chuck",
|
| 49 |
+
"ground coriander",
|
| 50 |
+
"ground cumin",
|
| 51 |
+
"ground pork",
|
| 52 |
+
"ground red pepper",
|
| 53 |
+
"hamburger",
|
| 54 |
+
"hazelnuts",
|
| 55 |
+
"heavy cream",
|
| 56 |
+
"heavy whipping cream",
|
| 57 |
+
"hot pepper",
|
| 58 |
+
"italian dressing",
|
| 59 |
+
"lean ground beef",
|
| 60 |
+
"lemon juice",
|
| 61 |
+
"lemon pepper",
|
| 62 |
+
"marjoram",
|
| 63 |
+
"miracle",
|
| 64 |
+
"noodles",
|
| 65 |
+
"nuts",
|
| 66 |
+
"oatmeal",
|
| 67 |
+
"oats",
|
| 68 |
+
"oleo",
|
| 69 |
+
"olive oil",
|
| 70 |
+
"onion salt",
|
| 71 |
+
"onions",
|
| 72 |
+
"orange",
|
| 73 |
+
"paprika",
|
| 74 |
+
"parmesan cheese",
|
| 75 |
+
"parsley",
|
| 76 |
+
"pasta",
|
| 77 |
+
"peaches",
|
| 78 |
+
"pecans",
|
| 79 |
+
"pork sausage",
|
| 80 |
+
"pork tenderloin",
|
| 81 |
+
"poultry seasoning",
|
| 82 |
+
"powdered sugar",
|
| 83 |
+
"pumpkin",
|
| 84 |
+
"red potatoes",
|
| 85 |
+
"red wine vinegar",
|
| 86 |
+
"rosemary",
|
| 87 |
+
"salmon",
|
| 88 |
+
"scallion",
|
| 89 |
+
"sesame oil",
|
| 90 |
+
"shell",
|
| 91 |
+
"stalks celery",
|
| 92 |
+
"tabasco sauce",
|
| 93 |
+
"tarragon",
|
| 94 |
+
"tomatoes",
|
| 95 |
+
"unsalted butter",
|
| 96 |
+
"vanilla wafers",
|
| 97 |
+
"vegetables",
|
| 98 |
+
"warm water",
|
| 99 |
+
"whipping cream",
|
| 100 |
+
"white wine vinegar",
|
| 101 |
+
"whole wheat flour",
|
| 102 |
+
"yellow squash",
|
| 103 |
+
"yogurt"
|
| 104 |
+
],
|
| 105 |
+
"next_100": [
|
| 106 |
+
"active dry yeast",
|
| 107 |
+
"almonds",
|
| 108 |
+
"apple",
|
| 109 |
+
"apple cider",
|
| 110 |
+
"apple cider vinegar",
|
| 111 |
+
"avocados",
|
| 112 |
+
"baby spinach",
|
| 113 |
+
"bay leaves",
|
| 114 |
+
"bean sprouts",
|
| 115 |
+
"beef",
|
| 116 |
+
"beef broth",
|
| 117 |
+
"broccoli",
|
| 118 |
+
"cabbage",
|
| 119 |
+
"capers",
|
| 120 |
+
"cashews",
|
| 121 |
+
"celery",
|
| 122 |
+
"celery salt",
|
| 123 |
+
"cherries",
|
| 124 |
+
"cherry pie filling",
|
| 125 |
+
"chicken",
|
| 126 |
+
"chicken broth",
|
| 127 |
+
"chicken stock",
|
| 128 |
+
"chickpeas",
|
| 129 |
+
"chili sauce",
|
| 130 |
+
"chocolate",
|
| 131 |
+
"cinnamon",
|
| 132 |
+
"cloves",
|
| 133 |
+
"corn",
|
| 134 |
+
"cottage cheese",
|
| 135 |
+
"cranberry sauce",
|
| 136 |
+
"egg noodles",
|
| 137 |
+
"egg yolks",
|
| 138 |
+
"extra virgin olive oil",
|
| 139 |
+
"freshly ground pepper",
|
| 140 |
+
"garlic powder",
|
| 141 |
+
"golden raisins",
|
| 142 |
+
"graham cracker crust",
|
| 143 |
+
"graham crackers",
|
| 144 |
+
"green peppers",
|
| 145 |
+
"ground black pepper",
|
| 146 |
+
"ground nutmeg",
|
| 147 |
+
"ground pepper",
|
| 148 |
+
"ground turmeric",
|
| 149 |
+
"kosher salt",
|
| 150 |
+
"lemon rind",
|
| 151 |
+
"mango",
|
| 152 |
+
"mint",
|
| 153 |
+
"mustard",
|
| 154 |
+
"nutmeg",
|
| 155 |
+
"orange juice",
|
| 156 |
+
"orange zest",
|
| 157 |
+
"oregano",
|
| 158 |
+
"peanut butter",
|
| 159 |
+
"peas",
|
| 160 |
+
"pecan halves",
|
| 161 |
+
"pepperoni",
|
| 162 |
+
"pine nuts",
|
| 163 |
+
"pinto beans",
|
| 164 |
+
"pizza sauce",
|
| 165 |
+
"plain yogurt",
|
| 166 |
+
"potatoes",
|
| 167 |
+
"raisins",
|
| 168 |
+
"red",
|
| 169 |
+
"red bell peppers",
|
| 170 |
+
"red pepper",
|
| 171 |
+
"red peppers",
|
| 172 |
+
"rhubarb",
|
| 173 |
+
"ricotta cheese",
|
| 174 |
+
"salad oil",
|
| 175 |
+
"sauce",
|
| 176 |
+
"scallions",
|
| 177 |
+
"sesame seeds",
|
| 178 |
+
"sherry",
|
| 179 |
+
"shredded cheese",
|
| 180 |
+
"skinless",
|
| 181 |
+
"soda",
|
| 182 |
+
"soy sauce",
|
| 183 |
+
"spinach",
|
| 184 |
+
"strawberries",
|
| 185 |
+
"sugar",
|
| 186 |
+
"sweet onion",
|
| 187 |
+
"sweet potatoes",
|
| 188 |
+
"swiss cheese",
|
| 189 |
+
"t",
|
| 190 |
+
"tomato",
|
| 191 |
+
"tomato paste",
|
| 192 |
+
"tomato soup",
|
| 193 |
+
"tuna",
|
| 194 |
+
"vanilla bean",
|
| 195 |
+
"vanilla ice cream",
|
| 196 |
+
"vanilla pudding",
|
| 197 |
+
"vegetable broth",
|
| 198 |
+
"vegetable oil",
|
| 199 |
+
"vegetable shortening",
|
| 200 |
+
"whipped cream",
|
| 201 |
+
"white onion",
|
| 202 |
+
"white sugar",
|
| 203 |
+
"yellow cake",
|
| 204 |
+
"yellow cornmeal",
|
| 205 |
+
"zucchini"
|
| 206 |
+
]
|
| 207 |
+
}
|
images/chef-transformer-transparent.png
ADDED
|
requirements.txt
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
streamlit==0.82.0
|
| 3 |
transformers==4.8.2
|
| 4 |
torch==1.9.0
|
| 5 |
streamlit-tags==1.2.6
|
|
|
|
| 1 |
+
streamlit==0.84.1
|
|
|
|
| 2 |
transformers==4.8.2
|
| 3 |
torch==1.9.0
|
| 4 |
streamlit-tags==1.2.6
|
server.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 2 |
-
from datetime import datetime as dt
|
| 3 |
import streamlit as st
|
| 4 |
from streamlit_tags import st_tags
|
| 5 |
import beam_search
|
|
@@ -12,63 +12,84 @@ with open("config.json") as f:
|
|
| 12 |
|
| 13 |
st.set_page_config(layout="wide")
|
| 14 |
|
|
|
|
| 15 |
@st.cache(allow_output_mutation=True)
|
| 16 |
def load_model():
|
| 17 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
| 18 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("
|
| 19 |
generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 20 |
return generator, tokenizer
|
| 21 |
|
|
|
|
| 22 |
def sampling_changed(obj):
|
| 23 |
print(obj)
|
| 24 |
-
|
| 25 |
|
| 26 |
with st.spinner('Loading model...'):
|
| 27 |
generator, tokenizer = load_model()
|
| 28 |
# st.image("images/chef-transformer.png", width=400)
|
| 29 |
-
st.header("Chef
|
| 30 |
-
st.markdown(
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
add_text_sidebar = st.sidebar.title("Popular recipes:")
|
| 33 |
add_text_sidebar = st.sidebar.text("Recipe preset(example#1)")
|
| 34 |
add_text_sidebar = st.sidebar.text("Recipe preset(example#2)")
|
| 35 |
|
| 36 |
add_text_sidebar = st.sidebar.title("Mode:")
|
| 37 |
-
sampling_mode = st.sidebar.selectbox("select a Mode", index=0, options=["
|
| 38 |
-
|
| 39 |
|
| 40 |
-
original_keywords = st.multiselect(
|
|
|
|
| 41 |
cfg["first_100"],
|
| 42 |
["parmesan cheese", "fresh oregano", "basil", "whole wheat flour"]
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
all_ingredients = []
|
| 54 |
all_ingredients.extend(original_keywords)
|
| 55 |
all_ingredients.extend(custom_keywords)
|
| 56 |
all_ingredients = ", ".join(all_ingredients)
|
| 57 |
-
st.markdown("**Generate recipe for:** "+all_ingredients)
|
| 58 |
-
|
| 59 |
|
| 60 |
submit = st.button('Get Recipe!')
|
| 61 |
if submit:
|
| 62 |
with st.spinner('Generating recipe...'):
|
| 63 |
if sampling_mode == "Beam Search":
|
| 64 |
-
generated = generator(all_ingredients, return_tensors=True, return_text=False,
|
|
|
|
| 65 |
outputs = beam_search.post_generator(generated, tokenizer)
|
| 66 |
elif sampling_mode == "Top-k Sampling":
|
| 67 |
-
generated = generator(all_ingredients, return_tensors=True, return_text=False,
|
|
|
|
| 68 |
outputs = top_sampling.post_generator(generated, tokenizer)
|
| 69 |
output = outputs[0]
|
|
|
|
| 70 |
markdown_output = ""
|
| 71 |
-
markdown_output += f"## {output['title']
|
| 72 |
markdown_output += f"#### Ingredients:\n"
|
| 73 |
for o in output["ingredients"]:
|
| 74 |
markdown_output += f"- {o}\n"
|
|
@@ -76,5 +97,3 @@ if submit:
|
|
| 76 |
for o in output["directions"]:
|
| 77 |
markdown_output += f"- {o}\n"
|
| 78 |
st.markdown(markdown_output)
|
| 79 |
-
st.balloons()
|
| 80 |
-
|
|
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 2 |
+
from datetime import datetime as dt
|
| 3 |
import streamlit as st
|
| 4 |
from streamlit_tags import st_tags
|
| 5 |
import beam_search
|
|
|
|
| 12 |
|
| 13 |
st.set_page_config(layout="wide")
|
| 14 |
|
| 15 |
+
|
| 16 |
@st.cache(allow_output_mutation=True)
|
| 17 |
def load_model():
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(cfg["model_name_or_path"])
|
| 19 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(cfg["model_name_or_path"])
|
| 20 |
generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 21 |
return generator, tokenizer
|
| 22 |
|
| 23 |
+
|
| 24 |
def sampling_changed(obj):
|
| 25 |
print(obj)
|
| 26 |
+
|
| 27 |
|
| 28 |
with st.spinner('Loading model...'):
|
| 29 |
generator, tokenizer = load_model()
|
| 30 |
# st.image("images/chef-transformer.png", width=400)
|
| 31 |
+
st.header("Chef Transformer π©βπ³ / π¨βπ³")
|
| 32 |
+
st.markdown(
|
| 33 |
+
"This demo uses [T5 trained on recipe-nlg](https://huggingface.co/flax-community/t5-recipe-generation) "
|
| 34 |
+
"to generate recipe from a given set of ingredients"
|
| 35 |
+
)
|
| 36 |
+
img = st.sidebar.image("images/chef-transformer-transparent.png", width=310)
|
| 37 |
add_text_sidebar = st.sidebar.title("Popular recipes:")
|
| 38 |
add_text_sidebar = st.sidebar.text("Recipe preset(example#1)")
|
| 39 |
add_text_sidebar = st.sidebar.text("Recipe preset(example#2)")
|
| 40 |
|
| 41 |
add_text_sidebar = st.sidebar.title("Mode:")
|
| 42 |
+
sampling_mode = st.sidebar.selectbox("select a Mode", index=0, options=["Top Sampling", "Beam Search"])
|
|
|
|
| 43 |
|
| 44 |
+
original_keywords = st.multiselect(
|
| 45 |
+
"Choose ingredients",
|
| 46 |
cfg["first_100"],
|
| 47 |
["parmesan cheese", "fresh oregano", "basil", "whole wheat flour"]
|
| 48 |
)
|
| 49 |
|
| 50 |
+
|
| 51 |
+
# st.write("Add custom ingredients here:")
|
| 52 |
+
# custom_keywords = st_tags(
|
| 53 |
+
# label="",
|
| 54 |
+
# text='Press enter to add more',
|
| 55 |
+
# value=['salt'],
|
| 56 |
+
# suggestions=["z"],
|
| 57 |
+
# maxtags=15,
|
| 58 |
+
# key='1')
|
| 59 |
+
|
| 60 |
+
def custom_keywords_on_change():
|
| 61 |
+
pass
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
custom_keywords = st.text_input(
|
| 65 |
+
'Add custom ingredients here (separated by `,`): ',
|
| 66 |
+
", ".join(["salt", "pepper"]),
|
| 67 |
+
key="custom_keywords",
|
| 68 |
+
on_change=custom_keywords_on_change,
|
| 69 |
+
max_chars=1000)
|
| 70 |
+
custom_keywords = list(set([x.strip() for x in custom_keywords.strip().split(',') if len(x.strip()) > 0]))
|
| 71 |
+
|
| 72 |
all_ingredients = []
|
| 73 |
all_ingredients.extend(original_keywords)
|
| 74 |
all_ingredients.extend(custom_keywords)
|
| 75 |
all_ingredients = ", ".join(all_ingredients)
|
| 76 |
+
st.markdown("**Generate recipe for:** " + all_ingredients)
|
|
|
|
| 77 |
|
| 78 |
submit = st.button('Get Recipe!')
|
| 79 |
if submit:
|
| 80 |
with st.spinner('Generating recipe...'):
|
| 81 |
if sampling_mode == "Beam Search":
|
| 82 |
+
generated = generator(all_ingredients, return_tensors=True, return_text=False,
|
| 83 |
+
**beam_search.generate_kwargs)
|
| 84 |
outputs = beam_search.post_generator(generated, tokenizer)
|
| 85 |
elif sampling_mode == "Top-k Sampling":
|
| 86 |
+
generated = generator(all_ingredients, return_tensors=True, return_text=False,
|
| 87 |
+
**top_sampling.generate_kwargs)
|
| 88 |
outputs = top_sampling.post_generator(generated, tokenizer)
|
| 89 |
output = outputs[0]
|
| 90 |
+
output['title'] = " ".join([w.capitalize() for w in output['title'].split()])
|
| 91 |
markdown_output = ""
|
| 92 |
+
markdown_output += f"## {output['title']}\n"
|
| 93 |
markdown_output += f"#### Ingredients:\n"
|
| 94 |
for o in output["ingredients"]:
|
| 95 |
markdown_output += f"- {o}\n"
|
|
|
|
| 97 |
for o in output["directions"]:
|
| 98 |
markdown_output += f"- {o}\n"
|
| 99 |
st.markdown(markdown_output)
|
|
|
|
|
|