Multilingual Classification
Multilingual predictions.
The Clarifai API supports many languages in addition to English. When making a predict api request, you can pass in the language you would like the concepts returned in. When you create a new Application, you must specify a default language which will be the language of the returned concepts if not specified in the predict request.

Example Predict API Request

You can predict concepts in a language other than the Application's default, by explicitly passing in the language. Here is how you predict concepts in Chinese:
Python
PHP
Java
NodeJS
Python
cURL
Javascript (REST)
1
# Insert here the initialization code as outlined on this page:
2
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
post_model_outputs_response = stub.PostModelOutputs(
5
service_pb2.PostModelOutputsRequest(
6
model_id="aaa03c23b3724a16a56b629203edc62c", # This is model ID of the publicly available General model.
7
inputs=[
8
resources_pb2.Input(
9
data=resources_pb2.Data(
10
image=resources_pb2.Image(
11
url="https://samples.clarifai.com/metro-north.jpg"
12
)
13
)
14
)
15
],
16
model=resources_pb2.Model(
17
output_info=resources_pb2.OutputInfo(
18
output_config=resources_pb2.OutputConfig(
19
language="zh" # Chinese
20
)
21
)
22
)
23
),
24
metadata=metadata
25
)
26
27
if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
28
raise Exception("Post model outputs failed, status: " + post_model_outputs_response.status.description)
29
30
# Since we have one input, one output will exist here.
31
output = post_model_outputs_response.outputs[0]
32
33
print("Predicted concepts:")
34
for concept in output.data.concepts:
35
print("\t%s %.2f" % (concept.name, concept.value))
Copied!
1
<?php
2
# Insert here the initialization code as outlined on this page:
3
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
4
5
///////////////////////////////////////////////////////////////////////////////
6
// Specifying the Request Data
7
///////////////////////////////////////////////////////////////////////////////
8
//
9
// In the Clarifai platform an image is defined by a special Image object.
10
// There are several ways in which an Image object can be populated including
11
// by url and image bytes (base64).
12
//
13
$image = new Image([
14
'url' => 'https://samples.clarifai.com/dog2.jpeg'
15
]);
16
17
//
18
// After an Image object is created, a Data object is constructed around it.
19
// The Data object offers a container that contains additional image independent
20
// metadata. In this particular use case, no other metadata is needed to be
21
// specified.
22
//
23
$data = new Data([
24
'image' => $image
25
]);
26
27
//
28
// The Data object is then wrapped in an Input object in order to meet the
29
// API specification. Additional fields are available to populate in the Input
30
// object, but for the purposes of this example we can send in just the
31
// Data object.
32
//
33
$input = new Input([
34
'data' => $data
35
]);
36
37
///////////////////////////////////////////////////////////////////////////////
38
// Specifying Output Configuration
39
///////////////////////////////////////////////////////////////////////////////
40
//
41
// Output configuration can be specified by the OutputConfig object.
42
//
43
$outputConfig = new OutputConfig([
44
'language' => 'zh' // Chinese
45
])
46
47
//
48
// The OutputInfo object is a wrapper around the OutputConfig object
49
//
50
$outputInfo = new OutputInfo([
51
'output_config' => $outputConfig
52
])
53
54
//
55
// The model object is a wrapper around the OutputInfo object. This is the
56
// final part needed to define an output configuration.
57
//
58
$model = new Model([
59
'output_info' => $outputInfo
60
]);
61
62
///////////////////////////////////////////////////////////////////////////////
63
// Creating the request object
64
///////////////////////////////////////////////////////////////////////////////
65
//
66
// Finally, the request object itself is created. This object carries the request
67
// along with the request status and other metadata related to the request itself.
68
// In this example we populate:
69
// - the `user_app_id` field with the UserAppIDSet constructed above
70
// - the `model_id` field with the ID of the model we are referencing
71
// - the `inputs` field with an array of input objects constructed above
72
// - the `model` field with the output configuration specified above
73
//
74
$request = new PostModelOutputsRequest([
75
'user_app_id' => $userDataObject, // This is defined above
76
'model_id' => 'aaa03c23b3724a16a56b629203edc62c', // This is the ID of the publicly available General model.
77
'inputs' => [$input],
78
'model' => $model
79
]);
80
81
///////////////////////////////////////////////////////////////////////////////
82
// Making the RPC Call
83
///////////////////////////////////////////////////////////////////////////////
84
//
85
// Once the request object is constructed, we can call the actual request to the
86
// Clarifai platform. This uses the opened gRPC client channel to communicate the
87
// request and then wait for the response.
88
//
89
[$response, $status] = $client->PostModelOutputs(
90
$request,
91
$metadata
92
)->wait();
93
94
///////////////////////////////////////////////////////////////////////////////
95
// Handling the Response
96
///////////////////////////////////////////////////////////////////////////////
97
//
98
// The response is returned and the first thing we do is check the status of it.
99
// A successful response will have a status code of 0, otherwise there is some
100
// reported error.
101
//
102
if ($status->code !== 0) throw new Exception("Error: {$status->details}");
103
104
//
105
// In addition to the RPC response status, there is a Clarifai API status that
106
// reports if the operationo was a success or failure (not just that the commuunication)
107
// was successful.
108
//
109
if ($response->getStatus()->getCode() != StatusCode::SUCCESS) {
110
throw new Exception("Failure response: " . $response->getStatus()->getDescription() . " " .
111
$response->getStatus()->getDetails());
112
}
113
114
//
115
// The output of a successful call can be used in many ways. In this example,
116
// we loop through all of the predicted concepts and print them out along with
117
// their numerical prediction value (confidence).
118
//
119
echo "Predicted concepts:\n";
120
foreach ($response->getOutputs()[0]->getData()->getConcepts() as $concept) {
121
echo $concept->getName() . ": " . number_format($concept->getValue(), 2) . "\n";
122
}
123
?>
Copied!
1
import com.clarifai.grpc.api.*;
2
import com.clarifai.grpc.api.status.*;
3
4
// Insert here the initialization code as outlined on this page:
5
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
6
7
MultiOutputResponse postModelOutputsResponse = stub.postModelOutputs(
8
PostModelOutputsRequest.newBuilder()
9
.setModelId("aaa03c23b3724a16a56b629203edc62c") // This is model ID of the publicly available General model.
10
.addInputs(
11
Input.newBuilder().setData(
12
Data.newBuilder().setImage(
13
Image.newBuilder().setUrl("https://samples.clarifai.com/metro-north.jpg")
14
)
15
)
16
)
17
.setModel(
18
Model.newBuilder().setOutputInfo(
19
OutputInfo.newBuilder().setOutputConfig(
20
OutputConfig.newBuilder().setLanguage("zh") // Chinese
21
)
22
)
23
)
24
.build()
25
);
26
27
if (postModelOutputsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
28
throw new RuntimeException("Post model outputs failed, status: " + postModelOutputsResponse.getStatus());
29
}
30
31
// Since we have one input, one output will exist here.
32
Output output = postModelOutputsResponse.getOutputs(0);
33
34
System.out.println("Predicted concepts:");
35
for (Concept concept : output.getData().getConceptsList()) {
36
System.out.printf("%s %.2f%n", concept.getName(), concept.getValue());
37
}
Copied!
1
// Insert here the initialization code as outlined on this page:
2
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
stub.PostModelOutputs(
5
{
6
model_id: "aaa03c23b3724a16a56b629203edc62c",
7
inputs: [
8
{data: {image: {url: "https://samples.clarifai.com/metro-north.jpg"}}}
9
],
10
model: {output_info: {output_config: {language: "zh"}}}
11
},
12
metadata,
13
(err, response) => {
14
if (err) {
15
throw new Error(err);
16
}
17
18
if (response.status.code !== 10000) {
19
throw new Error("Post model outputs failed, status: " + response.status.description);
20
}
21
22
// Since we have one input, one output will exist here.
23
const output = response.outputs[0];
24
25
console.log("Predicted concepts:");
26
for (const concept of output.data.concepts) {
27
console.log(concept.name + " " + concept.value);
28
}
29
}
30
);
Copied!
1
# Insert here the initialization code as outlined on this page:
2
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
post_model_outputs_response = stub.PostModelOutputs(
5
service_pb2.PostModelOutputsRequest(
6
user_app_id=userDataObject, # The userDataObject is created in the overview and is required when using a PAT
7
model_id="aaa03c23b3724a16a56b629203edc62c", # This is model ID of the publicly available General model.
8
inputs=[
9
resources_pb2.Input(
10
data=resources_pb2.Data(
11
image=resources_pb2.Image(
12
url="https://samples.clarifai.com/metro-north.jpg"
13
)
14
)
15
)
16
],
17
model=resources_pb2.Model(
18
output_info=resources_pb2.OutputInfo(
19
output_config=resources_pb2.OutputConfig(
20
language="zh" # Chinese
21
)
22
)
23
)
24
),
25
metadata=metadata
26
)
27
28
if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
29
print("There was an error with your request!")
30
print("\tCode: {}".format(post_model_outputs_response.outputs[0].status.code))
31
print("\tDescription: {}".format(post_model_outputs_response.outputs[0].status.description))
32
print("\tDetails: {}".format(post_model_outputs_response.outputs[0].status.details))
33
raise Exception("Post model outputs failed, status: " + post_model_outputs_response.status.description)
34
35
# Since we have one input, one output will exist here.
36
output = post_model_outputs_response.outputs[0]
37
38
print("Predicted concepts:")
39
for concept in output.data.concepts:
40
print("\t%s %.2f" % (concept.name, concept.value))
Copied!
1
curl -X POST \
2
-H "Authorization: Key YOUR_API_KEY" \
3
-H "Content-Type: application/json" \
4
-d '
5
{
6
"inputs": [
7
{
8
"data": {
9
"image": {
10
"url": "https://samples.clarifai.com/metro-north.jpg"
11
}
12
}
13
}
14
],
15
"model":{
16
"output_info":{
17
"output_config":{
18
"language":"zh"
19
}
20
}
21
}
22
}'\
23
https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs
24
25
# Above is model ID of the publicly available General model.
Copied!
1
const raw = JSON.stringify({
2
"user_app_id": {
3
"user_id": "{YOUR_USER_ID}",
4
"app_id": "{YOUR_APP_ID}"
5
},
6
"inputs": [
7
{
8
"data": {
9
"image": {
10
"url": "https://samples.clarifai.com/metro-north.jpg"
11
}
12
}
13
}
14
],
15
"model":{
16
"output_info":{
17
"output_config":{
18
"language":"zh"
19
}
20
}
21
}
22
});
23
24
const requestOptions = {
25
method: 'POST',
26
headers: {
27
'Accept': 'application/json',
28
'Authorization': 'Key {YOUR_PERSONAL_TOKEN}'
29
},
30
body: raw
31
};
32
33
// NOTE: MODEL_VERSION_ID is optional, you can also call prediction with the MODEL_ID only
34
// https://api.clarifai.com/v2/models/{YOUR_MODEL_ID}/outputs
35
// this will default to the latest version_id
36
37
fetch("https://api.clarifai.com/v2/models/{YOUR_MODEL_ID}/versions/{MODEL_VERSION_ID}/outputs", requestOptions)
38
.then(response => response.text())
39
.then(result => console.log(result))
40
.catch(error => console.log('error', error));
Copied!
Response JSON
1
{
2
"status": {
3
"code": 10000,
4
"description": "Ok"
5
},
6
"outputs": [
7
{
8
"id": "b9f3c12f1534440fa984dc463e491780",
9
"status": {
10
"code": 10000,
11
"description": "Ok"
12
},
13
"created_at": "2017-01-31T20:59:27Z",
14
"model": {
15
"name": "general-v1.3",
16
"id": "aaa03c23b3724a16a56b629203edc62c",
17
"created_at": "2016-03-09T17:11:39Z",
18
"app_id": null,
19
"output_info": {
20
"message": "Show output_info with: GET /models/{model_id}/output_info",
21
"type": "concept"
22
},
23
"model_version": {
24
"id": "aa9ca48295b37401f8af92ad1af0d91d",
25
"created_at": "2016-07-13T01:19:12Z",
26
"status": {
27
"code": 21100,
28
"description": "Model trained successfully"
29
}
30
}
31
},
32
"input": {
33
"id": "b9f3c12f1534440fa984dc463e491780",
34
"data": {
35
"image": {
36
"url": "https://samples.clarifai.com/metro-north.jpg"
37
}
38
}
39
},
40
"data": {
41
"concepts": [
42
{
43
"id": "ai_HLmqFqBf",
44
"name": "铁路列车",
45
"app_id": null,
46
"value": 0.9989112
47
},
48
{
49
"id": "ai_fvlBqXZR",
50
"name": "铁路",
51
"app_id": null,
52
"value": 0.9975532
53
},
54
{
55
"id": "ai_Xxjc3MhT",
56
"name": "运输系统",
57
"app_id": null,
58
"value": 0.9959158
59
},
60
{
61
"id": "ai_6kTjGfF6",
62
"name": "站",
63
"app_id": null,
64
"value": 0.992573
65
},
66
{
67
"id": "ai_RRXLczch",
68
"name": "火车",
69
"app_id": null,
70
"value": 0.992556
71
},
72
{
73
"id": "ai_VRmbGVWh",
74
"name": "旅游",
75
"app_id": null,
76
"value": 0.98789215
77
},
78
{
79
"id": "ai_SHNDcmJ3",
80
"name": "地铁",
81
"app_id": null,
82
"value": 0.9816359
83
},
84
{
85
"id": "ai_jlb9q33b",
86
"name": "通勤",
87
"app_id": null,
88
"value": 0.9712483
89
},
90
{
91
"id": "ai_46lGZ4Gm",
92
"name": "铁路",
93
"app_id": null,
94
"value": 0.9690325
95
},
96
{
97
"id": "ai_tr0MBp64",
98
"name": "交通",
99
"app_id": null,
100
"value": 0.9687052
101
},
102
{
103
"id": "ai_l4WckcJN",
104
"name": "模煳",
105
"app_id": null,
106
"value": 0.9667078
107
},
108
{
109
"id": "ai_2gkfMDsM",
110
"name": "平台",
111
"app_id": null,
112
"value": 0.9624243
113
},
114
{
115
"id": "ai_CpFBRWzD",
116
"name": "城市的",
117
"app_id": null,
118
"value": 0.960752
119
},
120
{
121
"id": "ai_786Zr311",
122
"name": "沒有人",
123
"app_id": null,
124
"value": 0.95864904
125
},
126
{
127
"id": "ai_6lhccv44",
128
"name": "商业",
129
"app_id": null,
130
"value": 0.95720303
131
},
132
{
133
"id": "ai_971KsJkn",
134
"name": "跑道",
135
"app_id": null,
136
"value": 0.9494642
137
},
138
{
139
"id": "ai_WBQfVV0p",
140
"name": "城市",
141
"app_id": null,
142
"value": 0.94089437
143
},
144
{
145
"id": "ai_dSCKh8xv",
146
"name": "快速的",
147
"app_id": null,
148
"value": 0.9399334
149
},
150
{
151
"id": "ai_TZ3C79C6",
152
"name": "马路",
153
"app_id": null,
154
"value": 0.93121606
155
},
156
{
157
"id": "ai_VSVscs9k",
158
"name": "终点站",
159
"app_id": null,
160
"value": 0.9230834
161
}
162
]
163
}
164
}
165
]
166
}
Copied!

