DelphiHuggingFace
The Hugging Face API wrapper for Delphi leverages cutting-edge models to deliver powerful features, including object detection, music generation, text classification, sentiment analysis, image segmentation, speech-to-text transcription, and text generation.
Install / Use
/learn @MaxiDonkey/DelphiHuggingFaceREADME
Delphi Hugging Face API
- Introduction
- Remarks
- Tools for simplifying this tutorial
- Asynchronous callback mode management
- Exploration Journey
- Common Ground Functionalities Across API Ecosystems
- Contributing
- License
Introduction
Hugging Face Hub is an open-source collaborative platform dedicated to democratizing access to artificial intelligence (AI) technologies. This platform hosts a vast collection of models, datasets, and interactive applications, facilitating the exploration, experimentation, and integration of AI solutions into various projects. Official page
Resources available on Hugging Face Hub
- Models: The Hub offers a multitude of pre-trained models covering domains such as natural language processing (NLP), computer vision, and audio recognition. These models are suited for various tasks, including text generation, classification, object detection, and speech transcription.
- Datasets: A diverse library of datasets is available for training and evaluating your own models, providing a foundation for developing customized solutions.
- Spaces: The Hub hosts interactive applications that allow you to visualize and test models directly from a browser. These spaces are useful for demonstrating model capabilities or conducting quick analyses.
Serverless Inference API
Hugging Face Hub offers a Inference API, enabling rapid integration of AI models into your projects without the need for complex infrastructure management.
<br/>Advantages of using Hugging Face Hub
- Time-saving: Models are ready to use, eliminating the need to train or deploy them locally, which accelerates the development of applications.
- Scalability: The Hub's infrastructure ensures automatic scaling, load balancing, and efficient caching.
In summary, Hugging Face Hub is a resource for integrating AI models into projects. With its serverless Inference API and collection of ready-to-use resources, it offers an solution to enhance applications with AI capabilities while simplifying their implementation and maintenance.
<br/>Rate Limits and Supported Models
By subscribing, you gain access to thousands of models. You can explore the benefits of individual, professional, and enterprise subscriptions by following the links below:
<br/>Licenses and Compliance
When integrating models or datasets from Hugging Face Hub into your projects, it is crucial to pay close attention to the associated licenses. Every resource hosted on the platform comes with a specific license that outlines the terms of use, modification, and distribution. A thorough understanding of these licenses is essential to ensure the legal and ethical compliance of your developments.
Why is this important?
- Legal compliance: Using a resource without adhering to its license terms can lead to legal violations, exposing your project to potential risks.
- Respect for creators' rights: Licenses protect the rights of creators. By respecting them, you acknowledge and honor their work.
- Transparency and ethics: Following the conditions of licenses promotes responsible and ethical use of open-source technologies.
Refer to the Model Card or Dataset Card for each model or dataset used in your application.
Tutorial content
The Hugging Face Hub provides open-source libraries such as Transformers, enables integration with Gradio, and offers evaluation tools like Evaluate. However, these aspects will not be covered in this tutorial, as they are beyond the scope of this document.
Instead, this tutorial will focus on using the APIs with Delphi, highlighting key features such as image and sound classification, music generation (music-gen), sentiment analysis, object detection in images, image segmentation, and all natural language processing (NLP) functions.
Remarks
<br/>[!IMPORTANT]
This is an unofficial library. Hugging Face does not provide any official library for
Delphi. This repository containsDelphiimplementation over Hugging Face public API.
Tools for simplifying this tutorial
To simplify the example codes provided in this tutorial, I have included two units in the source code: VCL.Stability.Tutorial and FMX.Stability.Tutorial. Depending on the option you choose to test the provided source code, you will need to instantiate either the TVCLStabilitySender or TFMXStabilitySender class in the application's OnCreate event, as follows:
[!TIP]
//uses VCL.HuggingFace.Tutorial; HFTutorial := TVCLHuggingFaceSender.Create(Memo1, Image1, Image2, MediaPlayer1);or
//uses FMX.HuggingFace.Tutorial; HFTutorial := TFMXHuggingFaceSender.Create(Memo1, Image1, Image2, MediaPlayer1);
Make sure to add a TMemo, two TImage and a TMediaPlayer component to your form beforehand.
Asynchronous callback mode management
In the context of asynchronous methods, for a method that does not involve streaming, callbacks use the following generic record: TAsynCallBack<T> = record defined in the HuggingFace.Async.Support.pas unit. This record exposes the following properties:
TAsynCallBack<T> = record
...
Sender: TObject;
OnStart: TProc<TObject>;
OnSuccess: TProc<TObject, T>;
OnError: TProc<TObject, string>;
<br/>
For methods requiring streaming, callbacks use the generic record TAsynStreamCallBack<T> = record, also defined in the HuggingFace.Async.Support.pas unit. This record exposes the following properties:
TAsynCallBack<T> = record
...
Sender: TObject;
OnStart: TProc<TObject>;
OnSuccess: TProc<TObject>;
OnProgress: TProc<TObject, T>;
OnError: TProc<TObject, string>;
OnCancellation: TProc<TObject>;
OnDoCancel: TFunc<Boolean>;
The name of each property is self-explanatory; if needed, refer to the internal documentation for more details.
<br/>Exploration Journey
This part of this document is designed to reflect the path I took while uncovering the features and possibilities of Hugging Face Hub APIs. Rather than presenting a rigid tutorial, I chose to structure it as an Exploration Journey to capture the iterative, curious, and hands-on process of discovery. Each step builds on the previous one, showcasing not only what I found but how I approached and learned from the API ecosystem."
Initialization
To initialize the API instance, you need to obtain an API key from Hugging Face.
Once you have a token, you can initialize the IHuggingFace interface, which serves as the entry point to the API.
[!NOTE]
uses HuggingFace; var HuggingFace := THuggingFaceFactory.CreateInstance(API_KEY);
When accessing the list of models or retrieving the description of a specific model, a different endpoint is used than the API endpoint. To instantiate this interface, use the following code:
uses HuggingFace;
var HFHub := THuggingFaceFactory.CreateInstance(API_KEY, True);
[!Warning] To use the examples provided in this tutorial, especially to work with asynchronous methods, I recommend defining the HuggingFace interface with the widest possible scope. <br/> So, set
HuggingFace := THuggingFaceFactory.CreateInstance(My_Key);in theOnCreateevent o
