class_docstr: A callback that saves each flagged sample (both the input and output data) to a HuggingFace dataset.
  
  Example:
      import gradio as gr
      hf_writer = gr.HuggingFaceDatasetSaver(HF_API_TOKEN, "image-classification-mistakes")
      def image_classifier(inp):
          return {'cat': 0.3, 'dog': 0.7}
      demo = gr.Interface(fn=image_classifier, inputs="image", outputs="label",
                          allow_flagging="manual", flagging_callback=hf_writer)
  Guides: using-flagging
class_docstr_tok: ["A","callback","that","saves","each","flagged","sample","(","both","the","input","and","output","data",")","to","a","HuggingFace","dataset",".","Example",":","import","gradio","as","gr","hf_writer","=","gr.HuggingFaceDatasetSaver","(","HF_API_TOKEN",",","``","image-classification-mistakes","''",")","def","image_classifier","(","inp",")",":","return","{","'cat","'",":","0.3",",","'dog","'",":","0.7","}","demo","=","gr.Interface","(","fn=image_classifier",",","inputs=","''","image","''",",","outputs=","''","label","''",",","allow_flagging=","''","manual","''",",","flagging_callback=hf_writer",")","Guides",":","using-flagging"]
docstr: Deserialize components and return the corresponding row for the flagged sample.
  
  Images/audio are saved to disk as individual files.
code: def _deserialize_components(
      self,
      data_dir: Path,
      flag_data: list[Any],
      flag_option: str = "",
      username: str = "",
  ) -> tuple[dict[Any, Any], list[Any]]:
      """Deserialize components and return the corresponding row for the flagged sample.
  
      Images/audio are saved to disk as individual files.
      """
      # Components that can have a preview on dataset repos
      file_preview_types = {gr.Audio: "Audio", gr.Image: "Image"}
  
      # Generate the row corresponding to the flagged sample
      features = OrderedDict()
      row = []
      for component, sample in zip(self.components, flag_data):
          # Get deserialized object (will save sample to disk if applicable -file, audio, image,...-)
          label = component.label or ""
          save_dir = (
              data_dir
              / client_utils.strip_invalid_filename_characters(label)
          )
          save_dir.mkdir(exist_ok=True, parents=True)
          deserialized = component.flag(sample, save_dir)
  
          # Add deserialized object to row
          features[label] = {"dtype": "string", "_type": "Value"}
          try:
              assert Path(deserialized).exists()
              row.append(
                  str(Path(deserialized).relative_to(self.dataset_dir))
              )
          except (AssertionError, TypeError, ValueError):
              deserialized = (
                  "" if deserialized is None else str(deserialized)
              )
              row.append(deserialized)
  
          # If component is eligible for a preview, add the URL of the file
          # Be mindful that images and audio can be None
          if isinstance(component, tuple(file_preview_types)):  # type: ignore
              for _component, _type in file_preview_types.items():
                  if isinstance(component, _component):
                      features[label + " file"] = {"_type": _type}
                      break
              if deserialized:
                  path_in_repo = str(  # returned filepath is absolute, we want it relative to compute URL
                      Path(deserialized).relative_to(self.dataset_dir)
                  ).replace(
                      "\\", "/"
                  )
                  row.append(
                      huggingface_hub.hf_hub_url(
                          repo_id=self.dataset_id,
                          filename=path_in_repo,
                          repo_type="dataset",
                      )
                  )
              else:
                  row.append("")
      features["flag"] = {"dtype": "string", "_type": "Value"}
      features["username"] = {"dtype": "string", "_type": "Value"}
      row.append(flag_option)
      row.append(username)
      return features, row
  
imports: from __future__ import annotations
  import csv
  import datetime
  import json
  import os
  import time
  import uuid
  from abc import ABC, abstractmethod
  from collections import OrderedDict
  from pathlib import Path
  from typing import TYPE_CHECKING, Any
  import filelock
  import huggingface_hub
  from gradio_client import utils
  from gradio_client.documentation import document, set_documentation_group
  import gradio
  from gradio import utils
label_desc: Use image node_id 7 for calling the HuggingFaceDatasetSaver obj's underlying member method code with example usage: obj._deserialize_components(data_dir, flag_data, flag_option, username) and returns: features,  row