class_docstr: Use Cloud Translate API as a Generator.
  
  In order to use this generator, you must have Cloud Translation set up
  through a Google Cloud project as described at
  https://cloud.google.com/translate/docs/setup and have downloaded your
  application credentials file locally and set the
  GOOGLE_APPLICATION_CREDENTIALS environment variable to point to that file.
class_docstr_tok: ["Use","Cloud","Translate","API","as","a","Generator",".","In","order","to","use","this","generator",",","you","must","have","Cloud","Translation","set","up","through","a","Google","Cloud","project","as","described","at","https",":","\/\/cloud.google.com\/translate\/docs\/setup","and","have","downloaded","your","application","credentials","file","locally","and","set","the","GOOGLE_APPLICATION_CREDENTIALS","environment","variable","to","point","to","that","file","."]
docstr: Run backtranslation on the list of strings.
docstr_tok: ["Run","backtranslation","on","the","list","of","strings","."]
code: def generate_from_texts(self, texts: list[str]) -> list[list[str]]:
      """Run backtranslation on the list of strings."""
      # Use Pandas to keep track of metadata, so we can batch MT inputs
      # without losing track of which example they belong to.
      # Prepare input DataFrame
      dataframes = []
      for lang in self._pivot_langs:
          df = pd.DataFrame(data={"source": texts}).reset_index()
          df["pivot_language"] = lang
          dataframes.append(df)
      df = pd.concat(dataframes, axis=0).sort_values(by="index")
      # Forward translation
      # pylint: disable=g-complex-comprehension
      mt_inputs = [
          {
              "source": text,
              "source_language": self._source_lang,
              "target_language": lang,
          }
          for text, lang in zip(df["source"], df["pivot_language"])
      ]
      result = []
      for mt_input in mt_inputs:
          result.append(
              self._translate_client.translate(
                  mt_input["source"],
                  target_language=mt_input["target_language"],
                  source_language=mt_input["source_language"],
              )
          )
      all_translations = [[r["translatedText"]] for r in result]
      # Track metadata by replicating input rows
      # TODO(iftenney): replace with DataFrame.explode() once we can use
      # pandas 0.25
      rows = []
      for i, translation_set in enumerate(all_translations):
          for translation in translation_set:
              row = dict(df.iloc[i])
              row["pivot"] = translation
              rows.append(row)
      # Forward translation results
      intermediate_df = pd.DataFrame.from_records(rows)
      # TODO(lit-team): yield a chunk with intermediate state at this point,
      # for visualization before reverse translate is complete.
      # Reverse translation
      # pylint: disable=g-complex-comprehension
      mt_inputs = [
          {
              "source": text,
              "source_language": src,
              "target_language": self._source_lang,
          }
          for text, src in zip(
              intermediate_df["pivot"],
              intermediate_df["pivot_language"],
          )
      ]
      logging.info(
          "Reverse: %d translations requested.", len(mt_inputs)
      )
      result = []
      for mt_input in mt_inputs:
          result.append(
              self._translate_client.translate(
                  mt_input["source"],
                  target_language=mt_input["target_language"],
                  source_language=mt_input["source_language"],
              )
          )
      all_translations = [[r["translatedText"]] for r in result]
      # Track metadata by replicating input rows
      # TODO(iftenney): replace with DataFrame.explode() once we can use
      # pandas 0.25
      rows = []
      for i, translation_set in enumerate(all_translations):
          for translation in translation_set:
              row = dict(intermediate_df.iloc[i])
              row["target"] = translation
              rows.append(row)
      final_df = pd.DataFrame.from_records(rows)
      # Since we kept the indices in the DataFrame, we can group over these to get
      # the paraphrase candidates for each input.
      # this gives a list(list(str))
      candidates = list(
          final_df.groupby(by="index").agg({"target": list})["target"]
      )
      return candidates
  
file_path: lit/lit_nlp/components/backtranslator.py
imports: from collections.abc import Sequence
  from typing import Optional
  from absl import logging
  from google.cloud import translate_v2
  from lit_nlp.api import components
  from lit_nlp.api import dataset
  from lit_nlp.api import model
  from lit_nlp.api import types
  from lit_nlp.lib import utils
  import pandas
label_desc: Use image node_id 5 for calling the Backtranslator obj's underlying member method code with example usage: obj.generate_from_texts(texts) and returns: candidates