Clarifai client library for C#
Clone or download

README.md

Clarifai logo

Clarifai API C# Client

NuGet Build Status Build status

Installation

Adding the Clarifai C# library to your project

Within Visual Studio IDE:

Install-Package Clarifai

with the dotnet command line tool:

dotnet add package Clarifai

Prerequisites

This library supports .NET Standard 1.3 which means it can be used on the following .NET implementations:

  • .NET Framework 4.6+
  • .NET Core 1.0+
  • Mono 4.6+
  • Xamarin.iOS 10.0+
  • Xamarin.Mac 3.0+
  • Xamarin.Android 7.0+
  • UWP 10.0+

Getting Started

Find complete code samples in the docs.

using System;
using Clarifai.API;
using Clarifai.DTOs.Inputs;

namespace hello_csharp
{
    internal class Program
    {
        public static void Main()
        {
            // With `CLARIFAI_API_KEY` defined as an environment variable
            var client = new ClarifaiClient().
            
            // When passed in as a string
            var client = new ClarifaiClient("YOUR_CLARIFAI_API_KEY");
            
            // When using async/await
            var res = await client.PublicModels.GeneralModel
                .Predict(new ClarifaiURLImage("https://samples.clarifai.com/metro-north.jpg"))
                .ExecuteAsync()
                            
            // When synchronous
            var res = client.PublicModels.GeneralModel
                .Predict(new ClarifaiURLImage("https://samples.clarifai.com/metro-north.jpg"))
                .ExecuteAsync()
                .Result;

            // Print the concepts
            foreach (var concept in res.Get().Data)
            {
                Console.WriteLine($"{concept.Name}: {concept.Value}");
            }
        }
    }
}

Public models can easily be used:

var response = await client.PublicModels.GeneralModel.Predict(new ClarifaiURLImage("IMAGE URL"))
    .ExecuteAsync();

As well as custom trained:

var response = await client.Predict<Concept>(
        "YOUR_MODEL_ID",
        new ClarifaiURLImage("https://samples.clarifai.com/metro-north.jpg")
    )
    .ExecuteAsync();

Generics and casting

The library uses generics to specify the type of an output. In the first example using the Predict method, we use Concept to indicate that the model is a concept model. If we'd use a model ID of our public color model, we'd use Color instead of Concept.

Not all methods can however use generics. For example, the GetModels is able to return multiple models and you cannot determine in advance (at compile time) the types of model that will be returned. The request therefore returns a list of IModel objects. You can cast these instances to specific models, if you know its type:

if (models[0].OutputInfo.TypeExt == "concept")
{
    ConceptModel model = (ConceptModel) models[0];
}

In a similar way you may cast IPrediction when the actual class type is not known at compile time.

Please see more in the Developer's Guide.

Tests

NUnit 3 VS Test Adapter must be installed.

Unit tests

Run all unit tests using .NET Core by running this command in the solution directory:

dotnet test Clarifai.UnitTests/Clarifai.UnitTests.csproj

Integration tests

To successfully run integration tests, you have to have a valid Clarifai API key with all required permissions.

Create a new API key at the API keys page and set it as an environmental variable CLARIFAI_API_KEY.

Warning: The requests made by integration tests are run against the production system and will use your operations.

dotnet test Clarifai.IntegrationTests/Clarifai.IntegrationTests.csproj

License

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