Skip to content

Commit 854a5a2

Browse files
committed
feat: add new samples and review model and other calls
1 parent 4d65747 commit 854a5a2

File tree

9 files changed

+206
-48
lines changed

9 files changed

+206
-48
lines changed

README.adoc

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,26 @@ Clone the repository:
5757

5858
- `$ git clone https://github.com/modzy/sdk-javascript.git`
5959

60-
++++
61-
<p>Use <a href=https://yarnpkg.com/ style="text-decoration:none">yarn</a> to package and install the SDK. </p>
62-
++++
60+
Use https://yarnpkg.com/[yarn] to package and install the SDK:
61+
62+
Globally:
63+
64+
- `$ yarn global add ./sdk-javascript`
65+
66+
In an existing project (directly from github):
67+
68+
- `$ yarn add modzy/sdk-javascript`
69+
70+
71+
Or you can use https://nodejs.org/[npm]:
72+
73+
Globally
74+
75+
- `$ npm install -g ./sdk-javascript`
6376

64-
- `$ yarn add ./sdk-javascript`
77+
In an existing project (directly from github):
6578

66-
//no reference to node.js in here but it is in contributing.
79+
- `$ yarn install modzy/sdk-javascript`
6780

6881
== Initialize
6982

@@ -76,6 +89,12 @@ link:https://models.modzy.com/docs/users-keys/api-keys/retrieve-users-api-keys[L
7689

7790
[source, js]
7891
----
92+
const keys = await modzyClient.getAccountingClient().getAPIKeys('your@email.com');
93+
keys.forEach(
94+
keys => {
95+
console.log(JSON.stringify(model));
96+
}
97+
);
7998
----
8099

81100
The *body* is the prefix’s complement and it’s required to perform API requests. Since it’s not stored on Modzy’s servers, it cannot be recovered. Make sure to save it securely. If lost, you can link:https://models.modzy.com/docs/users-keys/api-keys/replace-API-key[replace the API key's body].
@@ -84,6 +103,9 @@ link:https://models.modzy.com/docs/users-keys/api-keys/retrieve-full-API-key[Ret
84103

85104
[source, js]
86105
----
106+
107+
const hash = await modzyClient.getAccountingClient().getKeyBody('yourKey');
108+
console.log("The hash is: "+hash);
87109
----
88110

89111
Get authenticated with your API key:
@@ -116,15 +138,12 @@ link:https://models.modzy.com/docs/marketplace/models/retrieve-models[List model
116138

117139
[source, js]
118140
----
119-
try{
120-
const models = await modzyClient.getAllModels();
121-
models.forEach(
122-
model => {
123-
console.log(JSON.stringify(model));
124-
}
125-
);
126-
}
127-
141+
const models = await modzyClient.getAllModels();
142+
models.forEach(
143+
model => {
144+
console.log(JSON.stringify(model));
145+
}
146+
);
128147
----
129148

130149
Tags help categorize and filter models. They make model browsing easier.
@@ -133,14 +152,24 @@ link:https://models.modzy.com/docs/marketplace/tags/retrieve-tags[List tags]:
133152

134153
[source, js]
135154
----
136-
155+
const tags = await modzyClient.getAllTags();
156+
tags.forEach(
157+
tag => {
158+
console.log(JSON.stringify(tag));
159+
}
160+
);
137161
----
138162

139163
link:https://models.modzy.com/docs/marketplace/tags/retrieve-models-by-tags[List models by tag]:
140164

141165
[source, js]
142166
----
143-
167+
const tagsModels = await modzyClient.getTagsAndModels("language_and_text");
168+
tagsModels.models.forEach(
169+
model => {
170+
console.log(JSON.stringify(model));
171+
}
172+
);
144173
----
145174

146175
=== Get a model's details
@@ -165,7 +194,17 @@ link:https://models.modzy.com/docs/marketplace/versions/retrieve-version-details
165194

166195
[source, js]
167196
----
168-
197+
let modelVersion = await modzyClient.getModelVersion("ed542963de", "0.0.27");
198+
console.log("inputs:");
199+
for(key in modelVersion.inputs){
200+
let input = modelVersion.inputs[key];
201+
console.log(` key ${input.name}, type ${input.acceptedMediaTypes}, description: ${input.description}`);
202+
}
203+
console.log("outputs:");
204+
for(key in modelVersion.outputs){
205+
let output = modelVersion.outputs[key];
206+
console.log(` key ${output.name}, type ${output.mediaType}, description: ${output.description}`);
207+
}
169208
----
170209

171210
=== Submit a job and get results

contributing.adoc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Clone the repository:
8686

8787
Install dependencies:
8888

89-
- `$ yarn install`
89+
- `$ yarn install` or `$ npm install`
9090

9191
Create a branch for your awesome new feature:
9292

