class_docstr: The ``TranslationData`` class is a :class:`~flash.core.data.data_module.DataModule` with a set of classmethods
  for loading data for text translation.
docstr: Load the :class:`~flash.text.seq2seq.translation.data.TranslationData` from CSV files containing input text
  snippets and their corresponding target text snippets.
  
  Input text snippets will be extracted from the ``input_field`` column in the CSV files.
  Target text snippets will be extracted from the ``target_field`` column in the CSV files.
  To learn how to customize the transforms applied for each stage, read our
  :ref:`customizing transforms guide <customizing_transforms>`.
  
  Args:
      input_field: The field (column name) in the CSV files containing the input text snippets.
      target_field: The field (column name) in the CSV files containing the target text snippets.
      train_file: The CSV file to use when training.
      val_file: The CSV file to use when validating.
      test_file: The CSV file to use when testing.
      predict_file: The CSV file to use when predicting.
      input_cls: The :class:`~flash.core.data.io.input.Input` type to use for loading the data.
      transform: The :class:`~flash.core.data.io.input_transform.InputTransform` type to use.
      transform_kwargs: Dict of keyword arguments to be provided when instantiating the transforms.
      data_module_kwargs: Additional keyword arguments to provide to the
          :class:`~flash.core.data.data_module.DataModule` constructor.
  
  Returns:
      The constructed :class:`~flash.text.seq2seq.translation.data.TranslationData`.
  
  Examples
  ________
  
  The files can be in Comma Separated Values (CSV) format with either a ``.csv`` or ``.txt`` extension.
  
  .. testsetup::
  
      >>> import os
      >>> from pandas import DataFrame
      >>> DataFrame.from_dict({
      ...     "pig latin": ["ayay entencesay inyay igpay atinlay", "ellohay orldway"],
      ...     "english": ["a sentence in pig latin", "hello world"],
      ... }).to_csv("train_data.csv", index=False)
      >>> DataFrame.from_dict({
      ...     "pig latin": ["ayay entencesay orfay edictionpray"],
      ... }).to_csv("predict_data.csv", index=False)
  
  The file ``train_data.csv`` contains the following:
  
  .. code-block::
  
      pig latin,english
      ayay entencesay inyay igpay atinlay,a sentence in pig latin
      ellohay orldway,hello world
  
  The file ``predict_data.csv`` contains the following:
  
  .. code-block::
  
      pig latin
      ayay entencesay orfay edictionpray
  
  .. doctest::
  
      >>> from flash import Trainer
      >>> from flash.text import TranslationTask, TranslationData
      >>> datamodule = TranslationData.from_csv(
      ...     "pig latin",
      ...     "english",
      ...     train_file="train_data.csv",
      ...     predict_file="predict_data.csv",
      ...     batch_size=2,
      ... )
      >>> model = TranslationTask()
      >>> trainer = Trainer(fast_dev_run=True)
      >>> trainer.fit(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
      Training...
      >>> trainer.predict(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
      Predicting...
  
  .. testcleanup::
  
      >>> os.remove("train_data.csv")
      >>> os.remove("predict_data.csv")
  
  Alternatively, the files can be in Tab Separated Values (TSV) format with a ``.tsv`` extension.
  
  .. testsetup::
  
      >>> import os
      >>> from pandas import DataFrame
      >>> DataFrame.from_dict({
      ...     "pig latin": ["ayay entencesay inyay igpay atinlay", "ellohay orldway"],
      ...     "english": ["a sentence in pig latin", "hello world"],
      ... }).to_csv("train_data.tsv", sep="\t", index=False)
      >>> DataFrame.from_dict({
      ...     "pig latin": ["ayay entencesay orfay edictionpray"],
      ... }).to_csv("predict_data.tsv", sep="\t", index=False)
  
  The file ``train_data.tsv`` contains the following:
  
  .. code-block::
  
      pig latin                               english
      ayay entencesay inyay igpay atinlay     a sentence in pig latin
      ellohay orldway                         hello world
  
  The file ``predict_data.tsv`` contains the following:
  
  .. code-block::
  
      pig latin
      ayay entencesay orfay edictionpray
  
  .. doctest::
  
      >>> from flash import Trainer
      >>> from flash.text import TranslationTask, TranslationData
      >>> datamodule = TranslationData.from_csv(
      ...     "pig latin",
      ...     "english",
      ...     train_file="train_data.tsv",
      ...     predict_file="predict_data.tsv",
      ...     batch_size=2,
      ... )
      >>> model = TranslationTask()
      >>> trainer = Trainer(fast_dev_run=True)
      >>> trainer.fit(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
      Training...
      >>> trainer.predict(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
      Predicting...
  
  .. testcleanup::
  
      >>> os.remove("train_data.tsv")
      >>> os.remove("predict_data.tsv")
docstr_tok: ["Load","the",":","class",":","`","~flash.text.seq2seq.translation.data.TranslationData","`","from","CSV","files","containing","input","text","snippets","and","their","corresponding","target","text","snippets",".","Input","text","snippets","will","be","extracted","from","the","``","input_field","``","column","in","the","CSV","files",".","Target","text","snippets","will","be","extracted","from","the","``","target_field","``","column","in","the","CSV","files",".","To","learn","how","to","customize","the","transforms","applied","for","each","stage",",","read","our",":","ref",":","`","customizing","transforms","guide","<","customizing_transforms",">","`",".","Args",":","input_field",":","The","field","(","column","name",")","in","the","CSV","files","containing","the","input","text","snippets",".","target_field",":","The","field","(","column","name",")","in","the","CSV","files","containing","the","target","text","snippets",".","train_file",":","The","CSV","file","to","use","when","training",".","val_file",":","The","CSV","file","to","use","when","validating",".","test_file",":","The","CSV","file","to","use","when","testing",".","predict_file",":","The","CSV","file","to","use","when","predicting",".","input_cls",":","The",":","class",":","`","~flash.core.data.io.input.Input","`","type","to","use","for","loading","the","data",".","transform",":","The",":","class",":","`","~flash.core.data.io.input_transform.InputTransform","`","type","to","use",".","transform_kwargs",":","Dict","of","keyword","arguments","to","be","provided","when","instantiating","the","transforms",".","data_module_kwargs",":","Additional","keyword","arguments","to","provide","to","the",":","class",":","`","~flash.core.data.data_module.DataModule","`","constructor",".","Returns",":","The","constructed",":","class",":","`","~flash.text.seq2seq.translation.data.TranslationData","`",".","Examples","________","The","files","can","be","in","Comma","Separated","Values","(","CSV",")","format","with","either","a","``",".csv","``","or","``",".txt","``","extension",".","..","testsetup",":",":",">",">",">","import","os",">",">",">","from","pandas","import","DataFrame",">",">",">","DataFrame.from_dict","(","{","...","``","pig","latin","''",":","[","``","ayay","entencesay","inyay","igpay","atinlay","''",",","``","ellohay","orldway","''","]",",","...","``","english","''",":","[","``","a","sentence","in","pig","latin","''",",","``","hello","world","''","]",",","...","}",")",".to_csv","(","``","train_data.csv","''",",","index=False",")",">",">",">","DataFrame.from_dict","(","{","...","``","pig","latin","''",":","[","``","ayay","entencesay","orfay","edictionpray","''","]",",","...","}",")",".to_csv","(","``","predict_data.csv","''",",","index=False",")","The","file","``","train_data.csv","``","contains","the","following",":","..","code-block",":",":","pig","latin",",","english","ayay","entencesay","inyay","igpay","atinlay",",","a","sentence","in","pig","latin","ellohay","orldway",",","hello","world","The","file","``","predict_data.csv","``","contains","the","following",":","..","code-block",":",":","pig","latin","ayay","entencesay","orfay","edictionpray","..","doctest",":",":",">",">",">","from","flash","import","Trainer",">",">",">","from","flash.text","import","TranslationTask",",","TranslationData",">",">",">","datamodule","=","TranslationData.from_csv","(","...","``","pig","latin","''",",","...","``","english","''",",","...","train_file=","''","train_data.csv","''",",","...","predict_file=","''","predict_data.csv","''",",","...","batch_size=2",",","...",")",">",">",">","model","=","TranslationTask","(",")",">",">",">","trainer","=","Trainer","(","fast_dev_run=True",")",">",">",">","trainer.fit","(","model",",","datamodule=datamodule",")","#","doctest",":","+ELLIPSIS","+NORMALIZE_WHITESPACE","Training","...",">",">",">","trainer.predict","(","model",",","datamodule=datamodule",")","#","doctest",":","+ELLIPSIS","+NORMALIZE_WHITESPACE","Predicting","...","..","testcleanup",":",":",">",">",">","os.remove","(","``","train_data.csv","''",")",">",">",">","os.remove","(","``","predict_data.csv","''",")","Alternatively",",","the","files","can","be","in","Tab","Separated","Values","(","TSV",")","format","with","a","``",".tsv","``","extension",".","..","testsetup",":",":",">",">",">","import","os",">",">",">","from","pandas","import","DataFrame",">",">",">","DataFrame.from_dict","(","{","...","``","pig","latin","''",":","[","``","ayay","entencesay","inyay","igpay","atinlay","''",",","``","ellohay","orldway","''","]",",","...","``","english","''",":","[","``","a","sentence","in","pig","latin","''",",","``","hello","world","''","]",",","...","}",")",".to_csv","(","``","train_data.tsv","''",",","sep=","''","\\t","''",",","index=False",")",">",">",">","DataFrame.from_dict","(","{","...","``","pig","latin","''",":","[","``","ayay","entencesay","orfay","edictionpray","''","]",",","...","}",")",".to_csv","(","``","predict_data.tsv","''",",","sep=","''","\\t","''",",","index=False",")","The","file","``","train_data.tsv","``","contains","the","following",":","..","code-block",":",":","pig","latin","english","ayay","entencesay","inyay","igpay","atinlay","a","sentence","in","pig","latin","ellohay","orldway","hello","world","The","file","``","predict_data.tsv","``","contains","the","following",":","..","code-block",":",":","pig","latin","ayay","entencesay","orfay","edictionpray","..","doctest",":",":",">",">",">","from","flash","import","Trainer",">",">",">","from","flash.text","import","TranslationTask",",","TranslationData",">",">",">","datamodule","=","TranslationData.from_csv","(","...","``","pig","latin","''",",","...","``","english","''",",","...","train_file=","''","train_data.tsv","''",",","...","predict_file=","''","predict_data.tsv","''",",","...","batch_size=2",",","...",")",">",">",">","model","=","TranslationTask","(",")",">",">",">","trainer","=","Trainer","(","fast_dev_run=True",")",">",">",">","trainer.fit","(","model",",","datamodule=datamodule",")","#","doctest",":","+ELLIPSIS","+NORMALIZE_WHITESPACE","Training","...",">",">",">","trainer.predict","(","model",",","datamodule=datamodule",")","#","doctest",":","+ELLIPSIS","+NORMALIZE_WHITESPACE","Predicting","...","..","testcleanup",":",":",">",">",">","os.remove","(","``","train_data.tsv","''",")",">",">",">","os.remove","(","``","predict_data.tsv","''",")"]
code: def from_csv(
      cls,
      input_field: str,
      target_field: Optional[str] = None,
      train_file: Optional[PATH_TYPE] = None,
      val_file: Optional[PATH_TYPE] = None,
      test_file: Optional[PATH_TYPE] = None,
      predict_file: Optional[PATH_TYPE] = None,
      input_cls: Type[Input] = Seq2SeqCSVInput,
      transform: INPUT_TRANSFORM_TYPE = InputTransform,
      transform_kwargs: Optional[Dict] = None,
      **data_module_kwargs: Any,
  ) -> "TranslationData":
      """Load the :class:`~flash.text.seq2seq.translation.data.TranslationData` from CSV files containing input text
      snippets and their corresponding target text snippets.
  
      Input text snippets will be extracted from the ``input_field`` column in the CSV files.
      Target text snippets will be extracted from the ``target_field`` column in the CSV files.
      To learn how to customize the transforms applied for each stage, read our
      :ref:`customizing transforms guide <customizing_transforms>`.
  
      Args:
          input_field: The field (column name) in the CSV files containing the input text snippets.
          target_field: The field (column name) in the CSV files containing the target text snippets.
          train_file: The CSV file to use when training.
          val_file: The CSV file to use when validating.
          test_file: The CSV file to use when testing.
          predict_file: The CSV file to use when predicting.
          input_cls: The :class:`~flash.core.data.io.input.Input` type to use for loading the data.
          transform: The :class:`~flash.core.data.io.input_transform.InputTransform` type to use.
          transform_kwargs: Dict of keyword arguments to be provided when instantiating the transforms.
          data_module_kwargs: Additional keyword arguments to provide to the
              :class:`~flash.core.data.data_module.DataModule` constructor.
  
      Returns:
          The constructed :class:`~flash.text.seq2seq.translation.data.TranslationData`.
  
      Examples
      ________
  
      The files can be in Comma Separated Values (CSV) format with either a ``.csv`` or ``.txt`` extension.
  
      .. testsetup::
  
          >>> import os
          >>> from pandas import DataFrame
          >>> DataFrame.from_dict({
          ...     "pig latin": ["ayay entencesay inyay igpay atinlay", "ellohay orldway"],
          ...     "english": ["a sentence in pig latin", "hello world"],
          ... }).to_csv("train_data.csv", index=False)
          >>> DataFrame.from_dict({
          ...     "pig latin": ["ayay entencesay orfay edictionpray"],
          ... }).to_csv("predict_data.csv", index=False)
  
      The file ``train_data.csv`` contains the following:
  
      .. code-block::
  
          pig latin,english
          ayay entencesay inyay igpay atinlay,a sentence in pig latin
          ellohay orldway,hello world
  
      The file ``predict_data.csv`` contains the following:
  
      .. code-block::
  
          pig latin
          ayay entencesay orfay edictionpray
  
      .. doctest::
  
          >>> from flash import Trainer
          >>> from flash.text import TranslationTask, TranslationData
          >>> datamodule = TranslationData.from_csv(
          ...     "pig latin",
          ...     "english",
          ...     train_file="train_data.csv",
          ...     predict_file="predict_data.csv",
          ...     batch_size=2,
          ... )
          >>> model = TranslationTask()
          >>> trainer = Trainer(fast_dev_run=True)
          >>> trainer.fit(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
          Training...
          >>> trainer.predict(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
          Predicting...
  
      .. testcleanup::
  
          >>> os.remove("train_data.csv")
          >>> os.remove("predict_data.csv")
  
      Alternatively, the files can be in Tab Separated Values (TSV) format with a ``.tsv`` extension.
  
      .. testsetup::
  
          >>> import os
          >>> from pandas import DataFrame
          >>> DataFrame.from_dict({
          ...     "pig latin": ["ayay entencesay inyay igpay atinlay", "ellohay orldway"],
          ...     "english": ["a sentence in pig latin", "hello world"],
          ... }).to_csv("train_data.tsv", sep="\\t", index=False)
          >>> DataFrame.from_dict({
          ...     "pig latin": ["ayay entencesay orfay edictionpray"],
          ... }).to_csv("predict_data.tsv", sep="\\t", index=False)
  
      The file ``train_data.tsv`` contains the following:
  
      .. code-block::
  
          pig latin                               english
          ayay entencesay inyay igpay atinlay     a sentence in pig latin
          ellohay orldway                         hello world
  
      The file ``predict_data.tsv`` contains the following:
  
      .. code-block::
  
          pig latin
          ayay entencesay orfay edictionpray
  
      .. doctest::
  
          >>> from flash import Trainer
          >>> from flash.text import TranslationTask, TranslationData
          >>> datamodule = TranslationData.from_csv(
          ...     "pig latin",
          ...     "english",
          ...     train_file="train_data.tsv",
          ...     predict_file="predict_data.tsv",
          ...     batch_size=2,
          ... )
          >>> model = TranslationTask()
          >>> trainer = Trainer(fast_dev_run=True)
          >>> trainer.fit(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
          Training...
          >>> trainer.predict(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
          Predicting...
  
      .. testcleanup::
  
          >>> os.remove("train_data.tsv")
          >>> os.remove("predict_data.tsv")
  
      """
  
      ds_kw = {
          "input_key": input_field,
          "target_key": target_field,
      }
  
      return cls(
          input_cls(RunningStage.TRAINING, train_file, **ds_kw),
          input_cls(RunningStage.VALIDATING, val_file, **ds_kw),
          input_cls(RunningStage.TESTING, test_file, **ds_kw),
          input_cls(RunningStage.PREDICTING, predict_file, **ds_kw),
          transform=transform,
          transform_kwargs=transform_kwargs,
          **data_module_kwargs,
      )
  
file_path: lightning-flash/src/flash/text/seq2seq/translation/data.py
imports: from typing import Any, Dict, List, Optional, Type
  import torch
  from flash.core.data.data_module import DataModule
  from flash.core.data.io.input import Input
  from flash.core.data.io.input_transform import InputTransform
  from flash.core.data.utilities.paths import PATH_TYPE
  from flash.core.utilities.imports import _TOPIC_TEXT_AVAILABLE
  from flash.core.utilities.stages import RunningStage
  from flash.core.utilities.types import INPUT_TRANSFORM_TYPE
  from flash.text.seq2seq.core.input import Seq2SeqCSVInput, Seq2SeqInputBase, Seq2SeqJSONInput, Seq2SeqListInput
label_desc: Use image node_id 1 for calling the TranslationData obj's underlying member method code with example usage: obj.from_csv(cls, input_field, target_field, train_file, val_file, test_file, predict_file, input_cls, transform, transform_kwargs) and returns: cls