Skip to content

Commit

Permalink
Merge pull request #308 from Clarifai/Alfrick-branch
Browse files Browse the repository at this point in the history
DOC-464 <Import model snippets>
  • Loading branch information
Alfrick authored Jan 25, 2024
2 parents f68a2c8 + e7ee42a commit 072ab00
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 29 deletions.
53 changes: 53 additions & 0 deletions code_snippets/api-guide/model/import-model/import_6.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!--index.html file-->

<script>
//////////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, model ID, and model ZIP 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 these to import your own model
MODEL_ID = "embedding-model";
MODEL_ZIP_URL = "s3://clarifai-testing-persistent-data/test_triton_upload/jina-embeddings-v2-base-en.zip";

///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////

const raw = JSON.stringify({
"user_app_id": {
"user_id": USER_ID,
"app_id": APP_ID
},
"model_versions": [{
"pretrained_model_config": {
"model_zip_url": "s3://clarifai-testing-persistent-data/test_triton_upload/jina-embeddings-v2-base-en.zip",
"input_fields_map": {
"text": "text"
},
"output_fields_map": {
"embeddings": "embeddings"
}
}
}]
});

const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Key " + PAT
},
body: raw
};

fetch(`https://api.clarifai.com/v2/models/${MODEL_ID}/versions`, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log("error", error));

</script>
59 changes: 59 additions & 0 deletions code_snippets/api-guide/model/import-model/import_6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.clarifai.example;

import com.clarifai.grpc.api.*;
import com.clarifai.channel.ClarifaiChannel;
import com.clarifai.credentials.ClarifaiCallCredentials;
import com.clarifai.grpc.api.status.StatusCode;
import com.google.protobuf.Struct;
import com.google.protobuf.Value;

public class ClarifaiExample {

////////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, model ID, and model ZIP 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 these to import your own model
static final String MODEL_ID = "embedding-model";
static final String MODEL_ZIP_URL = "s3://clarifai-testing-persistent-data/test_triton_upload/jina-embeddings-v2-base-en.zip";

///////////////////////////////////////////////////////////////////////////////////
// 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));

Struct.Builder input__fields_params = Struct.newBuilder()
.putFields("text", Value.newBuilder().setStringValue("text").build());

Struct.Builder output__fields_params = Struct.newBuilder()
.putFields("embeddings", Value.newBuilder().setStringValue("embeddings").build());

SingleModelResponse postModelVersionsResponse = stub.postModelVersions(
PostModelVersionsRequest.newBuilder()
.setUserAppId(UserAppIDSet.newBuilder().setUserId(USER_ID).setAppId(APP_ID))
.setModelId(MODEL_ID)
.addModelVersions(ModelVersion.newBuilder()
.setPretrainedModelConfig(PretrainedModelConfig.newBuilder()
.setInputFieldsMap(input__fields_params)
.setOutputFieldsMap(output__fields_params)
.setModelZipUrl(MODEL_ZIP_URL)
)
)
.build()
);

if (postModelVersionsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
throw new RuntimeException("Post model outputs failed, status: " + postModelVersionsResponse.getStatus());
}

}
}
58 changes: 58 additions & 0 deletions code_snippets/api-guide/model/import-model/import_6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//index.js file

/////////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, model ID, and model ZIP 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 these to import your own model
const MODEL_ID = "embedding-model";
const MODEL_ZIP_URL = "s3://clarifai-testing-persistent-data/test_triton_upload/jina-embeddings-v2-base-en.zip";

/////////////////////////////////////////////////////////////////////////////
// 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.PostModelVersions(
{
user_app_id: {
"user_id": USER_ID,
"app_id": APP_ID
},
model_id: MODEL_ID,
model_versions: [{
pretrained_model_config: {
model_zip_url: MODEL_ZIP_URL,
input_fields_map: {
text: "text"
},
output_fields_map: {
embeddings: "embeddings"
}
}
}]
},
metadata,
(err, response) => {
if (err) {
throw new Error(err);
}

if (response.status.code !== 10000) {
console.error('Post models failed, status:', response.status);
throw new Error("Post models failed, status: " + response.status.description);
}
}
);
77 changes: 77 additions & 0 deletions code_snippets/api-guide/model/import-model/import_6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

require __DIR__ . "/vendor/autoload.php";

/////////////////////////////////////////////////////////////////////////////////////////////////
// In this section, we set the user authentication, app ID, model ID, and model ZIP 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 these to import your own model
$MODEL_ID = "embedding-model";
$MODEL_ZIP_URL = "s3://clarifai-testing-persistent-data/test_triton_upload/jina-embeddings-v2-base-en.zip";

///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////

use Clarifai\ClarifaiClient;
use Clarifai\Api\PostModelVersionsRequest;
use Clarifai\Api\Status\StatusCode;
use Clarifai\Api\UserAppIDSet;
use Clarifai\Api\ModelVersion;
use Clarifai\Api\PretrainedModelConfig;
use Google\Protobuf\Struct;

$client = ClarifaiClient::grpc();

$metadata = ["Authorization" => ["Key " . $PAT]];

