-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_full_workflow.js
287 lines (267 loc) · 9.45 KB
/
test_full_workflow.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const fs = require('fs');
const path = require('path');
const { ClarifaiStub, grpc } = require('clarifai-nodejs-grpc');
const dotenv = require('dotenv');
dotenv.config();
// Initialize the gRPC client
const stub = ClarifaiStub.grpc();
const metadata = new grpc.Metadata();
const pat = (process.env.CLARIFAI_PAT || '').trim();
metadata.set('authorization', `Key ${pat}`);
// Test concepts
const concepts = ['cat', 'dog'];
let modelId;
// Function to read image files and convert to base64
function readImageAsBase64(filepath) {
return fs.readFileSync(filepath).toString('base64');
}
async function createModel() {
console.log('Creating model...');
return new Promise((resolve, reject) => {
const modelRequest = {
user_app_id: {
user_id: process.env.CLARIFAI_USER_ID,
app_id: process.env.CLARIFAI_APP_ID
},
model: {
id: `test-model-${Date.now()}`,
name: `Test Model ${Date.now()}`,
model_type_id: "visual-classifier",
output_info: {
data: {
concepts: concepts.map(concept => ({
id: concept.toLowerCase().replace(/[^a-z0-9]/g, ''),
name: concept,
value: 1
}))
},
output_config: {
concepts_mutually_exclusive: true,
closed_environment: true
}
}
}
};
console.log('Model request structure:', JSON.stringify(modelRequest, null, 2));
stub.PostModels(
modelRequest,
metadata,
(err, response) => {
if (err) {
console.error('Error creating model:', err);
reject(err);
} else {
console.log('Model created successfully:', response);
modelId = modelRequest.model.id;
// Verify model structure after creation
stub.GetModel(
{
user_app_id: {
user_id: process.env.CLARIFAI_USER_ID,
app_id: process.env.CLARIFAI_APP_ID
},
model_id: modelId
},
metadata,
(verifyErr, verifyResponse) => {
if (verifyErr) {
console.error('Error verifying model structure:', verifyErr);
reject(verifyErr);
} else {
console.log('Verified model structure:', JSON.stringify(verifyResponse, null, 2));
resolve(response);
}
}
);
}
}
);
});
}
async function addInputs() {
console.log('Adding inputs...');
const inputs = [];
// Process cat images
for (let i = 1; i <= 10; i++) {
const imagePath = path.join(__dirname, `static/test_images/cats/cat${i}.jpg`);
try {
const base64Data = readImageAsBase64(imagePath);
inputs.push({
data: {
image: {
base64: base64Data,
allow_duplicate_url: true
}
},
concepts: [{
id: 'cat',
name: 'cat',
value: 1
}]
});
console.log(`Successfully added input for cat${i}.jpg`);
} catch (error) {
console.error(`Error processing cat${i}.jpg:`, error);
throw error;
}
}
// Process dog images
for (let i = 1; i <= 10; i++) {
const imagePath = path.join(__dirname, `static/test_images/dogs/dog${i}.jpg`);
try {
const base64Data = readImageAsBase64(imagePath);
inputs.push({
data: {
image: {
base64: base64Data,
allow_duplicate_url: true
}
},
concepts: [{
id: 'dog',
name: 'dog',
value: 1
}]
});
console.log(`Successfully added input for dog${i}.jpg`);
} catch (error) {
console.error(`Error processing dog${i}.jpg:`, error);
throw error;
}
}
return new Promise((resolve, reject) => {
stub.PostInputs(
{
user_app_id: {
user_id: process.env.CLARIFAI_USER_ID,
app_id: process.env.CLARIFAI_APP_ID
},
inputs: inputs
},
metadata,
(err, response) => {
if (err) {
console.error('Error adding inputs:', err);
reject(err);
} else {
console.log('All inputs added successfully');
resolve(response);
}
}
);
});
}
async function waitForInputProcessing() {
console.log('Waiting for inputs to be processed...');
const MAX_ATTEMPTS = 6;
const DELAY = 5000;
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
await new Promise(resolve => setTimeout(resolve, DELAY));
console.log(`Checking input processing status (attempt ${attempt + 1}/${MAX_ATTEMPTS})...`);
try {
const response = await new Promise((resolve, reject) => {
stub.ListInputs(
{
user_app_id: {
user_id: process.env.CLARIFAI_USER_ID,
app_id: process.env.CLARIFAI_APP_ID
},
page: 1,
per_page: 1
},
metadata,
(err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
}
);
});
if (response.status.code === 10000) {
console.log('Inputs processed successfully');
return true;
}
} catch (error) {
console.warn(`Input status check failed (attempt ${attempt + 1}):`, error);
}
}
console.warn('Max processing attempts reached, proceeding with training...');
return false;
}
async function createModelVersion() {
console.log('Creating model version...');
const simpleVersionId = `v${Date.now()}`;
console.log('Creating model version:', simpleVersionId);
return new Promise((resolve, reject) => {
stub.PostModelVersions(
{
user_app_id: {
user_id: process.env.CLARIFAI_USER_ID,
app_id: process.env.CLARIFAI_APP_ID
},
model_id: modelId,
version: {
id: simpleVersionId,
output_info: {
data: {
concepts: concepts.map(concept => ({
id: concept.toLowerCase().replace(/[^a-z0-9]/g, ''),
name: concept,
value: 1
}))
},
output_config: {
concepts_mutually_exclusive: true,
closed_environment: true
}
},
train_info: {
params: {
template: "classification_base_workflow",
use_embeddings: true
},
dataset: concepts.map(concept => ({
id: concept.toLowerCase().replace(/[^a-z0-9]/g, ''),
name: concept
}))
}
}
},
metadata,
(err, response) => {
if (err) {
console.error('Error creating model version:', {
error: err.message,
code: err.code,
details: err.details,
modelId: modelId,
versionId: simpleVersionId
});
reject(err);
} else {
console.log('Training initiated:', {
status: response.status,
modelId: modelId,
versionId: simpleVersionId
});
resolve(response);
}
}
);
});
}
async function main() {
try {
await createModel();
await addInputs();
await waitForInputProcessing();
await createModelVersion();
console.log('Full workflow completed successfully');
} catch (error) {
console.error('Error in workflow:', error);
process.exit(1);
}
}
main();