Giter VIP home page Giter VIP logo

clarifai-java's Introduction

Clarifai Java Client

Release

A simple client for the Clarifai v2 API.

NOTE: If you are planning to contribute to this API client, please see the CONTRIBUTING.md file.

Installation

Gradle:

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

compile 'com.clarifai.clarifai-api2:core:[version]'

Maven:

Add the following to your dependencies:

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

Getting Started

To create a ClarifaiClient instance, do the following:

final ClarifaiClient client = new ClarifaiBuilder("appID", "appSecret").buildSync();

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

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.

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.

clarifai-java's People

Contributors

kevinmost avatar keithito avatar atroutt avatar zeiler avatar

Watchers

James Cloos avatar dev.with avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.