Guide to Using AI Models on PubNub using HuggingFace API

4 min read Stephen Blum on Apr 25, 2024

In this guide we’ll use AI models with PubNub Functions to create a globally distributed and secure application. We'll use HuggingFace for the AI models. This integration enables you to use AI capabilities within a PubNub function, providing real-time interaction and data processing capabilities. We will cover setting up your HuggingFace and PubNub accounts, deploying an AI model and ensuring secure access.

Setting Up HuggingFace

HuggingFace is the GitHub of AI models. HuggingFace hosts AI model training code, the data to train the models and simplifies the deployment and hosting of AI models. This makes it easy for developers to add AI into apps.

  1. Create a HuggingFace Account: Visit and sign up for an account. This account will allow you to access and deploy AI models.

  2. Find Your Model: Browse through the HuggingFace model repository to find a model that fits your needs. For demonstration purposes, we’ll example models such as distilbert-base-uncased-finetuned-sst-2-english for sentiment analysis and gpt2 for text generation.

  3. Generate an API Key: In your HuggingFace account settings, find the section for API keys and generate a new one. This key is essential for authenticating your requests from PubNub to HuggingFace. The HuggingFace API key can be found here:

Setting Up PubNub

PubNub real-time infrastructure-as-a-service provides APIs for messaging, push notifications, and real-time updates. PubNub APIs exchange data between devices, allowing them to communicate. With PubNub Functions + HuggingFace you can intercept each message between the devices and run AI models. Here’s how to get started with PubNub:

  1. Sign Up for PubNub: If you haven’t already, create an account on PubNub.

  2. Create a New App: In the PubNub Dashboard, create a new app. Within this app, you’ll create a keyset that includes a publish and subscribe key. These keys are crucial for integrating with the PubNub network.

  3. Create a Function: Within your app, create a new function. This serverless function will be the bridge between PubNub Edge network and the HuggingFace AI APIs, processing incoming messages and utilizing the AI model to generate responses.

Writing the PubNub Function

The source code for the PubNub Function will connect to the AI model. This function listens for incoming messages, runs them through the AI model, and logs the AI response. Based on the AI response, you can alter the message or block it if needed. For now we'll just console.log() the output.

Source Code:

Deployment Instructions:

  1. Input the AI Models: In the code, replace the model variable with the model you wish to use from HuggingFace.

  2. Secure Your API Key: Store your HuggingFace API key in the PubNub Function vault for secure access. This prevents exposing your HuggingFace API key to your end-users.

  3. Deploy the Function: Click "Start Module". Once your function is set up and your API key is securely stored in the vault, deploy the function within the PubNub Dashboard clicking the "Start Module" button in the top right of the functions editor screen. Once deployed, the function will automatically process messages sent through the configured PubNub channel.

  4. Testing the Function: Send a test message through PubNub. You can do this with the "Test Payload" section on the left panel of the PubNub Functions editor screen. The PubNub Function should activate, processing the message through the AI model and log the response. This response can be further used to change or block the message if needed.

HuggingFace AI models + your PubNub Publish/Subscribe messages adds new options for your real-time data and new kinds of interactions between users. A good start can be added safety mechanics using the NLP model. Following the steps outlined above, you’ve learned how to set up an environment where you can leverage the power of AI models securely and efficiently. Always ensure your API keys are safely stored and manage your request rates to stay within HuggingFace’s API usage policies. With this setup, you’re ready to use any and all AI models! You can seamlessly integrate them into your PubNub real-time apps 🎉

We explored the integration of AI models from HuggingFace into PubNub Functions, enabling you access to all AI models. The PubNub Function JavaScript code uses HTTP requests to the HuggingFace API, sending text inputs and receiving AI-processed outputs. The PN Function leverages PubNub’s vault feature to securely store and access the HuggingFace API key, ensuring that sensitive information is kept confidential. The function provides example usages of models, and the developer can insert their chosen model into the function code and follow the deployment (or re-deployment) instructions to activate the function on PubNub. This will globally deploy your JS edge function to all AWS zones. Your end-users will automatically be routed to their nearest AWS zone for the best API performance and speed. This creates an improved user experience.

By following this guide, developers can harness the combined power of PubNub’s real-time messaging and HuggingFace’s AI models to add any AI model into their app. Sentiment analysis in social media feeds and generating dynamic responses in chatbots are all possible use cases. Secure handling of API keys and many AI models to choose, this guide showed you how to implement AI models on top of the Publish Subscribe model powering PubNub's real-time API.