code: def from_model(
      model_name: str, hf_token: str | None, alias: str | None, **kwargs
  ):
      model_url = f"https://huggingface.co/{model_name}"
      api_url = (
          f"https://api-inference.huggingface.co/models/{model_name}"
      )
      print(f"Fetching model from: {model_url}")
  
      headers = (
          {"Authorization": f"Bearer {hf_token}"}
          if hf_token is not None
          else {}
      )
  
      # Checking if model exists, and if so, it gets the pipeline
      response = httpx.request("GET", api_url, headers=headers)
      if response.status_code != 200:
          raise ModelNotFoundError(
              f"Could not find model: {model_name}. If it is a private or gated model, please provide your Hugging Face access token (https://huggingface.co/settings/tokens) as the argument for the `hf_token` parameter."
          )
      p = response.json().get("pipeline_tag")
      GRADIO_CACHE = os.environ.get(
          "GRADIO_TEMP_DIR"
      ) or str(  # noqa: N806
          Path(tempfile.gettempdir()) / "gradio"
      )
  
      pipelines = {
          "audio-classification": {
              # example model: ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition
              "inputs": components.Audio(
                  sources=["upload"],
                  type="filepath",
                  label="Input",
                  render=False,
              ),
              "outputs": components.Label(label="Class", render=False),
              "preprocess": lambda i: to_binary,
              "postprocess": lambda r: postprocess_label(
                  {
                      i["label"].split(", ")[0]: i["score"]
                      for i in r.json()
                  }
              ),
          },
          "audio-to-audio": {
              # example model: facebook/xm_transformer_sm_all-en
              "inputs": components.Audio(
                  sources=["upload"],
                  type="filepath",
                  label="Input",
                  render=False,
              ),
              "outputs": components.Audio(label="Output", render=False),
              "preprocess": to_binary,
              "postprocess": lambda x: save_base64_to_cache(
                  encode_to_base64(x),
                  cache_dir=GRADIO_CACHE,
                  file_name="output.wav",
              ),
          },
          "automatic-speech-recognition": {
              # example model: facebook/wav2vec2-base-960h
              "inputs": components.Audio(
                  sources=["upload"],
                  type="filepath",
                  label="Input",
                  render=False,
              ),
              "outputs": components.Textbox(
                  label="Output", render=False
              ),
              "preprocess": to_binary,
              "postprocess": lambda r: r.json()["text"],
          },
          "conversational": {
              "inputs": [
                  components.Textbox(render=False),
                  components.State(render=False),
              ],  # type: ignore
              "outputs": [
                  components.Chatbot(render=False),
                  components.State(render=False),
              ],  # type: ignore
              "preprocess": chatbot_preprocess,
              "postprocess": chatbot_postprocess,
          },
          "feature-extraction": {
              # example model: julien-c/distilbert-feature-extraction
              "inputs": components.Textbox(label="Input", render=False),
              "outputs": components.Dataframe(
                  label="Output", render=False
              ),
              "preprocess": lambda x: {"inputs": x},
              "postprocess": lambda r: r.json()[0],
          },
          "fill-mask": {
              "inputs": components.Textbox(label="Input", render=False),
              "outputs": components.Label(
                  label="Classification", render=False
              ),
              "preprocess": lambda x: {"inputs": x},
              "postprocess": lambda r: postprocess_label(
                  {i["token_str"]: i["score"] for i in r.json()}
              ),
          },
          "image-classification": {
              # Example: google/vit-base-patch16-224
              "inputs": components.Image(
                  type="filepath", label="Input Image", render=False
              ),
              "outputs": components.Label(
                  label="Classification", render=False
              ),
              "preprocess": to_binary,
              "postprocess": lambda r: postprocess_label(
                  {
                      i["label"].split(", ")[0]: i["score"]
                      for i in r.json()
                  }
              ),
          },
          "question-answering": {
              # Example: deepset/xlm-roberta-base-squad2
              "inputs": [
                  components.Textbox(
                      lines=7, label="Context", render=False
                  ),
                  components.Textbox(label="Question", render=False),
              ],
              "outputs": [
                  components.Textbox(label="Answer", render=False),
                  components.Label(label="Score", render=False),
              ],
              "preprocess": lambda c, q: {
                  "inputs": {"context": c, "question": q}
              },
              "postprocess": lambda r: (
                  r.json()["answer"],
                  {"label": r.json()["score"]},
              ),
          },
          "summarization": {
              # Example: facebook/bart-large-cnn
              "inputs": components.Textbox(label="Input", render=False),
              "outputs": components.Textbox(
                  label="Summary", render=False
              ),
              "preprocess": lambda x: {"inputs": x},
              "postprocess": lambda r: r.json()[0]["summary_text"],
          },
          "text-classification": {
              # Example: distilbert-base-uncased-finetuned-sst-2-english
              "inputs": components.Textbox(label="Input", render=False),
              "outputs": components.Label(
                  label="Classification", render=False
              ),
              "preprocess": lambda x: {"inputs": x},
              "postprocess": lambda r: postprocess_label(
                  {
                      i["label"].split(", ")[0]: i["score"]
                      for i in r.json()[0]
                  }
              ),
          },
          "text-generation": {
              # Example: gpt2
              "inputs": components.Textbox(label="Input", render=False),
              "outputs": components.Textbox(
                  label="Output", render=False
              ),
              "preprocess": lambda x: {"inputs": x},
              "postprocess": lambda r: r.json()[0]["generated_text"],
          },
          "text2text-generation": {
              # Example: valhalla/t5-small-qa-qg-hl
              "inputs": components.Textbox(label="Input", render=False),
              "outputs": components.Textbox(
                  label="Generated Text", render=False
              ),
              "preprocess": lambda x: {"inputs": x},
              "postprocess": lambda r: r.json()[0]["generated_text"],
          },
          "translation": {
              "inputs": components.Textbox(label="Input", render=False),
              "outputs": components.Textbox(
                  label="Translation", render=False
              ),
              "preprocess": lambda x: {"inputs": x},
              "postprocess": lambda r: r.json()[0]["translation_text"],
          },
          "zero-shot-classification": {
              # Example: facebook/bart-large-mnli
              "inputs": [
                  components.Textbox(label="Input", render=False),
                  components.Textbox(
                      label="Possible class names (" "comma-separated)",
                      render=False,
                  ),
                  components.Checkbox(
                      label="Allow multiple true classes", render=False
                  ),
              ],
              "outputs": components.Label(
                  label="Classification", render=False
              ),
              "preprocess": lambda i, c, m: {
                  "inputs": i,
                  "parameters": {
                      "candidate_labels": c,
                      "multi_class": m,
                  },
              },
              "postprocess": lambda r: postprocess_label(
                  {
                      r.json()["labels"][i]: r.json()["scores"][i]
                      for i in range(len(r.json()["labels"]))
                  }
              ),
          },
          "sentence-similarity": {
              # Example: sentence-transformers/distilbert-base-nli-stsb-mean-tokens
              "inputs": [
                  components.Textbox(
                      value="That is a happy person",
                      label="Source Sentence",
                      render=False,
                  ),
                  components.Textbox(
                      lines=7,
                      placeholder="Separate each sentence by a newline",
                      label="Sentences to compare to",
                      render=False,
                  ),
              ],
              "outputs": components.Label(
                  label="Classification", render=False
              ),
              "preprocess": lambda src, sentences: {
                  "inputs": {
                      "source_sentence": src,
                      "sentences": [
                          s for s in sentences.splitlines() if s != ""
                      ],
                  }
              },
              "postprocess": lambda r: postprocess_label(
                  {f"sentence {i}": v for i, v in enumerate(r.json())}
              ),
          },
          "text-to-speech": {
              # Example: julien-c/ljspeech_tts_train_tacotron2_raw_phn_tacotron_g2p_en_no_space_train
              "inputs": components.Textbox(label="Input", render=False),
              "outputs": components.Audio(label="Audio", render=False),
              "preprocess": lambda x: {"inputs": x},
              "postprocess": lambda x: save_base64_to_cache(
                  encode_to_base64(x),
                  cache_dir=GRADIO_CACHE,
                  file_name="output.wav",
              ),
          },
          "text-to-image": {
              # example model: osanseviero/BigGAN-deep-128
              "inputs": components.Textbox(label="Input", render=False),
              "outputs": components.Image(label="Output", render=False),
              "preprocess": lambda x: {"inputs": x},
              "postprocess": lambda x: save_base64_to_cache(
                  encode_to_base64(x),
                  cache_dir=GRADIO_CACHE,
                  file_name="output.jpg",
              ),
          },
          "token-classification": {
              # example model: huggingface-course/bert-finetuned-ner
              "inputs": components.Textbox(label="Input", render=False),
              "outputs": components.HighlightedText(
                  label="Output", render=False
              ),
              "preprocess": lambda x: {"inputs": x},
              "postprocess": lambda r: r,  # Handled as a special case in query_huggingface_api()
          },
          "document-question-answering": {
              # example model: impira/layoutlm-document-qa
              "inputs": [
                  components.Image(
                      type="filepath",
                      label="Input Document",
                      render=False,
                  ),
                  components.Textbox(label="Question", render=False),
              ],
              "outputs": components.Label(label="Label", render=False),
              "preprocess": lambda img, q: {
                  "inputs": {
                      "image": extract_base64_data(
                          client_utils.encode_url_or_file_to_base64(
                              img["path"]
                          )
                      ),  # Extract base64 data
                      "question": q,
                  }
              },
              "postprocess": lambda r: postprocess_label(
                  {i["answer"]: i["score"] for i in r.json()}
              ),
          },
          "visual-question-answering": {
              # example model: dandelin/vilt-b32-finetuned-vqa
              "inputs": [
                  components.Image(
                      type="filepath", label="Input Image", render=False
                  ),
                  components.Textbox(label="Question", render=False),
              ],
              "outputs": components.Label(label="Label", render=False),
              "preprocess": lambda img, q: {
                  "inputs": {
                      "image": extract_base64_data(
                          client_utils.encode_url_or_file_to_base64(
                              img["path"]
                          )
                      ),
                      "question": q,
                  }
              },
              "postprocess": lambda r: postprocess_label(
                  {i["answer"]: i["score"] for i in r.json()}
              ),
          },
          "image-to-text": {
              # example model: Salesforce/blip-image-captioning-base
              "inputs": components.Image(
                  type="filepath", label="Input Image", render=False
              ),
              "outputs": components.Textbox(
                  label="Generated Text", render=False
              ),
              "preprocess": to_binary,
              "postprocess": lambda r: r.json()[0]["generated_text"],
          },
      }
  
      if p in ["tabular-classification", "tabular-regression"]:
          example_data = get_tabular_examples(model_name)
          col_names, example_data = cols_to_rows(example_data)
          example_data = [[example_data]] if example_data else None
  
          pipelines[p] = {
              "inputs": components.Dataframe(
                  label="Input Rows",
                  type="pandas",
                  headers=col_names,
                  col_count=(len(col_names), "fixed"),
                  render=False,
              ),
              "outputs": components.Dataframe(
                  label="Predictions",
                  type="array",
                  headers=["prediction"],
                  render=False,
              ),
              "preprocess": rows_to_cols,
              "postprocess": lambda r: {
                  "headers": ["prediction"],
                  "data": [[pred] for pred in json.loads(r.text)],
              },
              "examples": example_data,
          }
  
      if p is None or p not in pipelines:
          raise ValueError(f"Unsupported pipeline type: {p}")
  
      pipeline = pipelines[p]
  
      def query_huggingface_api(*params):
          # Convert to a list of input components
          data = pipeline["preprocess"](*params)
          if isinstance(
              data, dict
          ):  # HF doesn't allow additional parameters for binary files (e.g. images or audio files)
              data.update({"options": {"wait_for_model": True}})
              data = json.dumps(data)
          response = httpx.request("POST", api_url, headers=headers, data=data)  # type: ignore
          if response.status_code != 200:
              errors_json = response.json()
              errors, warns = "", ""
              if errors_json.get("error"):
                  errors = f", Error: {errors_json.get('error')}"
              if errors_json.get("warnings"):
                  warns = f", Warnings: {errors_json.get('warnings')}"
              raise Error(
                  f"Could not complete request to HuggingFace API, Status Code: {response.status_code}"
                  + errors
                  + warns
              )
          if (
              p == "token-classification"
          ):  # Handle as a special case since HF API only returns the named entities and we need the input as well
              ner_groups = response.json()
              input_string = params[0]
              response = utils.format_ner_list(input_string, ner_groups)
          output = pipeline["postprocess"](response)
          return output
  
      if alias is None:
          query_huggingface_api.__name__ = model_name
      else:
          query_huggingface_api.__name__ = alias
  
      interface_info = {
          "fn": query_huggingface_api,
          "inputs": pipeline["inputs"],
          "outputs": pipeline["outputs"],
          "title": model_name,
          "examples": pipeline.get("examples"),
      }
  
      kwargs = dict(interface_info, **kwargs)
  
      # So interface doesn't run pre/postprocess
      # except for conversational interfaces which
      # are stateful
      kwargs["_api_mode"] = p != "conversational"
  
      interface = gradio.Interface(**kwargs)
      return interface
  
imports: from __future__ import annotations
  import json
  import os
  import re
  import tempfile
  import warnings
  from pathlib import Path
  from typing import TYPE_CHECKING, Callable
  import httpx
  from gradio_client import Client
  from gradio_client import utils
  from gradio_client.client import Endpoint
  from gradio_client.documentation import document, set_documentation_group
  from packaging import version
  import gradio
  from gradio import components, utils
  from gradio.context import Context
  from gradio.exceptions import Error, GradioVersionIncompatibleError, ModelNotFoundError, TooManyRequestsError
  from gradio.external_utils import cols_to_rows, encode_to_base64, get_tabular_examples, postprocess_label, rows_to_cols, streamline_spaces_interface
  from gradio.processing_utils import extract_base64_data, save_base64_to_cache, to_binary
label_desc: Use image node_id 5 for calling a global function with example usage: from_model(model_name, hf_token, alias) and returns: interface, output