-
Notifications
You must be signed in to change notification settings - Fork 7
/
update_model_config_v5.js
127 lines (119 loc) · 4.76 KB
/
update_model_config_v5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const { ClarifaiStub, grpc } = require('clarifai-nodejs-grpc');
const stub = ClarifaiStub.grpc();
const metadata = new grpc.Metadata();
const apiKey = process.env.new_clarifai_key.trim().replace(/[^a-zA-Z0-9]/g, '');
metadata.set('authorization', `Key ${apiKey}`);
async function updateModelConfig() {
try {
const modelId = 'catsdogstest';
// Step 1: Get current model to verify its existence
console.log('Step 1: Verifying model exists...');
const currentModel = await new Promise((resolve, reject) => {
stub.GetModel(
{ model_id: modelId },
metadata,
(err, response) => {
if (err) {
console.error('Error getting model:', err);
reject(err);
} else {
console.log('Current model:', JSON.stringify(response, null, 2));
resolve(response);
}
}
);
});
// Step 2: Update model with output_info using specific structure
console.log('Step 2: Updating model configuration...');
const patchResponse = await new Promise((resolve, reject) => {
stub.PatchModels(
{
action: 'overwrite',
models: [{
id: modelId,
output_info: {
params: {},
type: 'concept',
type_ext: 'classifier',
message: 'Show me cats and dogs',
concepts: [
{ id: 'cats', name: 'cats', value: 1 },
{ id: 'dogs', name: 'dogs', value: 1 }
],
output_config: {
concepts_mutually_exclusive: true,
closed_environment: true,
max_concepts: 0,
min_value: 0
}
}
}]
},
metadata,
(err, response) => {
if (err) {
console.error('Error updating model:', err);
reject(err);
} else {
console.log('Model update response:', JSON.stringify(response, null, 2));
resolve(response);
}
}
);
});
// Step 3: Verify the update
console.log('Step 3: Verifying update...');
const verifyResponse = await new Promise((resolve, reject) => {
stub.GetModel(
{ model_id: modelId },
metadata,
(err, response) => {
if (err) {
console.error('Error verifying update:', err);
reject(err);
} else {
console.log('Updated model structure:', JSON.stringify(response, null, 2));
resolve(response);
}
}
);
});
if (verifyResponse.model.output_info) {
console.log('Success: Model configuration updated successfully');
// Step 4: Create a new version
console.log('Step 4: Creating new version...');
const versionResponse = await new Promise((resolve, reject) => {
const versionId = `v${Date.now()}`;
stub.PostModelVersions(
{
model_id: modelId,
version: {
id: versionId,
train_info: {
params: {
template: 'classification_base',
use_embeddings: true
}
}
}
},
metadata,
(err, response) => {
if (err) {
console.error('Error creating version:', err);
reject(err);
} else {
console.log('Version creation response:', JSON.stringify(response, null, 2));
resolve(response);
}
}
);
});
} else {
console.log('Error: Model configuration update failed');
}
} catch (error) {
console.error('Update failed:', error);
}
}
updateModelConfig();