Combine or Negate
Group or separate items in your dataset
You can also combine searches using AND.
info
The initialization code used in the following example is outlined in detail on the client installation page.
- gRPC Python
- gRPC NodeJS
- gRPC Java
- cURL
################################################################################
# In this section, we set the user authentication, app ID, and the concepts we
# we want to search 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 these to search by your own concepts
CONCEPT_NAME_1 = 'cat'
CONCEPT_NAME_2 = 'dog'
##########################################################################
# 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 which we labeled with "cat" and for which the General prediction model does not find
# a "dog" 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 search 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 these to search by your own concepts
const CONCEPT_NAME_1 = 'cat';
const CONCEPT_NAME_2 = 'dog';
///////////////////////////////////////////////////////////////////////////////////
// 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 "cat" and for which the General prediction model does not find
// a "dog" 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 search 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 these to search by your own concepts
static final String CONCEPT_NAME_1 = "cat";
static final String CONCEPT_NAME_2 = "dog";
///////////////////////////////////////////////////////////////////////////////////
// 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 "cat" and for which the General prediction model does not find
// a "dog" 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)
)
)
)
)
)
.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 "cat" and for which the General prediction model does not find
# a "dog" 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": "cat",
"value": 1
}
]
}
}
},
{
"output": {
"data": {
"concepts": [
{
"name": "dog",
"value": 0
}
]
}
}
}
]
}
}'