avi0ra/huggingface

0.2.1

Connects Ballerina applications to the Hugging Face Hub Inference API for running state-of-the-art machine learning models.

This package provides a Client generated from the Hugging Face OpenAPI definition and exposes resources for common tasks such as:

  • Chat completions
  • Text generation
  • Text and token classification
  • Summarization and translation
  • Embeddings (feature extraction)
  • Image classification
  • Automatic speech recognition

Configure the connector

The connector authenticates with the Hugging Face Hub using a bearer token.

You can provide this token as an environment variable:

  • Environment variable: HF_TOKEN

Or via the ConnectionConfig record when constructing the client.

Copy
import avi0ra/huggingface;
import ballerina/io;
import ballerina/os;

configurable string? token = os:getEnv("HF_TOKEN");

public function initHuggingFaceClient() returns huggingface:Client|error {
    if token is string {
        huggingface:ConnectionConfig config = {
            auth: {
                token: token
            }
        };
        return new (config);
    }

    return error("HF_TOKEN is not set; set it in the environment or Config.toml");
}

Basic usage

Chat completions

Copy
import avi0ra/huggingface;
import ballerina/io;
import ballerina/os;

configurable string? token = os:getEnv("HF_TOKEN");

public function main() returns error? {
    if token is string {
        huggingface:Client hfClient = check new ({auth: {token}});

        huggingface:ChatCompletionResponse resp = check hfClient->/v1/chat/completions.post({
            model: "meta-llama/Llama-3.2-3B-Instruct",
            messages: [
                {role: "user", content: "Say hello in one sentence."}
            ],
            maxTokens: 32
        });

        if resp?.choices is huggingface:ChatCompletionChoice[] {
            io:println(resp.choices[0].message?.content);
        }
    } else {
        io:println("HF_TOKEN is not set; configure it in the environment or Config.toml.");
    }
}

Text generation

Copy
import avi0ra/huggingface;
import ballerina/io;
import ballerina/os;

configurable string? token = os:getEnv("HF_TOKEN");

public function main() returns error? {
    if token is string {
        huggingface:Client hfClient = check new ({auth: {token}});

        huggingface:TextGenerationResult[] res = check hfClient->/hf\-inference/models/["gpt2"].post({
            inputs: "Ballerina is designed for",
            parameters: {
                maxNewTokens: 20,
                returnFullText: false
            }
        });

        if res.length() > 0 {
            io:println(res[0].generatedText);
        }
    } else {
        io:println("HF_TOKEN is not set; configure it in the environment or Config.toml.");
    }
}

Import

import avi0ra/huggingface;Copy

Other versions

See more...

Metadata

Released date: 3 months ago

Version: 0.2.1

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.13.1

GraalVM compatible: Yes


Pull count

Total: 186

Current verison: 10


Weekly downloads


Source repository


Keywords

huggingface

ai

llm

inference

machine-learning

nlp


Contributors