What’s an API?

An API, which stands for Application Programming Interface, allows to communicate software apps to each other. In other words, they can ask for information and share data. This helps apps work together and do task like sending message or getting information from the internet.

How does ML / LLM API works?

Figure 1 illustrates the architecture of the ML/LLM API, which initiated with a request containing the input data (it can be an image, text, voice, etc.,depending on your task) from a software application to the API. The API then facilities the transmission of this input data to the ML/LLM model application. Once the ML/LLM model processes the input data, it generates predictions based on the input and sends a response containing the prediction as output back to the software application.

Figure 1: Architecture of ML/LLM model

The Hugging Face Inference API works like that.

What is Inference API?

The Hugging Face Inference API is a complimentary service that enables users to execute models hosted on Hugging Face for different tasks, as demonstrated in Figure 2. It is well-suited for prototyping applications.

Getting Started with API Hugging Face

Figure 2: Tasks in Hugging Face

To utilize the Hugging Face Inference API, users only have to send an HTTP request with their input data. The API will then give back the predicted output data, as shown in Figure 1.

If you need an inference solution for production, check out Inference Endpoints

OBTAINING HUGGING FACE API TOKEN

To obtain your hugging face api token, first you need to create your account in Hugging Face.

1. Create a Hugging Face account

Step 1: Edit the following boxes shown in the Figure 3. And Next

Getting Started with API Hugging Face

Figure 3: Form part 1

Step 2: Edit the following boxes, check on I have a read and agree with the Terms of Service and the Code of Conduct such as the Figure 4.

Figure 4: Form part 2

Step 3: Finally, you need to confirm your account by clicking on the confirmation link sent to your email, as indicated in Figure 5.

The process will be completed, similar to what is shown in Figure 6.

Figure 5: Check email view

Figure 6: email address verified

2. Obtaining Access Token

Once you’ve done the previous process. You are already for getting the token.

Step 1: Click on setting.

Figure 7: Setting

Step 2: Click on Access Token.

Figure 8: Access Tokens

Step 3: Click on New Token

Figure 9: New Token

Step 4: Edit the following fields, as illustrated in the Figure 10, and click on Generate a token.

Figure 10: Create a new access token

Step 5: Finally, you have successfully created your first Access Token. Congratulations 😄!!! You will use it in the next section.

Figure 11: Token created

You can copy your access token by clicking on the icon located to the right.

USING HUGGING FACE API TOKEN

In this section we are going to code in Python using ***Google Colab***.

GPT 2 for text generation

# Import the InferenceClient class from the huggingface_hub library.
# This class is used to interact with the Hugging Face Inference API.
from huggingface_hub import InferenceClient

# Create an instance of the InferenceClient.
# The `model` argument specifies the model identifier from Hugging Face's Model Hub.
# In this case, "gpt2" is used, which is a text generation model.
# The `token` argument is your Hugging Face API token for authentication.
# This token grants you access to the API, so it should be kept secure.
client = InferenceClient(model="gpt2", token="HUGGINGFACE_TOKEN")

# Define the input text for the model.
# This is the prompt that the model will use as the basis for generating text.
input_text = "Who are you?"

# Call the text generation API using the `text_generation` method of the client.
# The input text is passed to the model, and the model generates a response based on it.
# The response includes the generated text and possibly other metadata.
response = client.text_generation(input_text)

# Print the generated text to the console.
# The `response` object contains the output from the model, which in this case is the text
# generated based on the input prompt "Who are you?".
print(response)

If you’re looking to jumpstart your career as a data analyst, consider enrolling in our comprehensive Data Analyst Bootcamp with Internship program. Our program provides you with the skills and experience necessary to succeed in today’s data-driven world. You’ll learn the fundamentals of statistical analysis, as well as how to use tools such as SQL, Python, Excel, and PowerBI to analyze and visualize data designed by Mohammad Arshad, 18 years of   Data Science & AI Experience. But that’s not all – our program also includes a 3-month internship with us where you can showcase your Capstone Project.

Are you passionate about AI and Data Science? Looking to connect with like-minded individuals, learn new concepts, and apply them in real-world situations? Join our growing AI community today! We provide a platform where you can engage in insightful discussions, share resources, collaborate on projects, and learn from experts in the field.

Don’t miss out on this opportunity to broaden your horizons and sharpen your skills. Visit https://nas.io/artificialintelligence and be part of our AI community. We can’t wait to see what you’ll bring to the table. Let’s shape the future of AI together!

Your Guide to Building AI Magic with LangChain LLM

Leave a Reply

Your email address will not be published. Required fields are marked *

Need help?