Hugging Face: Serverless Inference action

This topic describes using the Hugging Face: Serverless Inference action to leverage the power of Hugging Face models for various NLP tasks.

Prerequisites

Note: The examples and graphics provided on this page are for representation purposes only and may not accurately reflect your specific instance. We do not assume responsibility for their maintenance or accuracy.
  • You must have the Bot creator role to use the Hugging Face: Authentication action in a bot.
  • Create a Hugging Face account. You can create an account here Create a Hugging Face account.
  • Ensure that you have the necessary credentials to send a request and have included Hugging Face: Authentication action before calling Hugging Face: Serverless Inference action.

Procedure

  1. Log in to your Hugging Face account.
  2. Select a Model: Navigate to the Hugging Face model library (Hugging Face models).Search for and select a model that supports Serverless Inference. Supported model types include:
    1. Text Generation
    2. Text to Image Generation
    3. Image to Text Generation
    4. Text to Audio Generation
    For this example, openai-community/gpt2 model is used for Text Generation.
  3. Copy the following from the model's Serverless Inference request format.
    1. Model Id: Copy the model ID from the API URL in the request format. In this case, it would be openai-community/gpt2.
    2. Request Payload: Use the JSON provided in the model's Serverless Inference format.

      Hugging Face Request Format
      Modify the prompt within the JSON to fit your specific use case. For example: you can modify the above JSON as follows:
      {"inputs": "Explain quantum computing in simple terms."}
      

      Watch the following video to learn how to access the above request format of a model.

  4. Log in to the Control Room.
  5. From the Actions pane, select AI > Hugging Face > Serverless Inference and place it in the canvas.
  6. Enter or select the following fields:

    Hugging Face Serverless Inference action
    1. In the Model Id field, paste the Model ID copied from your Hugging Face.
    2. In the Request Payload field, paste the Request Payload copied from your Hugging Face.
    3. Optional: Data File Path: Data File Path specifies the download location for image/audio files generated by your model.
    4. Enter Default as the session name to limit the session to the current session.
    5. Save the response to a variable. In this example, the response is saved to HuggingFace-response.
  7. Click Run to start the automation.
    You can read the value of the field by printing the response in a Message box action. In this example, HuggingFace-response prints the response.
    Tip: To maintain multiple chats in the same bot, you will need to create multiple sessions with different names or variables.

    The response of the above automation is as follows:


    Hugging face Serverless Inference response

    You can parse the output JSON using the JSON package to derive the desired output.

More examples

The following automation showcases text to image, image to text, and text to audio generation capabilities using the Hugging Face: Serverless Inference action. Each example is explained in detail in the subsequent sections.


Hugging Face examples

Text to Image

The following example demonstrates text to image generation capability using the Hugging Face: Serverless Inference action:


Hugging Face Automation Anywhere Text to Image example
Note: Provide a Data File Path to which the image gets saved to.

Image to Text

The following example demonstrates image to text generation capability using the Hugging Face: Serverless Inference action:


Hugging Face Automation Anywhere Image to Text example
Note: Convert the image to a Base64 format using Base64 Conversion >> Base64 Encode action before feeding it to the model. The model requires the image in this format to accurately generate text.

Text to Audio

The following examples demonstrates text to audio generation capability using the Hugging Face: Serverless Inference action:


Hugging Face Automation Anywhere Text to Audio example
Note: Provide a Data File Path with an extension to which the input prompt text in the Request payload is converted to an audio format.