docstr: Formats either a `FeedbackDataset` or a `RemoteFeedbackDataset` as a `datasets.Dataset` object.
  
  Args:
      dataset: The `FeedbackDataset` or `RemoteFeedbackDataset` to format as `datasets.Dataset`.
  
  Returns:
      The records from the dataset formatted as a `datasets.Dataset` object, including the fields,
      questions, and metadata_properties formatted as `datasets.Features`.
  
  Examples:
      >>> from argilla.client.feedback.integrations.dataset import HuggingFaceDatasetMixin
      >>> dataset = FeedbackDataset(...) or RemoteFeedbackDataset(...)
      >>> huggingface_dataset = HuggingFaceDatasetMixin._huggingface_format(dataset)
docstr_tok: ["Formats","either","a","`","FeedbackDataset","`","or","a","`","RemoteFeedbackDataset","`","as","a","`","datasets.Dataset","`","object",".","Args",":","dataset",":","The","`","FeedbackDataset","`","or","`","RemoteFeedbackDataset","`","to","format","as","`","datasets.Dataset","`",".","Returns",":","The","records","from","the","dataset","formatted","as","a","`","datasets.Dataset","`","object",",","including","the","fields",",","questions",",","and","metadata_properties","formatted","as","`","datasets.Features","`",".","Examples",":",">",">",">","from","argilla.client.feedback.integrations.dataset","import","HuggingFaceDatasetMixin",">",">",">","dataset","=","FeedbackDataset","(","...",")","or","RemoteFeedbackDataset","(","...",")",">",">",">","huggingface_dataset","=","HuggingFaceDatasetMixin._huggingface_format","(","dataset",")"]
code: def _huggingface_format(
      dataset: Union["FeedbackDataset", "RemoteFeedbackDataset"]
  ) -> "Dataset":
      """Formats either a `FeedbackDataset` or a `RemoteFeedbackDataset` as a `datasets.Dataset` object.
  
      Args:
          dataset: The `FeedbackDataset` or `RemoteFeedbackDataset` to format as `datasets.Dataset`.
  
      Returns:
          The records from the dataset formatted as a `datasets.Dataset` object, including the fields,
          questions, and metadata_properties formatted as `datasets.Features`.
  
      Examples:
          >>> from argilla.client.feedback.integrations.dataset import HuggingFaceDatasetMixin
          >>> dataset = FeedbackDataset(...) or RemoteFeedbackDataset(...)
          >>> huggingface_dataset = HuggingFaceDatasetMixin._huggingface_format(dataset)
      """
      from datasets import Dataset, Features, Sequence, Value
  
      hf_dataset, hf_features = {}, {}
  
      for field in dataset.fields:
          if field.type not in FIELD_TYPE_TO_PYTHON_TYPE.keys():
              raise ValueError(
                  f"Field {field.name} has an unsupported type: {field.type}, for the moment"
                  f" only the following types are supported: {list(FIELD_TYPE_TO_PYTHON_TYPE.keys())}"
              )
          hf_features[field.name] = Value(dtype="string", id="field")
          if field.name not in hf_dataset:
              hf_dataset[field.name] = []
  
      for question in dataset.questions:
          if question.type in [
              QuestionTypes.text,
              QuestionTypes.label_selection,
          ]:
              value = Value(dtype="string", id="question")
          elif question.type == QuestionTypes.rating:
              value = Value(dtype="int32", id="question")
          elif question.type == QuestionTypes.ranking:
              value = Sequence(
                  {
                      "rank": Value(dtype="uint8"),
                      "value": Value(dtype="string"),
                  },
                  id="question",
              )
          elif question.type in QuestionTypes.multi_label_selection:
              value = Sequence(Value(dtype="string"), id="question")
          else:
              raise ValueError(
                  f"Question {question.name} is of type `{question.type}`,"
                  " for the moment only the following question types are supported:"
                  f" `{'`, `'.join([arg.value for arg in QuestionTypes])}`."
              )
  
          hf_features[question.name] = [
              {
                  "user_id": Value(dtype="string", id="question"),
                  "value": value,
                  "status": Value(dtype="string", id="question"),
              }
          ]
          if question.name not in hf_dataset:
              hf_dataset[question.name] = []
  
          value.id = "suggestion"
          hf_features[f"{question.name}-suggestion"] = value
          if f"{question.name}-suggestion" not in hf_dataset:
              hf_dataset[f"{question.name}-suggestion"] = []
  
          hf_features[f"{question.name}-suggestion-metadata"] = {
              "type": Value(dtype="string", id="suggestion-metadata"),
              "score": Value(dtype="float32", id="suggestion-metadata"),
              "agent": Value(dtype="string", id="suggestion-metadata"),
          }
          if f"{question.name}-suggestion-metadata" not in hf_dataset:
              hf_dataset[f"{question.name}-suggestion-metadata"] = []
  
      hf_features["external_id"] = Value(
          dtype="string", id="external_id"
      )
      hf_dataset["external_id"] = []
  
      hf_features["metadata"] = Value(dtype="string", id="metadata")
      hf_dataset["metadata"] = []
  
      vectors_settings = dataset.vectors_settings
      if vectors_settings:
          hf_features["vectors"] = {}
          for vector_settings in vectors_settings:
              hf_features["vectors"].update(
                  {
                      vector_settings.name: Sequence(
                          Value(dtype="float32"), id="vectors"
                      )
                  }
              )
          hf_dataset["vectors"] = []
  
      for record in dataset.records:
          for field in dataset.fields:
              hf_dataset[field.name].append(
                  record.fields.get(field.name, None)
              )
          for question in dataset.questions:
              if not record.responses:
                  hf_dataset[question.name].append([])
              else:
                  responses = []
                  for response in record.responses:
                      if question.name not in response.values:
                          continue
                      formatted_response = {
                          "user_id": response.user_id,
                          "value": None,
                          "status": response.status.value
                          if hasattr(response.status, "value")
                          else response.status,
                      }
                      if question.type == QuestionTypes.ranking:
                          value = [
                              r.dict()
                              for r in response.values[
                                  question.name
                              ].value
                          ]
                      else:
                          value = response.values[question.name].value
                      formatted_response["value"] = value
                      responses.append(formatted_response)
                  hf_dataset[question.name].append(responses)
  
              suggestion_value, suggestion_metadata = None, {
                  "type": None,
                  "score": None,
                  "agent": None,
              }
              if record.suggestions:
                  for suggestion in record.suggestions:
                      if question.name == suggestion.question_name:
                          suggestion_value = suggestion.value
                          suggestion_metadata = {
                              "type": suggestion.type,
                              "score": suggestion.score,
                              "agent": suggestion.agent,
                          }
                          break
              hf_dataset[f"{question.name}-suggestion"].append(
                  suggestion_value
              )
              hf_dataset[f"{question.name}-suggestion-metadata"].append(
                  suggestion_metadata
              )
  
          hf_dataset["metadata"].append(
              json.dumps(record.metadata) if record.metadata else {}
          )
          hf_dataset["external_id"].append(record.external_id or None)
  
          if vectors_settings:
              vectors = {}
              for vector_settings in vectors_settings:
                  vectors.update(
                      {
                          vector_settings.name: record.vectors.get(
                              vector_settings.name, None
                          )
                      }
                  )
              hf_dataset["vectors"].append(vectors)
  
      return Dataset.from_dict(
          hf_dataset, features=Features(hf_features)
      )
  
file_path: argilla/src/argilla/client/feedback/integrations/huggingface/dataset.py
imports: import json
  import logging
  import tempfile
  import warnings
  from typing import TYPE_CHECKING, Any, Optional, Type, Union
  from packaging.version import parse
  from tqdm import trange
  from argilla.client.feedback.constants import FIELD_TYPE_TO_PYTHON_TYPE
  from argilla.client.feedback.schemas.enums import QuestionTypes
  from argilla.client.feedback.schemas.records import FeedbackRecord
  from argilla.client.feedback.schemas.remote.records import RemoteFeedbackRecord
  from argilla.utils.dependency import requires_dependencies
label_desc: Use image node_id 1 for calling the HuggingFaceDatasetMixin obj's underlying member method code with example usage: obj._huggingface_format(dataset) and returns: Dataset