cubic

package module
v1.6.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2023 License: Apache-2.0 Imports: 12 Imported by: 3

Documentation

Overview

Package cubic provides for interacting with an instance of cubic server using GRPC for performing speech recognition.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is an object for interacting with the Cubic GRPC API.

All methods except Close may be called concurrently.

func NewClient

func NewClient(addr string, opts ...Option) (*Client, error)

NewClient creates a new Client that connects to a Cubic Server listening on the provided address. Transport security is enabled by default. Use Options to override default settings if necessary.

func (*Client) Close

func (c *Client) Close() error

Close closes the connection to the API service. The user should only invoke this when the client is no longer needed. Pending or in-progress calls to other methods may fail with an error if Close is called, and any subsequent calls with this client will also fail.

func (*Client) CompileContext added in v1.5.0

func (c *Client) CompileContext(
	ctx context.Context, modelID, token string, phrases []string, boostValues []float32,
) (*cubicpb.CompileContextResponse, error)

CompileContext compiles the given list of phrases or words into a compact, fast to access form for Cubic, which may later be provided in a `Recognize` or `StreamingRecognize` call to aid speech recognition.

The modelID is the unique identifier of the model to compile the context information for. The model chosen needs to support context which can be verified by checking its ModelAttributes.ContextInfo obtained via `ListModels`.

The token is a string allowed by the model being used, such as "name" or "airport", that is used to determine the place in the recognition output where the provided list of phrases or words may appear. The allowed tokens for a given model can be found in its ModelAttributes.ContextInfo obtained via the `ListModels` method.

The phrases (or words) are given as a slice of strings.

Optionally, a slice of positive floating numbers, boostValues, may also be provided, one for each phrase or word in the phrase list. These values can be used to increase the likelihood of the corresponding entry in the phrases by setting a higher value for it. The boostValues slice can be set to nil if boosting is not required.

If given boostValues, the new probability of the corresponding phrase entry becomes (boost + 1.0) * old probability. By default, all provded phrases or words are given an equal probability of 1/N, where N = total number of phrases or words.The new probabilities are normalized after boosting so that they sum to one. This means that if all phrases are given the same boost value, they will still have the same default likelihood. This also means that the boost value can be any positive value, but for most cases, values between 0 to 10 work well. Negative values may be provided but they will be treated as 0 (no boost).

func (*Client) ListModels

func (c *Client) ListModels(ctx context.Context) (*cubicpb.ListModelsResponse, error)

ListModels retrieves a list of available speech recognition models

func (*Client) Recognize

Recognize performs synchronous speech recognition and returns after all audio has been processed. It is expected that this request be used for short audio segments (less than a minute long). For longer content, the `StreamingRecognize` method should be used.

func (*Client) StreamingRecognize

func (c *Client) StreamingRecognize(ctx context.Context,
	cfg *cubicpb.RecognitionConfig, audio io.Reader,
	handlerFunc RecognitionResponseHandler) error

StreamingRecognize uses the bidirectional streaming API for performing speech recognition. It sets up recognition using the given cfg.

Data is read from the given audio reader into a buffer and streamed to cubic server. The default buffer size may be overridden using Options when creating the Client.

As results are received from the cubic server, they will be sent to the provided handlerFunc.

If any error occurs while reading the audio or sending it to the server, this method will immediately exit, returning that error.

This function returns only after all results have been passed to the resultHandler.

func (*Client) Version

func (c *Client) Version(ctx context.Context) (*cubicpb.VersionResponse, error)

Version queries the server for its version

type Option

type Option func(*Client) error

Option configures how we setup the connection with a server.

func WithClientCert

func WithClientCert(certPem []byte, keyPem []byte) Option

WithClientCert returns an Option which sets up the given PEM certificate and key as the credentials presented by this Client when connecting to a server. Use this when setting up mutually authenticated TLS.

func WithConnectTimeout added in v1.4.0

func WithConnectTimeout(t time.Duration) Option

WithConnectTimeout returns an Option that configures the timeout for establishing grpc connection with the server. Use this only when you are on a slow network and when Cobalt recommends you to do so.

func WithInsecure

func WithInsecure() Option

WithInsecure returns an Option which disables transport security for this Client. Use this when connecting to a non-TLS enabled cubic server, such as during debugging.

func WithServerCert

func WithServerCert(cert []byte) Option

WithServerCert returns an Option which sets up the given PEM certificate as a root certificate that can validate the certificate presented by the server we are connecting to. Use this when connecting to an instance of cubic server that is using a self-signed certificate.

func WithStreamingBufferSize

func WithStreamingBufferSize(n uint32) Option

WithStreamingBufferSize returns an Option that sets up the buffer size (bytes) of each message sent from the Client to the server during streaming GRPC calls. Use this only if Cobalt recommends you to do so. A value n>0 is required.

type RecognitionResponseHandler

type RecognitionResponseHandler func(*cubicpb.RecognitionResponse)

RecognitionResponseHandler is a type of callback function that will be called when the `StreamingRecognize` method is running. For each response received from cubic server, this method will be called once. The provided RecognitionResponse is guaranteed to be non-nil. Since this function is executed as part of the streaming process, it should preferably return quickly and certainly not block.

Directories

Path Synopsis
gw Module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL