-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #308 from Clarifai/Alfrick-branch
DOC-464 <Import model snippets>
- Loading branch information
Showing
7 changed files
with
273 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters