-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_model_structure.js
128 lines (113 loc) · 4.02 KB
/
test_model_structure.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
128
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const path = require('path');
// Load the Clarifai proto file
const packageDefinition = protoLoader.loadSync(
path.join(__dirname, 'proto/clarifai/api/service.proto'),
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs: [path.join(__dirname, 'proto')]
}
);
const service = grpc.loadPackageDefinition(packageDefinition).clarifai.api.V2;
const stub = new service.V2('api.clarifai.com:443', grpc.credentials.createSsl());
// Clean and set API key
const apiKey = (process.env.new_clarifai_key || '').trim().replace(/[^a-zA-Z0-9]/g, '');
const metadata = new grpc.Metadata();
metadata.set('authorization', `Key ${apiKey}`);
async function testModelStructure() {
try {
// First, create a model with basic structure
const modelId = 'test-model-structure';
const concepts = ['cats', 'dogs'];
console.log('Creating test model...');
const modelRequest = {
model: {
id: modelId,
name: modelId,
output_info: {
data: {
concepts: concepts.map(concept => ({
id: concept,
name: concept
}))
},
output_config: {
concepts_mutually_exclusive: false,
closed_environment: false
}
}
}
};
// Create model
const modelResponse = await new Promise((resolve, reject) => {
stub.PostModels(modelRequest, metadata, (err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
});
});
console.log('Model creation response:', JSON.stringify(modelResponse, null, 2));
if (modelResponse.status.code !== 10000 && modelResponse.status.code !== 21202) {
throw new Error(`Model creation failed: ${modelResponse.status.description}`);
}
// Get model details to understand structure
console.log('\nFetching model details...');
const getModelResponse = await new Promise((resolve, reject) => {
stub.GetModel(
{ model_id: modelId },
metadata,
(err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
}
);
});
console.log('Model details:', JSON.stringify(getModelResponse, null, 2));
// Try to create a version with the same structure
console.log('\nCreating model version...');
const versionRequest = {
model_id: modelId,
version: {
output_info: {
data: {
concepts: concepts.map(concept => ({
id: concept,
name: concept
}))
},
output_config: {
concepts_mutually_exclusive: false,
closed_environment: false
}
}
}
};
const versionResponse = await new Promise((resolve, reject) => {
stub.PostModelVersions(
versionRequest,
metadata,
(err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
}
);
});
console.log('Version creation response:', JSON.stringify(versionResponse, null, 2));
} catch (error) {
console.error('Error:', error);
}
}
testModelStructure();