code: def encode_to_base64(r: httpx.Response) -> str:
      # Handles the different ways HF API returns the prediction
      base64_repr = base64.b64encode(r.content).decode("utf-8")
      data_prefix = ";base64,"
      # Case 1: base64 representation already includes data prefix
      if data_prefix in base64_repr:
          return base64_repr
      else:
          content_type = r.headers.get("content-type")
          # Case 2: the data prefix is a key in the response
          if content_type == "application/json":
              try:
                  data = r.json()[0]
                  content_type = data["content-type"]
                  base64_repr = data["blob"]
              except KeyError as ke:
                  raise ValueError(
                      "Cannot determine content type returned by external API."
                  ) from ke
          # Case 3: the data prefix is included in the response headers
          else:
              pass
          new_base64 = f"data:{content_type};base64,{base64_repr}"
          return new_base64
  
file_path: gradio/gradio/external_utils.py
imports: import base64
  import math
  import operator
  import re
  import warnings
  from typing import Dict, List, Tuple
  import httpx
  import yaml
  from gradio import components
label_desc: Use image node_id 5 for calling a global function with example usage: encode_to_base64(r) and returns: base64_repr, new_base64