Skip to main content

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.

info

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.

#####################################################################################
# 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 Account's Security section
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))

By Custom Concepts

After you have added inputs with concepts, you can search by those concepts.

########################################################################################
# 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 Account's Security section
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))

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.

################################################################################
# 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 Account's Security section
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))

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="犬".

################################################################################
# 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 Account's Security section
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))

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

##################################################################
# 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 Account's Security section
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))

By URL

You can also search for an input by URL.

##################################################################
# 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 Account's Security section
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.Annotation(
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))