@@ -134,19 +134,23 @@ MODZY_API_KEY=my_key.modzy
134134

135135
Run tests:
136136

137-
- `$ yarn test`
137+
- `$ yarn test` or `$ npm test`
138138

139139
Or specify the test that you want to run:
140140

141141
- `$ yarn test -- model-client.test.js`
142142

143+
With npm:
144+
145+
- `$ npm test -- model-client.test.js`
146+
143147
=== 4. Document your changes
144148

145149
Add supporting documentation for your code.
146150

147151
=== 5. Transpile the code
148152

149-
- `$ yarn build`
153+
- `$ yarn build` or `$ npm run-script build`
150154

151155
=== 6. Send a pull request
152156

package.json

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
{
22
"name": "modzy-sdk",
3-
"version": "1.0.1",
3+
"version": "0.7.2",
4+
"description": "Javascript SDK for Modzy",
45
"main": "dist/main.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/modzy/sdk-javascript.git"
9+
},
10+
"keywords": [
11+
"Modzy",
12+
"SDK"
13+
],
14+
"author": "Modzy",
15+
"bugs": {
16+
"url": "https://github.com/modzy/sdk-javascript/issues"
17+
},
18+
"homepage": "https://github.com/modzy/sdk-javascript#readme",
519
"scripts": {
620
"docs": "jsdoc -c jsdoc.json",
721
"build": "babel src --out-dir=dist",
822
"test": "jest --detectOpenHandles"
923
},
1024
"dependencies": {
11-
"axios": "^0.19.2",
25+
"axios": "^0.21.1",
1226
"dotenv": "^8.2.0",
13-
"log4js": "^6.1.2"
27+
"log4js": "^6.1.2",
28+
"string-similarity": "^4.0.4"
1429
},
1530
"devDependencies": {
1631
"@babel/cli": "^7.8.4",
1732
"@babel/core": "^7.8.7",
1833
"@babel/plugin-proposal-class-properties": "^7.8.3",
19-
"@babel/preset-env": "^7.8.7",
34+
"@babel/preset-env": "^7.8.7",
2035
"babel-jest": "^25.1.0",
2136
"jest": "^25.1.0",
2237
"jsdoc": "^3.6.4"

samples/model_samples.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ async function modelsSample(){
4848
models = await modzyClient.getModels(
4949
/*modelId*/null, /*author*/'Open Source', /*createdByEmail*/null, /*name*/'Image', /*description*/null,
5050
/*isActive*/true, /*isExpired*/null, /*isRecommended*/null, /*lastActiveDateTime*/null, /*expirationDateTime*/null,
51-
/*page*/null, /*perPage*/100, /*direction*/null, /*sortBy*/null);
51+
/*page*/null, /*perPage*/1, /*direction*/null, /*sortBy*/null);
5252
logger.info( `Active open source models which name starts with 'Image' ${models.length}` );
5353
// Get model details
5454
// the models route didn't return much info about the models, just modelId, latestVersion and versions:
5555
for( mKey in models){
5656
let model = models[mKey];
57-
logger.info(Object.keys(model).toString().replace('\n', ' '));
57+
//logger.info(Object.keys(model).toString().replace('\n', ' '));
58+
logger.info(model);
5859
// In order to get more info about the models you need to get the details by identifier
5960
model = await modzyClient.getModel(model.modelId);
6061
// then you'll get all the details about the model

src/accounting-client.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const axios = require('axios').default;
2+
const logger = require('log4js').getLogger("modzy.accounting-client");
3+
4+
const ApiError = require('./api-error.js');
5+
6+
/**
7+
* Utility class that mask the interaction with the model api
8+
*/
9+
class AccountingClient{
10+
11+
/**
12+
* Create a ModelClient
13+
* @param {string} baseURL - base url of modzy api (i.e.: https://modzy.url/api)
14+
* @param {string} apiKey - api key to access modzy
15+
*/
16+
constructor(baseURL, apiKey){
17+
this.baseURL = baseURL + ( baseURL.endsWith('/') ? "" : "/" ) + "accounting";
18+
this.apiKey = apiKey;
19+
}
20+
21+
getAPIKeys(email){
22+
if( email !== null && typeof email !== "string" ){
23+
throw("the email param should be a not empty string");
24+
}
25+
const requestURL = `${this.baseURL}/access-keys/user/${email}`;
26+
logger.debug(`getAPIKeys(${email}) GET ${requestURL}`);
27+
return axios.get(
28+
requestURL,
29+
{headers: {'Authorization':`ApiKey ${this.apiKey}`}}
30+
)
31+
.then(
32+
( response )=>{
33+
logger.info(`getAPIKeys(${email}) :: ${response.status} ${response.statusText}`);
34+
return response.data;
35+
}
36+
)
37+
.catch(
38+
( error )=>{
39+
throw( new ApiError( error ) );
40+
}
41+
);
42+
}
43+
44+
getKeyBody(key){
45+
if( key !== null && typeof key !== "string" ){
46+
throw("the key param should be a not empty string");
47+
}
48+
const requestURL = `${this.baseURL}/access-keys/${key}/hash`;
49+
logger.debug(`getAPIKeys(${key}) GET ${requestURL}`);
50+
return axios.post(
51+
requestURL,
52+
{},
53+
{headers: {'Authorization':`ApiKey ${this.apiKey}`}}
54+
)
55+
.then(
56+
( response )=>{
57+
logger.info(`getKeyBody(${key}) :: ${response.status} ${response.statusText}`);
58+
return response.data;
59+
}
60+
)
61+
.catch(
62+
( error )=>{
63+
throw( new ApiError( error ) );
64+
}
65+
);
66+
}
67+
68+
}
69+
70+
module.exports = AccountingClient;

src/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ const ModelClient = require('./model-client.js');
44
const TagClient = require( './tag-client.js');
55
const JobClient = require('./job-client.js');
66
const ResultClient = require('./result-client.js');
7+
const AccountingClient = require('./accounting-client.js');
78
const ModzyClient = require('./modzy-client.js');
89
const ApiError = require('./api-error.js');
910

1011
/**
11-
* @type {{TagClient: TagClient, ResultClient: ResultClient, ModelClient: ModelClient, ModzyClient: ModzyClient, ApiError: ApiError, JobClient: JobClient}}
12+
* @type {{TagClient: TagClient, ResultClient: ResultClient, ModelClient: ModelClient, AccountingClient: AccountingClient, ModzyClient: ModzyClient, ApiError: ApiError, JobClient: JobClient}}
1213
*/
1314
module.exports = {
1415
ModelClient,
1516
TagClient,
1617
JobClient,
1718
ResultClient,
19+
AccountingClient,
1820
ModzyClient,
1921
ApiError
2022
};

src/model-client.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const axios = require('axios').default;
22
const logger = require('log4js').getLogger("modzy.model-client");
33

44
const ApiError = require('./api-error.js');
5+
const stringSimilarity = require("string-similarity");
56

67
/**
78
* Utility class that mask the interaction with the model api
@@ -198,14 +199,33 @@ class ModelClient{
198199
getModelByName(name){
199200
return this.getModels(
200201
null, null, null, name,
201-
null, null, null, null, null, null,
202-
null, null, null, null
202+
null, true, null, null, null, null,
203+
null, 20, null, "name"
203204
)
204205
.then(
205206
(models) => {
206207
logger.debug(`getModelByName(${name}) models ${models.length}`);
207208
if( models !== null && models.length > 0 ){
208-
logger.debug(`getModelByName(${name}) model ${models[0].modelId}`);
209+
if( models.length > 1 ){
210+
return Promise.all(
211+
models.map( model => this.getModel(model.modelId))
212+
).then(
213+
models2 => {
214+
models2 = models2.map(
215+
model => {
216+
model.distance = stringSimilarity.compareTwoStrings(name, model.name);
217+
return model;
218+
}
219+
).sort(
220+
(a,b) => a.distance - b.distance
221+
);
222+
models2.forEach(model=>logger.debug(`${model.modelId} ${model.name} ${model.distance}`));
223+
logger.debug(`getModelByName(${name}) model ${models2[models2.length-1].modelId} :: ${models2[models2.length-1].name}`);
224+
return models2[models2.length-1];
225+
}
226+
);
227+
}
228+
logger.debug(`getModelByName(${name}) model ${models[0].modelId} `);
209229
return this.getModel(models[0].modelId);
210230
}
211231
else{

src/modzy-client.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const logger = require('log4js').getLogger("modzy");
44
const ModelClient = require('./model-client.js');
55
const TagClient = require('./tag-client.js');
66
const JobClient = require('./job-client.js');
7+
const AccountingClient = require('./accounting-client.js');
78
const ResultClient = require('./result-client.js');
89

910
/**
@@ -31,6 +32,7 @@ class ModzyClient {
3132
this.tagClient = new TagClient(baseURL, apiKey);
3233
this.jobClient = new JobClient(baseURL, apiKey);
3334
this.resultClient = new ResultClient(baseURL, apiKey);
35+
this.accountingClient = new AccountingClient(baseURL, apiKey);
3436
}
3537

3638
/**
@@ -72,6 +74,15 @@ class ModzyClient {
7274
return this.resultClient;
7375
}
7476

77+
/**
78+
* Get the accounting client initialized
79+
* @see {@link AccountingClient}
80+
* @returns {AccountingClient}
81+
*/
82+
getAccountingClient(){
83+
return this.accountingClient;
84+
}
85+
7586
/**
7687
* @see {@link ModelClient#getAllModels}
7788
*/

0 commit comments

Comments
 (0)