code: def serialize_payload(
      data_source: Union[pd.DataFrame, pd.Series]
  ) -> tuple:
      """
      Generates two dictionaries to be sent via REST API for Ludwig prediction
      service.
      First dictionary created is payload_dict. Keys found in payload_dict:
      raw_data: this is json string created by pandas to_json() method
      source_type: indicates if the data_source is either a pandas dataframe or
          pandas series.  This is needed to know how to rebuild the structure.
      ndarray_dtype:  this is a dictionary where each entry is for any ndarray
          data found in the data_source.  This could be an empty dictioinary if no
          ndarray objects are present in data_source. Key for this dictionary is
          column name if data_source is dataframe or index name if data_source is
          series.  The value portion of the dictionary is the dtype of the
          ndarray.  This value is used to set the correct dtype when rebuilding
          the entry.
  
      Second dictionary created is called payload_files, this contains information
      and content for files to be sent to the server.  NOTE: if no files are to be
      sent, this will be an empty dictionary.
      Entries in this dictionary:
      Key: file path string for file to be sent to server
      Value: tuple(file path string, byte encoded file content,
                   'application/octet-stream')
  
      Args:
          data_source: input features to be sent to Ludwig server
  
      Returns: tuple(payload_dict, payload_files)
  
      """
      payload_dict = {}
      payload_dict["ndarray_dtype"] = {}
      payload_files = {}
      if isinstance(data_source, pd.DataFrame):
          payload_dict["raw_data"] = data_source.to_json(
              orient="columns"
          )
          payload_dict["source_type"] = "dataframe"
          for col in data_source.columns:
              if isinstance(data_source[col].iloc[0], np.ndarray):
                  # if we have any ndarray columns, record dtype
                  payload_dict["ndarray_dtype"][col] = str(
                      data_source[col].iloc[0].dtype
                  )
              elif isinstance(
                  data_source[col].iloc[0], str
              ) and os.path.exists(data_source[col].iloc[0]):
                  # if we have file path feature, prepare file for transport
                  for v in data_source[col]:
                      payload_files[v] = (
                          v,
                          open(v, "rb"),
                          "application/octet-stream",
                      )
      elif isinstance(data_source, pd.Series):
          payload_dict["raw_data"] = data_source.to_json(orient="index")
          payload_dict["source_type"] = "series"
          for col in data_source.index:
              if isinstance(data_source[col], np.ndarray):
                  # for ndarrays record dtype for reconstruction
                  payload_dict["ndarray_dtype"][col] = str(
                      data_source[col].dtype
                  )
              elif isinstance(data_source[col], str) and os.path.exists(
                  data_source[col]
              ):
                  # if we have file path feature, prepare file for transport
                  v = data_source[col]
                  payload_files[v] = (
                      v,
                      open(v, "rb"),
                      "application/octet-stream",
                  )
      else:
          ValueError(
              '"data_source" must be either a pandas DataFrame or Series, '
              "format found to be {}".format(type(data_source))
          )
  
      return payload_dict, payload_files
  
file_path: ludwig/ludwig/utils/server_utils.py
imports: import json
  import os
  import tempfile
  from typing import Any, Dict, Union
  import numpy
  import pandas
  from starlette.datastructures import UploadFile
  from starlette.responses import JSONResponse
  from ludwig.utils.data_utils import NumpyEncoder
label_desc: Use image node_id 1 for calling a global function with example usage: serialize_payload(data_source) and returns: payload_dict,  payload_files