$userDataObject = new UserAppIDSet([
"user_id" => $USER_ID,
"app_id" => $APP_ID,
]);

// create Struct instances
$input__fields_params = new Struct();
$input__fields_params->text = "text";

$output__fields_params = new Struct();
$output__fields_params->embeddings = "embeddings";

// Let's make a RPC call to the Clarifai platform. It uses the opened gRPC client channel to communicate a
// request and then wait for the response
[$response, $status] = $client->PostModelVersions(
// The request object carries the request along with the request status and other metadata related to the request itself
new PostModelVersionsRequest([
"user_app_id" => $userDataObject,
"model_id" => $MODEL_ID,
"model_versions" => [
new ModelVersion([
"pretrained_model_config" => new PretrainedModelConfig([
"input_fields_map" => $input__fields_params,
"output_fields_map" => $output__fields_params,
"model_zip_url" => $MODEL_ZIP_URL
])
]),
],
]),
$metadata
)->wait();

// A response is returned and the first thing we do is check the status of it
// A successful response will have a status code of 0; otherwise, there is some error
if ($status->code !== 0) {
throw new Exception("Error: {$status->details}");
}
// In addition to the RPC response status, there is a Clarifai API status that reports if the operation was a success or failure
// (not just that the communication was successful)
if ($response->getStatus()->getCode() != StatusCode::SUCCESS) {
throw new Exception("Failure response: " . $response->getStatus()->getDescription() . " " . $response->getStatus()->getDetails());
}

?>
35 changes: 19 additions & 16 deletions code_snippets/api-guide/model/import-model/import_6.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
########################################################################################
# In this section, we set the user authentication, app ID, and model ID.
#############################################################################################
# In this section, we set the user authentication, app ID, model ID, and model ZIP 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 these to import your own model
MODEL_ID = "embedding-model"
MODEL_ZIP_URL = 's3://clarifai-testing-persistent-data/test_triton_upload/jina-embeddings-v2-base-en.zip'
MODEL_ZIP_URL = "s3://clarifai-testing-persistent-data/test_triton_upload/jina-embeddings-v2-base-en.zip"

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
Expand All @@ -18,10 +18,23 @@
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
from google.protobuf.struct_pb2 import Struct

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

input__fields_params = Struct()
input__fields_params.update({"text": "text"})

output__fields_params = Struct()
output__fields_params.update({"embeddings": "embeddings"})

pretrained_config = resources_pb2.PretrainedModelConfig(
input_fields_map=input__fields_params,
output_fields_map=output__fields_params,
model_zip_url=MODEL_ZIP_URL,
)

metadata = (("authorization", "Key " + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)
Expand All @@ -31,22 +44,12 @@
user_app_id=userDataObject,
model_id=MODEL_ID,
model_versions=[
resources_pb2.ModelVersion(
pretrained_model_config=resources_pb2.PretrainedModelConfig(
model_zip_url=MODEL_ZIP_URL,
input_fields_map=resources_pb2.InputFieldsMap(
text='text'
)
),
output_fields_map=resources_pb2.OutputFieldsMap(
embeddings='embeddings'
)
)
resources_pb2.ModelVersion(pretrained_model_config=pretrained_config)
],
),
metadata=metadata,
)

if post_model_versions.status.code != status_code_pb2.SUCCESS:
print(post_model_versions.status)
raise Exception("Post models versions failed, status: " + post_model_versions.status.description)
raise Exception("Post models versions failed, status: " + post_model_versions.status.description)
9 changes: 4 additions & 5 deletions code_snippets/api-guide/model/import-model/import_6.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ curl -X POST "https://api.clarifai.com/v2/users/YOUR_USER_ID_HERE/apps/YOUR_APP_
"model_zip_url": "s3://clarifai-testing-persistent-data/test_triton_upload/jina-embeddings-v2-base-en.zip",
"input_fields_map": {
"text": "text"
},
"output_fields_map": {
"embeddings": "embeddings"
},
"output_fields_map": {
"embeddings": "embeddings"
}
}
}
}]
}'
11 changes: 3 additions & 8 deletions docs/api-guide/model/import-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,30 +257,25 @@ Next, import the model by creating a new version for it.

<Tabs>

<!--
<TabItem value="python" label="Python">
<CodeBlock className="language-python">{PythonImport6}</CodeBlock>
</TabItem>

<TabItem value="js_rest" label="JavaScript (REST)">
<CodeBlock className="language-javascript">{JSImport6}</CodeBlock>
</TabItem>
-->
<!--

<TabItem value="nodejs" label="NodeJS">
<CodeBlock className="language-javascript">{NodeImport6}</CodeBlock>
</TabItem>
-->
<!--

<TabItem value="java" label="Java">
<CodeBlock className="language-java">{JavaImport6}</CodeBlock>
</TabItem>
-->
<!--

<TabItem value="php" label="PHP">
<CodeBlock className="language-php">{PHPImport6}</CodeBlock>
</TabItem>
-->

<TabItem value="curl" label="cURL">
<CodeBlock className="language-bash">{CurlImport6}</CodeBlock>
Expand Down

0 comments on commit 072ab00

Please sign in to comment.