Rank
Search your data based on concepts or visual similarity
Rank order your search results with the intuitive insights of an AI. Your model can identify concepts in your data and rank search results by how confident it is that a given concept is present.
You can even rank search results by how similar one input is to another input.
The initialization code used in the following example is outlined in detail on the client installation page.
Search by Concepts
Once your images are indexed, you can search for them by concepts.
By Clarifai/main App Concepts
When you add an input, it automatically gets predictions from the models in your default, which are typically models from the clarifai/main app, such as the General model. You can search by those predictions.
- gRPC Python
- gRPC NodeJS
- gRPC Java
- cURL
#####################################################################################
# In this section, we set the user authentication, app ID, and the concept name we
# we want to rank by. Change these strings to run your own example.
#####################################################################################
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the portal under Authentification
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change this to rank by a Clarifai/main concept
CONCEPT_NAME = 'ai_fvlBqXZR'
##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + PAT),)
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID) # The userDataObject is required when using a PAT
post_searches_response = stub.PostSearches(
service_pb2.PostSearchesRequest(
user_app_id=userDataObject,
query=resources_pb2.Query(
ands=[
resources_pb2.And(
output=resources_pb2.Output( # Setting Output indicates we search for images that have the concept(s)
# which were predicted by the General model
data=resources_pb2.Data(
concepts=[ # You can search by multiple concepts
resources_pb2.Concept(
name=CONCEPT_NAME,
#id=CONCEPT_ID, # You could search by concept ID as well
value=1 # Value of 0 will search for images that don't have the concept
)
]
)
)
)
]
)
),
metadata=metadata
)
if post_searches_response.status.code != status_code_pb2.SUCCESS:
print(post_searches_response.status)
raise Exception("Post searches failed, status: " + post_searches_response.status.description)
print("Found inputs:")
for hit in post_searches_response.hits:
print("\tScore %.2f for %s" % (hit.score, hit.input.id))
//index.js file
///////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and the concept name we
// we want to rank by. Change these strings to run your own example.
//////////////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the portal under Authentification
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change this to rank by a Clarifai/main concept
const CONCEPT_NAME = 'ai_fvlBqXZR';
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
// This will be used by every Clarifai endpoint call
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key " + PAT);
stub.PostSearches(
{
user_app_id: {
user_id: USER_ID,
app_id: APP_ID
},
query: {
ands: [
{
output: { // Setting Output indicates we search for images that have the concept(s)
// which were predicted by the General model
data: {
concepts: [ // You can search by multiple concepts
{
name: CONCEPT_NAME,
//id: CONCEPT_ID, // You could search by concept ID as well
value: 1 // Value of 0 will search for images that don't have the concept
}
]
}
}
}
]
}
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
throw new Error("Post searches failed, status: " + response.status.description);
}
console.log("Found inputs:");
for (const hit of response.hits) {
console.log("\tScore " + hit.score + " for " + hit.input.id);
}
}
);
package com.clarifai.example;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.StatusCode;
public class ClarifaiExample {
///////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and the concept name we
// we want to rank by. Change these strings to run your own example.
//////////////////////////////////////////////////////////////////////////////////////
static final String USER_ID = "YOUR_USER_ID_HERE";
//Your PAT (Personal Access Token) can be found in the portal under Authentication
static final String PAT = "YOUR_PAT_HERE";
static final String APP_ID = "YOUR_APP_ID_HERE";
// Change this to rank by a Clarifai/main concept
static final String CONCEPT_NAME = "ai_fvlBqXZR";
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
.withCallCredentials(new ClarifaiCallCredentials(PAT));
MultiSearchResponse postSearchesResponse = stub.postSearches(
PostSearchesRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID))
.setQuery(
Query.newBuilder().addAnds(
And.newBuilder().setOutput( // Setting Output indicates we search for images that have the concept(s)
// which were predicted by the General model.
Output.newBuilder().setData(
Data.newBuilder().addConcepts( // You can search by multiple concepts
Concept.newBuilder()
.setName(CONCEPT_NAME) // You could search by concept ID as well
.setValue(1f) // Value of 0 will search for images that don't have the concept
)
)
)
)
)
.build()
);
if (postSearchesResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("Post searches failed, status: " + postSearchesResponse.getStatus());
}
System.out.println("Found inputs " + postSearchesResponse.getHitsCount() + ":");
for (Hit hit: postSearchesResponse.getHitsList()) {
System.out.printf("\tScore %.2f for %s\n", hit.getScore(), hit.getInput().getId());
}
}
}
# Setting "output" indicates we search for images that have the concept(s) which were predicted by
# the General model.
# Value of 0 will search for images that don't have the concept.
# Instead of "name", you can search by "id" as well.
curl -X POST "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/YOUR_APP_ID_HERE/searches" \
-H "Authorization: Key YOUR_PAT_HERE" \
-H "Content-Type: application/json" \
-d '{
"query": {
"ands": [
{
"output": {
"data": {
"concepts": [
{
"name":"ai_fvlBqXZR",
"value": 1
}
]
}
}
}
]
}
}'
By Custom Concepts
After you have added inputs with concepts, you can search by those concepts.
- gRPC Python
- gRPC NodeJS
- gRPC Java
- cURL
########################################################################################
# In this section, we set the user authentication, app ID, and the custom concept we
# want to rank by. Change these strings to run your own example.
########################################################################################
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the portal under Authentification
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change this to rank by your own custom concept
CONCEPT_NAME = 'people'
##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + PAT),)
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID) # The userDataObject is required when using a PAT
post_searches_response = stub.PostSearches(
service_pb2.PostSearchesRequest(
user_app_id=userDataObject,
query=resources_pb2.Query(
ands=[
resources_pb2.And(
input=resources_pb2.Input( # Setting Input indicates we search for images that have the concept(s)
# which we added to the input manually
data=resources_pb2.Data(
concepts=[ # You can search by multiple concepts
resources_pb2.Concept(
name=CONCEPT_NAME,
#id=CONCEPT_ID, # You could search by concept ID as well
value=1 # Value of 0 will search for images that we marked not to have the concept
)
]
)
)
)
]
)
),
metadata=metadata
)
if post_searches_response.status.code != status_code_pb2.SUCCESS:
print(post_searches_response.status)
raise Exception("Post searches failed, status: " + post_searches_response.status.description)
print("Found inputs:")
for hit in post_searches_response.hits:
print("\tScore %.2f for %s" % (hit.score, hit.input.id))
//index.js file
////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and the custom concept we
// want to rank by. Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the portal under Authentification
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change this to rank by your own custom concept
const CONCEPT_NAME = 'people';
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
// This will be used by every Clarifai endpoint call
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key " + PAT);
stub.PostSearches(
{
user_app_id: {
user_id: USER_ID,
app_id: APP_ID
},
query: {
ands: [
{
input: { // Setting Input indicates we search for images that have the concept(s)
// which we added to the input manually
data: {
concepts: [ // You can search by multiple concepts
{
name: CONCEPT_NAME,
//id: CONCEPT_ID, // You could search by concept ID as well
value: 1 // Value of 0 will search for images that we marked not to have the concept
}
]
}
}
}
]
}
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
throw new Error("Post searches failed, status: " + response.status.description);
}
console.log("Found inputs:");
for (const hit of response.hits) {
console.log("\tScore " + hit.score + " for " + hit.input.id);
}
}
);
package com.clarifai.example;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.StatusCode;
public class ClarifaiExample {
////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and the custom concept we
// want to rank by. Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////////
static final String USER_ID = "YOUR_USER_ID_HERE";
//Your PAT (Personal Access Token) can be found in the portal under Authentication
static final String PAT = "YOUR_PAT_HERE";
static final String APP_ID = "YOUR_APP_ID_HERE";
// Change this to rank by your own custom concept
static final String CONCEPT_NAME = "people";
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
.withCallCredentials(new ClarifaiCallCredentials(PAT));
MultiSearchResponse postSearchesResponse = stub.postSearches(
PostSearchesRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID))
.setQuery(
Query.newBuilder().addAnds(
And.newBuilder().setInput( // Setting Input indicates we search for images that have the concept(s)
// which we added to the input manually.
Input.newBuilder().setData(
Data.newBuilder().addConcepts( // You can search by multiple concepts
Concept.newBuilder()
.setName(CONCEPT_NAME) // You could search by concept ID as well
.setValue(1f) // Value of 0 will search for images that we marked not to have the concept
)
)
)
)
)
.build()
);
if (postSearchesResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("Post searches failed, status: " + postSearchesResponse.getStatus());
}
System.out.println("Found inputs " + postSearchesResponse.getHitsCount() + ":");
for (Hit hit: postSearchesResponse.getHitsList()) {
System.out.printf("\tScore %.2f for %s\n", hit.getScore(), hit.getInput().getId());
}
}
}
# Setting "input" indicates we search for images that have the concept(s) which we added to the
# input manually.
# Value of 0 will search for images that don't have the concept.#
# Instead of "name" you can search by "id" as well.
curl -X POST "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/YOUR_APP_ID_HERE/searches" \
-H "Authorization: Key YOUR_PAT_HERE" \
-H "Content-Type: application/json" \
-d '{
"query": {
"ands": [
{
"input": {
"data": {
"concepts": [
{
"name":"people",
"value": 1
}
]
}
}
}
]
}
}'
By Clarifai/main and Custom Concepts
You can combine a search to find inputs that have concepts you have supplied as well as predictions from your model.
- gRPC Python
- gRPC NodeJS
- gRPC Java
- cURL
################################################################################
# In this section, we set the user authentication, app ID, and the concepts we
# we want to rank by. Change these strings to run your own example.
################################################################################
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the portal under Authentification
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change this to a Clarifai/main concept
CONCEPT_NAME_1 = 'ai_fvlBqXZR'
# Change this to your own custom concept
CONCEPT_NAME_2 = 'people'
##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + PAT),)
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID) # The userDataObject is required when using a PAT
# Here we search for images labeled with 'ai_fvlBqXZR' and for which the General prediction model does not find
# a 'people' concept
post_searches_response = stub.PostSearches(
service_pb2.PostSearchesRequest(
user_app_id=userDataObject,
query=resources_pb2.Query(
ands=[
resources_pb2.And(
input=resources_pb2.Input( # Setting Input indicates we search for images that have the concept(s)
# which we added to the input manually
data=resources_pb2.Data(
concepts=[
resources_pb2.Concept(
name=CONCEPT_NAME_1,
value=1
)
]
)
)
),
resources_pb2.And(
output=resources_pb2.Output( # Setting Output indicates we search for images that have the concept(s)
# which were predicted by the General model
data=resources_pb2.Data(
concepts=[
resources_pb2.Concept(
name=CONCEPT_NAME_2,
value=0
)
]
)
)
)
]
)
),
metadata=metadata
)
if post_searches_response.status.code != status_code_pb2.SUCCESS:
print(post_searches_response.status)
raise Exception("Post searches failed, status: " + post_searches_response.status.description)
print("Found inputs:")
for hit in post_searches_response.hits:
print("\tScore %.2f for %s" % (hit.score, hit.input.id))
//index.js file
////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and the concepts we
// we want to rank by. Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the portal under Authentification
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change this to a Clarifai/main concept
const CONCEPT_NAME_1 = 'ai_fvlBqXZR';
// Change this to your own custom concept
const CONCEPT_NAME_2 = 'people';
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
// This will be used by every Clarifai endpoint call
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key " + PAT);
// Here we search for images which we labeled with "ai_fvlBqXZR" and for which the General prediction model does not find
// a "people" concept
stub.PostSearches(
{
user_app_id: {
user_id: USER_ID,
app_id: APP_ID
},
query: {
ands: [
{
input: { // Setting Input indicates we search for images that have the concept(s)
// which we added to the input manually
data: {
concepts: [
{
name: CONCEPT_NAME_1,
value: 1
}
]
}
}
},
{
output: { // Setting Output indicates we search for images that have the concept(s)
// which were predicted by the General model
data: {
concepts: [
{
name: CONCEPT_NAME_2,
value: 0
}
]
}
}
}
]
}
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
throw new Error("Post searches failed, status: " + response.status.description);
}
console.log("Found inputs:");
for (const hit of response.hits) {
console.log("\tScore " + hit.score + " for " + hit.input.id);
}
}
);
package com.clarifai.example;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.StatusCode;
public class ClarifaiExample {
////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and the concepts we
// we want to rank by. Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////////
static final String USER_ID = "YOUR_USER_ID_HERE";
//Your PAT (Personal Access Token) can be found in the portal under Authentication
static final String PAT = "YOUR_PAT_HERE";
static final String APP_ID = "YOUR_APP_ID_HERE";
// Change this to a Clarifai/main concept
static final String CONCEPT_NAME_1 = "ai_fvlBqXZR";
// Change this to your own custom concept
static final String CONCEPT_NAME_2 = "people";
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
.withCallCredentials(new ClarifaiCallCredentials(PAT));
// Here we search for images which we labeled with "ai_fvlBqXZR" and for which the General prediction model does not find
// a "people" concept.
MultiSearchResponse postSearchesResponse = stub.postSearches(
PostSearchesRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID))
.setQuery(
Query.newBuilder()
.addAnds(
And.newBuilder().setInput( // Setting Input indicates we search for images that have the concept(s)
// which we added to the input manually.
Input.newBuilder().setData(
Data.newBuilder().addConcepts(
Concept.newBuilder()
.setName(CONCEPT_NAME_1)
.setValue(1f)
)
)
)
)
.addAnds(
And.newBuilder().setOutput( // Setting Output indicates we search for images that have the concept(s)
// which were predicted by the General model.
Output.newBuilder().setData(
Data.newBuilder().addConcepts(
Concept.newBuilder()
.setName(CONCEPT_NAME_2)
.setValue(0f) // Because of 0, the concept must not be present in the image
)
)
)
)
)
.build()
);
if (postSearchesResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("Post searches failed, status: " + postSearchesResponse.getStatus());
}
System.out.println("Found inputs " + postSearchesResponse.getHitsCount() + ":");
for (Hit hit: postSearchesResponse.getHitsList()) {
System.out.printf("\tScore %.2f for %s\n", hit.getScore(), hit.getInput().getId());
}
}
}
# Here we search for images which we labeled with "ai_fvlBqXZR" and for which the General prediction model
# does not find a "people" concept.
curl -X POST "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/YOUR_APP_ID_HERE/searches" \
-H "Authorization: Key YOUR_PAT_HERE" \
-H "Content-Type: application/json" \
-d '{
"query": {
"ands": [
{
"input": {
"data": {
"concepts": [
{
"name": "ai_fvlBqXZR",
"value": 1
}
]
}
}
},
{
"output": {
"data": {
"concepts": [
{
"name": "people",
"value": 0
}
]
}
}
}
]
}
}'
By Concept in Another Language
Concepts that have a translation into another language can be searched for in that language, even without having the default language for your app being in that language. This uses Clarifai's knowledge graph to lookup the translation and then perform the search.
For example, if your app is in English and you want to search for "dog" in Japanese, then you could search with language="ja"
and name="犬"
.
- gRPC Python
- gRPC NodeJS
- gRPC Java
- cURL
################################################################################
# In this section, we set the user authentication, app ID, concept name, and
# language ID. Change these strings to run your own example.
################################################################################
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the portal under Authentification
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to what you want to search by
CONCEPT_NAME = '犬'
LANGUAGE_ID = 'ja' # Japanese
##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + PAT),)
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID) # The userDataObject is required when using a PAT
post_searches_response = stub.PostSearches(
service_pb2.PostSearchesRequest(
user_app_id=userDataObject,
query=resources_pb2.Query(
ands=[
resources_pb2.And(
output=resources_pb2.Output( # Setting Output indicates we search for images that have the concept(s)
# which were predicted by the General model
data=resources_pb2.Data(
concepts=[ # You can search by multiple concepts
resources_pb2.Concept(
name=CONCEPT_NAME, # You could search by concept ID as well
language=LANGUAGE_ID,
value=1 # Value of 0 will search for images that don't have the concept
)
]
)
)
)
]
)
),
metadata=metadata
)
if post_searches_response.status.code != status_code_pb2.SUCCESS:
print(post_searches_response.status)
raise Exception("Post searches failed, status: " + post_searches_response.status.description)
print("Found inputs:")
for hit in post_searches_response.hits:
print("\tScore %.2f for %s" % (hit.score, hit.input.id))
//index.js file
///////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, concept name, and
// language ID. Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the portal under Authentification
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change these to what you want to search by
const CONCEPT_NAME = '犬';
const LANGUAGE_ID = 'ja'; // Japanese
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
// This will be used by every Clarifai endpoint call
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key " + PAT);
stub.PostSearches(
{
user_app_id: {
user_id: USER_ID,
app_id: APP_ID
},
query: {
ands: [
{
output: { // Setting Output indicates we search for images that have the concept(s)
// which were predicted by the General model
data: {
concepts: [ // You can search by multiple concepts
{
name: CONCEPT_NAME, // You could search by concept ID as well
language: LANGUAGE_ID,
value: 1 // Value of 0 will search for images that don't have the concept
}
]
}
}
}
]
}
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
throw new Error("Post searches failed, status: " + response.status.description);
}
console.log("Found inputs:");
for (const hit of response.hits) {
console.log("\tScore " + hit.score + " for " + hit.input.id);
}
}
);
package com.clarifai.example;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.StatusCode;
public class ClarifaiExample {
///////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, concept name, and
// language ID. Change these strings to run your own example.
///////////////////////////////////////////////////////////////////////////////////
static final String USER_ID = "YOUR_USER_ID_HERE";
//Your PAT (Personal Access Token) can be found in the portal under Authentication
static final String PAT = "YOUR_PAT_HERE";
static final String APP_ID = "YOUR_APP_ID_HERE";
// Change these to what you want to search by
static final String CONCEPT_NAME = "犬";
static final String LANGUAGE_ID = "ja"; // Japanese
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
.withCallCredentials(new ClarifaiCallCredentials(PAT));
MultiSearchResponse postSearchesResponse = stub.postSearches(
PostSearchesRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID))
.setQuery(
Query.newBuilder().addAnds(
And.newBuilder().setOutput( // Setting Output indicates we search for images that have the concept(s)
// which were predicted by the General model.
Output.newBuilder().setData(
Data.newBuilder().addConcepts( // You can search by multiple concepts
Concept.newBuilder()
.setName(CONCEPT_NAME) // You could search by concept ID as well
.setLanguage(LANGUAGE_ID)
.setValue(1f) // Value of 0 will search for images that don't have the concept
)
)
)
)
)
.build()
);
if (postSearchesResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("Post searches failed, status: " + postSearchesResponse.getStatus());
}
System.out.println("Found inputs " + postSearchesResponse.getHitsCount() + ":");
for (Hit hit : postSearchesResponse.getHitsList()) {
System.out.printf("\tScore %.2f for %s\n", hit.getScore(), hit.getInput().getId());
}
}
}
curl -X POST "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/YOUR_APP_ID_HERE/searches" \
-H "Authorization: Key YOUR_PAT_HERE" \
-H "Content-Type: application/json" \
-d '{
"query": {
"ands": [
{
"output": {
"data": {
"concepts": [
{
"name":"犬",
"language": "ja",
"value": 1
}
]
}
}
}
]
}
}'
Search by Visual Similarity
You can use images to search through your collection. The API will return ranked results based on how similar the results are to the image you provided in your query.
By Image
- gRPC Python
- gRPC NodeJS
- gRPC Java
- cURL
##################################################################
# In this section, we set the user authentication, app ID, and
# image URL. Change these strings to run your own example.
##################################################################
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the portal under Authentification
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change this to the image URL you want to search by
IMAGE_URL = 'https://samples.clarifai.com/metro-north.jpg'
##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + PAT),)
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID) # The userDataObject is required when using a PAT
post_searches_response = stub.PostSearches(
service_pb2.PostSearchesRequest(
user_app_id=userDataObject,
query=resources_pb2.Query(
ands=[
resources_pb2.And(
output=resources_pb2.Output(
input=resources_pb2.Input(
data=resources_pb2.Data(
image=resources_pb2.Image(
url=IMAGE_URL
)
)
)
)
)
]
)
),
metadata=metadata
)
if post_searches_response.status.code != status_code_pb2.SUCCESS:
print(post_searches_response.status)
raise Exception("Post searches failed, status: " + post_searches_response.status.description)
print("Found inputs:")
for hit in post_searches_response.hits:
print("\tScore %.2f for %s" % (hit.score, hit.input.id))
//index.js file
////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and
// image URL. Change these strings to run your own example.
////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the portal under Authentification
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change this to the image URL you want to search by
const IMAGE_URL = 'https://samples.clarifai.com/metro-north.jpg';
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
// This will be used by every Clarifai endpoint call
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key " + PAT);
stub.PostSearches(
{
user_app_id: {
user_id: USER_ID,
app_id: APP_ID
},
query: {
ands: [
{
output: {
input: {
data: {
image: {
url: IMAGE_URL
}
}
}
}
}
]
}
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
throw new Error("Post searches failed, status: " + response.status.description);
}
console.log("Found inputs:");
for (const hit of response.hits) {
console.log("\tScore " + hit.score + " for " + hit.input.id);
}
}
);
package com.clarifai.example;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.StatusCode;
public class ClarifaiExample {
////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and
// image URL. Change these strings to run your own example.
////////////////////////////////////////////////////////////////////
static final String USER_ID = "YOUR_USER_ID_HERE";
//Your PAT (Personal Access Token) can be found in the portal under Authentication
static final String PAT = "YOUR_PAT_HERE";
static final String APP_ID = "YOUR_APP_ID_HERE";
// Change this to the image URL you want to search by
static final String IMAGE_URL = "https://samples.clarifai.com/metro-north.jpg";
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
.withCallCredentials(new ClarifaiCallCredentials(PAT));
MultiSearchResponse postSearchesResponse = stub.postSearches(
PostSearchesRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID))
.setQuery(
Query.newBuilder().addAnds(
And.newBuilder().setOutput(
Output.newBuilder().setInput(
Input.newBuilder().setData(
Data.newBuilder().setImage(
Image.newBuilder()
.setUrl(IMAGE_URL)
)
)
)
)
)
)
.build()
);
if (postSearchesResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("Post searches failed, status: " + postSearchesResponse.getStatus());
}
System.out.println("Found inputs " + postSearchesResponse.getHitsCount() + ":");
for (Hit hit : postSearchesResponse.getHitsList()) {
System.out.printf("\tScore %.2f for %s\n", hit.getScore(), hit.getInput().getId());
}
}
}
curl -X POST "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/YOUR_APP_ID_HERE/searches" \
-H "Authorization: Key YOUR_PAT_HERE" \
-H "Content-Type: application/json" \
-d '{
"query": {
"ands": [
{
"output":{
"input":{
"data": {
"image": {
"url": "https://samples.clarifai.com/metro-north.jpg"
}
}
}
}
}
]
}
}'
By URL
You can also search for an input by URL.
- gRPC Python
- gRPC NodeJS
- gRPC Java
- cURL
##################################################################
# In this section, we set the user authentication, app ID, and
# image URL. Change these strings to run your own example.
##################################################################
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the portal under Authentification
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change this to the image URL you want to search by
IMAGE_URL = 'https://samples.clarifai.com/metro-north.jpg'
##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + PAT),)
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID) # The userDataObject is required when using a PAT
post_searches_response = stub.PostSearches(
service_pb2.PostSearchesRequest(
user_app_id=userDataObject,
query=resources_pb2.Query(
ands=[
resources_pb2.And(
input=resources_pb2.Input(
data=resources_pb2.Data(
image=resources_pb2.Image(
url=IMAGE_URL
)
)
)
)
]
)
),
metadata=metadata
)
if post_searches_response.status.code != status_code_pb2.SUCCESS:
print(post_searches_response.status)
raise Exception("Post searches failed, status: " + post_searches_response.status.description)
print("Found inputs:")
for hit in post_searches_response.hits:
print("\tScore %.2f for %s" % (hit.score, hit.input.id))
//index.js file
////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and
// image URL. Change these strings to run your own example.
////////////////////////////////////////////////////////////////////
const USER_ID = 'YOUR_USER_ID_HERE';
// Your PAT (Personal Access Token) can be found in the portal under Authentification
const PAT = 'YOUR_PAT_HERE';
const APP_ID = 'YOUR_APP_ID_HERE';
// Change this to the image URL you want to search by
const IMAGE_URL = 'https://samples.clarifai.com/metro-north.jpg';
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const { ClarifaiStub, grpc } = require("clarifai-nodejs-grpc");
const stub = ClarifaiStub.grpc();
// This will be used by every Clarifai endpoint call
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key " + PAT);
stub.PostSearches(
{
user_app_id: {
user_id: USER_ID,
app_id: APP_ID
},
query: {
ands: [
{
input: {
data: {
image: {
url: IMAGE_URL
}
}
}
}
]
}
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}
if (response.status.code !== 10000) {
throw new Error("Post searches failed, status: " + response.status.description);
}
console.log("Found inputs:");
for (const hit of response.hits) {
console.log("\tScore " + hit.score + " for " + hit.input.id);
}
}
);
package com.clarifai.example;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.StatusCode;
public class ClarifaiExample {
////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, and
// image URL. Change these strings to run your own example.
////////////////////////////////////////////////////////////////////
static final String USER_ID = "YOUR_USER_ID_HERE";
//Your PAT (Personal Access Token) can be found in the portal under Authentication
static final String PAT = "YOUR_PAT_HERE";
static final String APP_ID = "YOUR_APP_ID_HERE";
// Change this to the image URL you want to search by
static final String IMAGE_URL = "https://samples.clarifai.com/metro-north.jpg";
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(ClarifaiChannel.INSTANCE.getGrpcChannel())
.withCallCredentials(new ClarifaiCallCredentials(PAT));
MultiSearchResponse postSearchesResponse = stub.postSearches(
PostSearchesRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID))
.setQuery(
Query.newBuilder().addAnds(
And.newBuilder().setInput(
Input.newBuilder().setData(
Data.newBuilder().setImage(
Image.newBuilder()
.setUrl(IMAGE_URL)
)
)
)
)
)
.build()
);
if (postSearchesResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("Post searches failed, status: " + postSearchesResponse.getStatus());
}
System.out.println("Found inputs " + postSearchesResponse.getHitsCount() + ":");
for (Hit hit : postSearchesResponse.getHitsList()) {
System.out.printf("\tScore %.2f for %s\n", hit.getScore(), hit.getInput().getId());
}
}
}
curl -X POST "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/YOUR_APP_ID_HERE/searches" \
-H "Authorization: Key YOUR_PAT_HERE" \
-H "Content-Type: application/json" \
-d '{
"query": {
"ands": [
{
"input":{
"data": {
"image": {
"url": "https://samples.clarifai.com/metro-north.jpg"
}
}
}
}
]
}
}'