Views
No views yet
1{
2 "NER": [
3 "oyster crackers",
4 "salad dressing",
5 "lemon pepper",
6 "dill weed",
7 "garlic powder",
8 "salad oil"
9 ],
10 "directions": [
11 "Combine salad dressing mix and oil.",
12 "Add dill weed, garlic powder and lemon pepper.",
13 "Pour over crackers; stir to coat.",
14 "Place in warm oven.",
15 "Use very low temperature for 15 to 20 minutes."
16 ],
17 "ingredients": [
18 "12 to 16 oz. plain oyster crackers",
19 "1 pkg. Hidden Valley Ranch salad dressing mix",
20 "1/4 tsp. lemon pepper",
21 "1/2 to 1 tsp. dill weed",
22 "1/4 tsp. garlic powder",
23 "3/4 to 1 c. salad oil"
24 ],
25 "link": "www.cookbooks.com/Recipe-Details.aspx?id=648947",
26 "source": "Gathered",
27 "title": "Hidden Valley Ranch Oyster Crackers"
28}1# Installing requirements
2pip install transformers1from transformers import FlaxAutoModelForSeq2SeqLM
2from transformers import AutoTokenizer
3
4MODEL_NAME_OR_PATH = "JustAPR/resGen"
5tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME_OR_PATH, use_fast=True)
6model = FlaxAutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME_OR_PATH)
7
8prefix = "items: "
9# generation_kwargs = {
10# "max_length": 512,
11# "min_length": 64,
12# "no_repeat_ngram_size": 3,
13# "early_stopping": True,
14# "num_beams": 5,
15# "length_penalty": 1.5,
16# }
17generation_kwargs = {
18 "max_length": 512,
19 "min_length": 64,
20 "no_repeat_ngram_size": 3,
21 "do_sample": True,
22 "top_k": 60,
23 "top_p": 0.95
24}
25
26
27special_tokens = tokenizer.all_special_tokens
28tokens_map = {
29 "<sep>": "--",
30 "<section>": "\n"
31}
32def skip_special_tokens(text, special_tokens):
33 for token in special_tokens:
34 text = text.replace(token, "")
35
36 return text
37
38def target_postprocessing(texts, special_tokens):
39 if not isinstance(texts, list):
40 texts = [texts]
41
42 new_texts = []
43 for text in texts:
44 text = skip_special_tokens(text, special_tokens)
45
46 for k, v in tokens_map.items():
47 text = text.replace(k, v)
48
49 new_texts.append(text)
50
51 return new_texts
52
53def generation_function(texts):
54 _inputs = texts if isinstance(texts, list) else [texts]
55 inputs = [prefix + inp for inp in _inputs]
56 inputs = tokenizer(
57 inputs,
58 max_length=256,
59 padding="max_length",
60 truncation=True,
61 return_tensors="jax"
62 )
63
64 input_ids = inputs.input_ids
65 attention_mask = inputs.attention_mask
66
67 output_ids = model.generate(
68 input_ids=input_ids,
69 attention_mask=attention_mask,
70 **generation_kwargs
71 )
72 generated = output_ids.sequences
73 generated_recipe = target_postprocessing(
74 tokenizer.batch_decode(generated, skip_special_tokens=False),
75 special_tokens
76 )
77 return generated_recipe1items = []
2a = input()
3for x in range(3):#to generate 3 recipies on given ingridents
4 items.append(a)
5
6generated = generation_function(items)
7for text in generated:
8 sections = text.split("\n")
9 for section in sections:
10 section = section.strip()
11 if section.startswith("title:"):
12 section = section.replace("title:", "")
13 headline = "TITLE"
14 elif section.startswith("ingredients:"):
15 section = section.replace("ingredients:", "")
16 headline = "INGREDIENTS"
17 elif section.startswith("directions:"):
18 section = section.replace("directions:", "")
19 headline = "DIRECTIONS"
20
21 if headline == "TITLE":
22 print(f"[{headline}]: {section.strip().capitalize()}")
23 else:
24 section_info = [f" - {i+1}: {info.strip().capitalize()}" for i, info in enumerate(section.split("--"))]
25 print(f"[{headline}]:")
26 print("\n".join(section_info))
27
28 print("-" * 130)1[TITLE]: Macaroni and corn
2[INGREDIENTS]:
3 - 1: 2 c. macaroni
4 - 2: 2 tbsp. butter
5 - 3: 1 tsp. salt
6 - 4: 4 slices bacon
7 - 5: 2 c. milk
8 - 6: 2 tbsp. flour
9 - 7: 1/4 tsp. pepper
10 - 8: 1 can cream corn
11[DIRECTIONS]:
12 - 1: Cook macaroni in boiling salted water until tender.
13 - 2: Drain.
14 - 3: Melt butter in saucepan.
15 - 4: Blend in flour, salt and pepper.
16 - 5: Add milk all at once.
17 - 6: Cook and stir until thickened and bubbly.
18 - 7: Stir in corn and bacon.
19 - 8: Pour over macaroni and mix well.
20----------------------------------------------------------------------------------------------------------------------------------
21[TITLE]: Grilled provolone and bacon sandwich
22[INGREDIENTS]:
23 - 1: 2 slices provolone cheese
24 - 2: 2 slices bacon
25 - 3: 2 slices sourdough bread
26 - 4: 2 slices pickled ginger
27[DIRECTIONS]:
28 - 1: Place a slice of provolone cheese on one slice of bread.
29 - 2: Top with a slice of bacon.
30 - 3: Top with a slice of pickled ginger.
31 - 4: Top with the other slice of bread.
32 - 5: Heat a skillet over medium heat.
33 - 6: Place the sandwich in the skillet and cook until the cheese is melted and the bread is golden brown.
34----------------------------------------------------------------------------------------------------------------------------------