Example Search By Tag API Request

You can search for concepts in other languages even if the default language of your application is English. When you add inputs to your application, concepts are predicted for every language. Here is an example of searching for '人' which is simplified Chinese for 'people'.
Python
PHP
Java
NodeJS
Python
cURL
Javascript (REST)
1
# Insert here the initialization code as outlined on this page:
2
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
post_concepts_searches_response = stub.PostConceptsSearches(
5
service_pb2.PostConceptsSearchesRequest(
6
concept_query=resources_pb2.ConceptQuery(
7
name="人",
8
language="zh"
9
)
10
),
11
metadata=metadata
12
)
13
14
if post_concepts_searches_response.status.code != status_code_pb2.SUCCESS:
15
raise Exception("Post concepts searches failed, status: " + post_concepts_searches_response.status.description)
16
17
print("Found concepts:")
18
for concept in post_concepts_searches_response.concepts:
19
print("\t%s %.2f" % (concept.name, concept.value))
Copied!
1
<?php
2
# Insert here the initialization code as outlined on this page:
3
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
4
5
6
///////////////////////////////////////////////////////////////////////////////
7
// Specifying Concept Search Parameters
8
///////////////////////////////////////////////////////////////////////////////
9
//
10
// The ConceptQuery object contains the concept restrictions for the search.
11
//
12
$conceptQuery = new ConceptQuery([
13
'name' => "人",
14
'language' => "zh"
15
])
16
17
///////////////////////////////////////////////////////////////////////////////
18
// Creating the request object
19
///////////////////////////////////////////////////////////////////////////////
20
//
21
// Finally, the request object itself is created. This object carries the request
22
// along with the request status and other metadata related to the request itself.
23
// In this example we populate:
24
// - the `user_app_id` field with the UserAppIDSet constructed above
25
// - the `concept_query` field contains the search restrictions above
26
//
27
$request = new PostConceptsSearches([
28
'user_app_id' => $userDataObject, // This is defined above
29
'concept_query' => $conceptQuery
30
]);
31
32
///////////////////////////////////////////////////////////////////////////////
33
// Making the RPC Call
34
///////////////////////////////////////////////////////////////////////////////
35
//
36
// Once the request object is constructed, we can call the actual request to the
37
// Clarifai platform. This uses the opened gRPC client channel to communicate the
38
// request and then wait for the response.
39
//
40
[$response, $status] = $client->PostConceptsSearches(
41
$request,
42
$metadata
43
)->wait();
44
45
///////////////////////////////////////////////////////////////////////////////
46
// Handling the Response
47
///////////////////////////////////////////////////////////////////////////////
48
//
49
// The response is returned and the first thing we do is check the status of it.
50
// A successful response will have a status code of 0, otherwise there is some
51
// reported error.
52
//
53
if ($status->code !== 0) throw new Exception("Error: {$status->details}");
54
55
//
56
// In addition to the RPC response status, there is a Clarifai API status that
57
// reports if the operationo was a success or failure (not just that the commuunication)
58
// was successful.
59
//
60
if ($response->getStatus()->getCode() != StatusCode::SUCCESS) {
61
throw new Exception("Failure response: " . $response->getStatus()->getDescription() . " " .
62
$response->getStatus()->getDetails());
63
}
64
65
//
66
// The output of a successful call can be used in many ways. In this example,
67
// we loop through all of the predicted concepts and print them out along with
68
// their numerical prediction value (confidence).
69
//
70
echo "Predicted concepts:\n";
71
foreach ($response->getConcepts() as $concept) {
72
echo $concept->getName() . ": " . number_format($concept->getValue(), 2) . "\n";
73
}
74
?>
Copied!
1
import com.clarifai.grpc.api.*;
2
import com.clarifai.grpc.api.status.*;
3
4
// Insert here the initialization code as outlined on this page:
5
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
6
7
MultiConceptResponse postConceptsSearchesResponse = stub.postConceptsSearches(
8
PostConceptsSearchesRequest.newBuilder()
9
.setConceptQuery(
10
ConceptQuery.newBuilder()
11
.setName("人")
12
.setLanguage("zh")
13
)
14
.build()
15
);
16
17
if (postConceptsSearchesResponse.getStatus().getCode() != StatusCode.SUCCESS) {
18
throw new RuntimeException("Post concepts searches failed, status: " + postConceptsSearchesResponse.getStatus());
19
}
20
21
System.out.println("Found concepts:");
22
for (Concept concept : postConceptsSearchesResponse.getConceptsList()) {
23
System.out.printf("\t%s %.2f%n", concept.getName(), concept.getValue());
24
}
Copied!
1
// Insert here the initialization code as outlined on this page:
2
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
stub.PostConceptsSearches(
5
{
6
concept_query: {name: "人", language: "zh"}
7
},
8
metadata,
9
(err, response) => {
10
if (err) {
11
throw new Error(err);
12
}
13
14
if (response.status.code !== 10000) {
15
throw new Error("Post concepts searches failed, status: " + response.status.description);
16
}
17
18
console.log("Found concepts:");
19
for (const concept of response.concepts) {
20
console.log("\t" + concept.name + " " + concept.value);
21
}
22
}
23
);
Copied!
1
# Insert here the initialization code as outlined on this page:
2
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
post_concepts_searches_response = stub.PostConceptsSearches(
5
service_pb2.PostConceptsSearchesRequest(
6
user_app_id=userDataObject, # The userDataObject is created in the overview and is required when using a PAT
7
concept_query=resources_pb2.ConceptQuery(
8
name="人",
9
language="zh"
10
)
11
),
12
metadata=metadata
13
)
14
15
if post_concepts_searches_response.status.code != status_code_pb2.SUCCESS:
16
print("There was an error with your request!")
17
print("\tCode: {}".format(post_concepts_searches_response.outputs[0].status.code))
18
print("\tDescription: {}".format(post_concepts_searches_response.outputs[0].status.description))
19
print("\tDetails: {}".format(post_concepts_searches_response.outputs[0].status.details))
20
raise Exception("Post concepts searches failed, status: " + post_concepts_searches_response.status.description)
21
22
print("Found concepts:")
23
for concept in post_concepts_searches_response.concepts:
24
print("\t%s %.2f" % (concept.name, concept.value))
Copied!
1
curl -X POST \
2
-H "Authorization: Key YOUR_API_KEY" \
3
-H "Content-Type: application/json" \
4
-d '
5
{
6
"query": {
7
"ands": [
8
{
9
"output": {
10
"data": {
11
"concepts": [
12
{
13
"name": "人"
14
}
15
]
16
}
17
}
18
}
19
],
20
"language": "zh"
21
}
22
}'\
23
https://api.clarifai.com/v2/searches
Copied!
1
const raw = JSON.stringify({
2
"user_app_id": {
3
"user_id": "{YOUR_USER_ID}",
4
"app_id": "{YOUR_APP_ID}"
5
},
6
"query": {
7
"ands": [
8
{
9
"output": {
10
"data": {
11
"concepts": [
12
{
13
"name": "人"
14
}
15
]
16
}
17
}
18
}
19
],
20
"language": "zh"
21
}
22
});
23
24
const requestOptions = {
25
method: 'POST',
26
headers: {
27
'Accept': 'application/json',
28
'Authorization': 'Key {YOUR_PERSONAL_TOKEN}'
29
},
30
body: raw
31
};
32
33
fetch("https://api.clarifai.com/v2/searches", requestOptions)
34
.then(response => response.text())
35
.then(result => console.log(result))
36
.catch(error => console.log('error', error));
Copied!
Last modified 1mo ago