HuggingFaceClient
HuggingFace Inference API client for image generation.
This client provides access to HuggingFace's hosted diffusion models through their Inference API. It supports popular models like Stable Diffusion and other text-to-image models available on the HuggingFace Hub.
Value parameters
- config
-
Configuration containing API key and model settings
Attributes
- Example
-
val config = HuggingFaceConfig( apiKey = "your-hf-token", model = "stabilityai/stable-diffusion-2-1" ) val client = new HuggingFaceClient(config) client.generateImage("a beautiful sunset over mountains") match { case Right(image) => println(s"Generated image: $${image.size}") case Left(error) => println(s"Error: $${error.message}") } - Graph
-
- Supertypes
Members list
Value members
Concrete methods
Builds the JSON payload required for the HuggingFace Inference API based on the provided prompt and image generation options.
Builds the JSON payload required for the HuggingFace Inference API based on the provided prompt and image generation options.
Value parameters
- options
-
The image generation options such as size, seed, guidance scale, etc.
- prompt
-
The text description for the image generation.
Attributes
- Returns
-
Either an
ImageGenerationErrorif payload creation fails, or the JSON string representing the payload.
Converts the byte content of an HTTP response into a Base64-encoded string. If an error occurs during the conversion process, it wraps the exception into an ImageGenerationError.
Converts the byte content of an HTTP response into a Base64-encoded string. If an error occurs during the conversion process, it wraps the exception into an ImageGenerationError.
Value parameters
- response
-
The HTTP response containing the byte data to convert.
Attributes
- Returns
-
Either an
ImageGenerationErrorin case of a failure or the resulting Base64-encoded string on success.
Serializes a HuggingClientPayload object into a JSON string.
Serializes a HuggingClientPayload object into a JSON string.
Value parameters
- huggingClientPayload
-
The payload object containing input text and generation parameters to be serialized into JSON format.
Attributes
- Returns
-
The JSON string representation of the provided
HuggingClientPayloadobject.
Generates multiple images based on the given text prompt using predefined options and base64-encoded image data.
Generates multiple images based on the given text prompt using predefined options and base64-encoded image data.
This method creates a sequence of GeneratedImage objects by iteratively adding offsets to the provided seed (if present in the options). It handles errors by returning an ImageGenerationError wrapped in an Either.
Value parameters
- base64Data
-
The base64-encoded image data that will be used to populate each generated image.
- count
-
The number of images to generate. Ensures the specified number of iterations in the generation process.
- options
-
Optional parameters for image generation, including size, format, seed, guidance scale, etc.
- prompt
-
The text description of the images to be generated.
Attributes
- Returns
-
Either an
ImageGenerationErrorin case of a generation issue, or anIndexedSeqofGeneratedImageobjects containing all successfully created images.
Generate a single image from a text prompt using HuggingFace Inference API.
Generate a single image from a text prompt using HuggingFace Inference API.
Value parameters
- options
-
Optional generation parameters like size, guidance scale, etc.
- prompt
-
The text description of the image to generate
Attributes
- Returns
-
Either an error or the generated image
- Definition Classes
Generate multiple images from a text prompt using HuggingFace Inference API.
Generate multiple images from a text prompt using HuggingFace Inference API.
Note: HuggingFace Inference API typically returns one image per request, so multiple images are generated by making the same request multiple times with different seeds.
Value parameters
- count
-
Number of images to generate (1-4)
- options
-
Optional generation parameters like size, guidance scale, etc.
- prompt
-
The text description of the images to generate
Attributes
- Returns
-
Either an error or a sequence of generated images
- Definition Classes
Check the health status of the HuggingFace Inference API.
Check the health status of the HuggingFace Inference API.
Attributes
- Returns
-
Either an error or the current service status
- Definition Classes
Makes an HTTP POST request to the HuggingFace Inference API to send a payload and retrieve a response.
Makes an HTTP POST request to the HuggingFace Inference API to send a payload and retrieve a response.
Value parameters
- payload
-
The JSON payload to send with the HTTP request.
Attributes
- Returns
-
Either an ImageGenerationError if the request fails or a successful
requests.Responseobject.
Validates the provided count to ensure it is within the allowable range for image generation (1 to 4 inclusive, for HuggingFace).
Validates the provided count to ensure it is within the allowable range for image generation (1 to 4 inclusive, for HuggingFace).
Value parameters
- count
-
The number of images requested for generation. Must be between 1 and 4.
Attributes
- Returns
-
Either an
ImageGenerationErrorif the count is out of range, or the valid count as anIntif the validation succeeds.
Validates the provided prompt to ensure it is not empty or blank.
Validates the provided prompt to ensure it is not empty or blank.
Value parameters
- prompt
-
The input string representing the prompt to validate.
Attributes
- Returns
-
Either an
ImageGenerationErrorif the validation fails, or the original valid prompt as aString.