Clarifai

Clarifai Java API Client

License

License

GroupId

GroupId

com.clarifai.clarifai-api2
ArtifactId

ArtifactId

core
Last Version

Last Version

2.8.1
Release Date

Release Date

Type

Type

jar
Description

Description

Clarifai
Clarifai Java API Client
Project URL

Project URL

https://github.com/Clarifai/clarifai-java
Source Code Management

Source Code Management

https://github.com/Clarifai/clarifai-java

Download core

How to add to project

<!-- https://jarcasting.com/artifacts/com.clarifai.clarifai-api2/core/ -->
<dependency>
    <groupId>com.clarifai.clarifai-api2</groupId>
    <artifactId>core</artifactId>
    <version>2.8.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.clarifai.clarifai-api2/core/
implementation 'com.clarifai.clarifai-api2:core:2.8.1'
// https://jarcasting.com/artifacts/com.clarifai.clarifai-api2/core/
implementation ("com.clarifai.clarifai-api2:core:2.8.1")
'com.clarifai.clarifai-api2:core:jar:2.8.1'
<dependency org="com.clarifai.clarifai-api2" name="core" rev="2.8.1">
  <artifact name="core" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.clarifai.clarifai-api2', module='core', version='2.8.1')
)
libraryDependencies += "com.clarifai.clarifai-api2" % "core" % "2.8.1"
[com.clarifai.clarifai-api2/core "2.8.1"]

Dependencies

compile (7)

Group / Artifact Type Version
com.squareup.okhttp3 : okhttp jar 3.12.0
com.google.code.gson : gson jar 2.8.5
com.google.protobuf : protobuf-java jar 3.6.1
io.grpc : grpc-netty-shaded jar 1.17.1
io.grpc : grpc-protobuf jar 1.17.1
io.grpc : grpc-stub jar 1.17.1
com.google.protobuf : protobuf-java-util jar 3.6.1

Project Modules

There are no modules declared in this project.

Clarifai logo

DEPRECATED

This API Client is no longer supported.

Please use Clarifai Java gRPC instead, which is faster and more feature-rich.

Clarifai API Java Client

Release Build Status Build Status

Installation

Gradle:

Add the following to the dependencies section of your build.gradle:

// Add the client to your dependencies:
dependencies {
    compile 'com.clarifai.clarifai-api2:core:[version]'
}

// Make sure you have the Maven Central Repository in your Gradle File
repositories {
    mavenCentral()
}

Maven:

Add the following to your dependencies:

<dependency>
  <groupId>com.clarifai.clarifai-api2</groupId>
  <artifactId>core</artifactId>
  <version>[version]</version>
</dependency>

Jars:

