readme: ---
  language: en
  tags:
  - seq2seq
  - t5
  - text-generation
  - recipe-generation
  pipeline_tag: text2text-generation
  widget:
    - text: "provolone cheese, bacon, bread, ginger"
    - text: "sugar, crunchy jif peanut butter, cornflakes"
    - text: "sweet butter, confectioners sugar, flaked coconut, condensed milk, nuts, vanilla, dipping chocolate"
    - text: "macaroni, butter, salt, bacon, milk, flour, pepper, cream corn"
    - text: "hamburger, sausage, onion, regular, american cheese, colby cheese"
    - text: "chicken breasts, onion, garlic, great northern beans, black beans, green chilies, broccoli, garlic oil, butter, cajun seasoning, salt, oregano, thyme, black pepper, basil, worcestershire sauce, chicken broth, sour cream, chardonnay wine"
    - text: "serrano peppers, garlic, celery, oregano, canola oil, vinegar, water, kosher salt, salt, black pepper"
  ---
  
  ![avatar](chef-transformer.png)
  
  # Chef Transformer (T5) 
  > This is part of the
  [Flax/Jax Community Week](https://discuss.huggingface.co/t/recipe-generation-model/7475), organized by [HuggingFace](https://huggingface.co/) and TPU usage sponsored by Google.
  
  Want to give it a try? Then what's the wait, head over to Hugging Face Spaces [here](https://huggingface.co/spaces/flax-community/chef-transformer).
  
  
  ## Team Members
  - Mehrdad Farahani ([m3hrdadfi](https://huggingface.co/m3hrdadfi))
  - Kartik Godawat ([dk-crazydiv](https://huggingface.co/dk-crazydiv))
  - Haswanth Aekula ([hassiahk](https://huggingface.co/hassiahk))
  - Deepak Pandian ([rays2pix](https://huggingface.co/rays2pix))
  - Nicholas Broad ([nbroad](https://huggingface.co/nbroad))
  
  ## Dataset
  
  [RecipeNLG: A Cooking Recipes Dataset for Semi-Structured Text Generation](https://recipenlg.cs.put.poznan.pl/). This dataset contains **2,231,142** cooking recipes (>2 millions) with size of **2.14 GB**. It's processed in more careful way.
  
  ### Example
  
  ```json
  {
    "NER": [
      "oyster crackers",
      "salad dressing",
      "lemon pepper",
      "dill weed",
      "garlic powder",
      "salad oil"
    ],
    "directions": [
      "Combine salad dressing mix and oil.",
      "Add dill weed, garlic powder and lemon pepper.",
      "Pour over crackers; stir to coat.",
      "Place in warm oven.",
      "Use very low temperature for 15 to 20 minutes."
    ],
    "ingredients": [
      "12 to 16 oz. plain oyster crackers",
      "1 pkg. Hidden Valley Ranch salad dressing mix",
      "1/4 tsp. lemon pepper",
      "1/2 to 1 tsp. dill weed",
      "1/4 tsp. garlic powder",
      "3/4 to 1 c. salad oil"
    ],
    "link": "www.cookbooks.com/Recipe-Details.aspx?id=648947",
    "source": "Gathered",
    "title": "Hidden Valley Ranch Oyster Crackers"
  }
  ```
  
  ## How To Use
  
  ```bash
  # Installing requirements
  pip install transformers
  ```
  
  ```python
  from transformers import FlaxAutoModelForSeq2SeqLM
  from transformers import AutoTokenizer
  
  MODEL_NAME_OR_PATH = "flax-community/t5-recipe-generation"
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME_OR_PATH, use_fast=True)
  model = FlaxAutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME_OR_PATH)
  
  prefix = "items: "
  # generation_kwargs = {
  #     "max_length": 512,
  #     "min_length": 64,
  #     "no_repeat_ngram_size": 3,
  #     "early_stopping": True,
  #     "num_beams": 5,
  #     "length_penalty": 1.5,
  # }
  generation_kwargs = {
      "max_length": 512,
      "min_length": 64,
      "no_repeat_ngram_size": 3,
      "do_sample": True,
      "top_k": 60,
      "top_p": 0.95
  }
  
  
  special_tokens = tokenizer.all_special_tokens
  tokens_map = {
      "<sep>": "--",
      "<section>": "\n"
  }
  def skip_special_tokens(text, special_tokens):
      for token in special_tokens:
          text = text.replace(token, "")
  
      return text
  
  def target_postprocessing(texts, special_tokens):
      if not isinstance(texts, list):
          texts = [texts]
      
      new_texts = []
      for text in texts:
          text = skip_special_tokens(text, special_tokens)
  
          for k, v in tokens_map.items():
              text = text.replace(k, v)
  
          new_texts.append(text)
  
      return new_texts
  
  def generation_function(texts):
      _inputs = texts if isinstance(texts, list) else [texts]
      inputs = [prefix + inp for inp in _inputs]
      inputs = tokenizer(
          inputs, 
          max_length=256, 
          padding="max_length", 
          truncation=True, 
          return_tensors="jax"
      )
  
      input_ids = inputs.input_ids
      attention_mask = inputs.attention_mask
  
      output_ids = model.generate(
          input_ids=input_ids, 
          attention_mask=attention_mask,
          **generation_kwargs
      )
      generated = output_ids.sequences
      generated_recipe = target_postprocessing(
          tokenizer.batch_decode(generated, skip_special_tokens=False),
          special_tokens
      )
      return generated_recipe
  ```
  
  ```python
  items = [
      "macaroni, butter, salt, bacon, milk, flour, pepper, cream corn",
      "provolone cheese, bacon, bread, ginger"
  ]
  generated = generation_function(items)
  for text in generated:
      sections = text.split("\n")
      for section in sections:
          section = section.str