All links hosted by Maven Central (https://repo1.maven.org/):

Getting Started

There are two authentication approaches. The recommended is to use API Keys. You may also use the now deprecated app ID & secret pair. See the Authentication section of the docs for more info.

To create a ClarifaiClient instance with an API Key do the following:

final ClarifaiClient client = new ClarifaiBuilder("apiKey").buildSync();

The ClarifaiBuilder optionally allows you to pass in a custom OkHttpClient (allowing for user-defined parameters such as connection timeouts, etc):

final ClarifaiClient client = new ClarifaiBuilder(apiKey)
    .client(new OkHttpClient.Builder()
        .connectTimeout(60, TimeUnit.SECONDS)
        .readTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(60, TimeUnit.SECONDS)
        .addInterceptor(new HttpLoggingInterceptor(logger::info).setLevel(HttpLoggingInterceptor.Level.BASIC))
        .build()
    )
    .buildSync();

Making API requests

Network operations using the API client only occur by calling .executeSync() or .executeAsync(...) on a ClarifaiRequest<T> object.

All methods on the ClarifaiClient will either return a ClarifaiRequest<T> or ClarifaiPaginatedRequest<T>, or a custom object that allows you to specify parameters that go into ultimately building a ClarifaiRequest<T> or ClarifaiPaginatedRequest<T>.

Using .executeSync() will block the current thread and return a ClarifaiResponse<T>, where T is the returned data type. ClarifaiResponse<T> has methods to check the success or failure status of the method, and methods that mimic Java 8 Optional<T> to safely retrieve the returned data.

Using .executeAsync() returns void, but allows the user to pass in callback(s) to handle successful responses, failed responses, and/or network errors.

ClarifaiPaginatedRequest<T> objects should be thought of as factories that create ClarifaiRequest<T>s. When building a ClarifaiPaginatedRequest<T>, you have the option of specifying a perPage (the number of elements in each page of the response).

Once a ClarifaiPaginatedRequest<T> is built, you can call ClarifaiPaginatedRequest#getPage(int) to get back a ClarifaiRequest<T> for the specified page. Pages are 1-indexed. Currently, the API does not indicate how many elements there are in a paginated request in total, but this is planned for the future.

Example Requests

Predict the contents of an image:

Model<Concept> generalModel = client.getDefaultModels().generalModel();

PredictRequest<Concept> request = generalModel.predict().withInputs(
        ClarifaiInput.forImage("https://samples.clarifai.com/metro-north.jpg")
    );
List<ClarifaiOutput<Concept>> result = request.executeSync().get();

Predict the contents of a video:

Model<Frame> generalVideoModel = client.getDefaultModels().generalVideoModel();

PredictRequest<Frame> videoRequest = generalVideoModel.predict().withInputs(
    ClarifaiInput.forVideo("https://samples.clarifai.com/beer.mp4")
);
List<ClarifaiOutput<Frame>> videoResults = videoRequest.executeSync().get();

See the developer guide for more detailed examples and examples of other features such as custom training and search.

Using API responses

All responses from the API are immutable data types (constructed using AutoValue). Some of these types, such as ClarifaiModel, are also used as parameters to make requests (for example, you can either get a model as a response from the API, or pass a model to the API to create it in your account). Builders are exposed to the user for all data types that they can use as request params.

Some convenience methods are provided as well on data types; eg: myModel.predict() on ClarifaiModel.

This allows you to make requests in a fluent, object-oriented way. For example:

client.getModelByID("myID").executeAsync(
    model -> model.predict()
        .withInputs(input)
        .executeAsync(
            outputs -> System.out.println("First output of this prediction is " + outputs.get(0))
        ),
    code -> System.err.println("Error code: " + code + ". Error msg: " + message),
    e -> { throw new ClarifaiException(e); }
);

Requirements

JDK 7 or later.

Android

The client will work on Android Gingerbread and higher (minSdkVersion 9).

You need to add the INTERNET permission to your AndroidManifest.xml, as follows:

<uses-permission android:name="android.permission.INTERNET" />

The Android Linter may also give an "InvalidPackage" error. This error may be safely ignored, and is caused by OkHttp using Java 8 methods when they are available (which will not occur on Android). To suppress these linter errors, do NOT disable your linter. Simply follow the instructions here.

Getting Help

If you need any help with using the library, please contact Support at [email protected] or our Developer Relations team at [email protected].

If you've found a bug or would like to make a feature request, please make an issue or a pull request here.

Contributions are welcome, see CONTRIBUTING.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

com.clarifai.clarifai-api2

Clarifai

Versions

Version
2.8.1
2.8.0
2.8.0-alpha1
2.7.0
2.6.1
2.6.0
2.5.2
2.5.1
2.5.0.dirty
2.5.0
2.4.0
2.3.0
2.2.12
2.2.11
2.2.10
2.2.9.dirty
2.2.9
2.2.8
2.2.7
2.2.6
2.2.5.dirty
2.2.5
2.2.3
2.2.2
2.2.1
2.2.0
2.1.0
2.0.2
2.0.1
2.0.0
2.0.0-rc1
2.0.0-beta22
2.0.0-beta21
2.0.0-beta19-2d15494