Skip to content

Commit 186523f

Browse files
author
Dan Kantor
committed
initial commit
0 parents  commit 186523f

22 files changed

+1791
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules/*
2+
build/*
3+
.DS_Store
4+
aws.json
5+
npm-debug.log
6+
.awspublish*

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2016 Clarifai, Inc.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
# Clarifai JavaScript Client
2+
3+
The official JavaScript client for interacting with the [Clarifai API](https://developer.clarifai.com).
4+
5+
## Basic Use
6+
7+
To start, install the SDK via NPM: `npm install clarifai-official` and initialize with your clientId and
8+
clientSecret:
9+
10+
*This will work in node.js and browsers via [Browserify](http://browserify.org/)*
11+
12+
```js
13+
var Clarifai = require('clarifai-official');
14+
15+
Clarifai.initialize({
16+
'clientId': '{clientId}',
17+
'clientSecret': '{clientSecret}'
18+
});
19+
20+
```
21+
22+
You can also use the SDK by adding this script to your HTML:
23+
24+
```js
25+
<script type="text/javascript" src="https://sdk.clarifai.com/js/clarifai-0.0.14.js"></script>
26+
<script>
27+
Clarifai.initialize({
28+
'clientId': '{clientId}',
29+
'clientSecret': '{clientSecret}'
30+
});
31+
</script>
32+
```
33+
34+
## Table of Contents
35+
36+
#### Tag
37+
38+
* [Get tags for an image via URL](#get-tags-for-an-image-via-url)
39+
* [Get tags for an image via bytes](#get-tags-for-an-image-via-image-bytes)
40+
* [Get tags for multiple images via url](#get-tags-for-multiple-images-via-url)
41+
* [Get tags for an image via url passing in a model](#get-tags-for-an-image-via-url-passing-in-a-model)
42+
* [Get tags for an image via url passing in a language](#get-tags-for-an-image-via-url-passing-in-a-language)
43+
* [Get tags for an image via url and set a localId](#get-tags-for-an-image-via-url-and-set-a-localid)
44+
* [Get tags for an image via url and restrict the tags returned](#get-tags-for-an-image-via-url-and-restrict-the-tags-returned)
45+
46+
#### Info
47+
48+
* [Get API info](#get-api-info)
49+
50+
#### Languages
51+
52+
* [Get supported languages](#get-supported-languages)
53+
54+
#### Color
55+
56+
* [Get colors for an image via url](#get-colors-for-an-image-via-url)
57+
58+
#### Usage
59+
60+
* [Get API usage](#get-api-usage)
61+
62+
#### Feedback
63+
64+
* [Send feedback to the API](#send-feedback-to-the-api)
65+
66+
#### Token
67+
68+
* [Get a token](#get-a-token)
69+
* [Set a token](#set-a-token)
70+
* [Delete a token](#delete-a-token)
71+
72+
#### Promises and Callbacks
73+
74+
* [Instructions](#promises-and-callbacks)
75+
76+
## Examples
77+
78+
### Tag
79+
80+
#### Get tags for an image via url
81+
82+
```js
83+
Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg').then(
84+
handleResponse,
85+
handleError
86+
);
87+
```
88+
89+
#### Get tags for multiple images via url
90+
91+
```js
92+
Clarifai.getTagsByUrl([
93+
'https://samples.clarifai.com/wedding.jpg',
94+
'https://samples.clarifai.com/cookies.jpeg'
95+
]).then(
96+
handleResponse,
97+
handleError
98+
);
99+
```
100+
101+
#### Get tags for an image via image bytes
102+
103+
```js
104+
Clarifai.getTagsByImageBytes('R0lGODlhZAHIAPcAAKeno6Oinc3Do6iVeMe7o1ZEM...').then(
105+
handleResponse,
106+
handleError
107+
);
108+
```
109+
110+
#### Get tags for an image via url passing in a model
111+
112+
```js
113+
Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg', {
114+
'model': 'nsfw-v0.1'
115+
}).then(
116+
handleResponse,
117+
handleError
118+
);
119+
```
120+
121+
#### Get tags for an image via url passing in a language
122+
123+
```js
124+
Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg', {
125+
'language': 'es'
126+
}).then(
127+
handleResponse,
128+
handleError
129+
);
130+
```
131+
132+
#### Get tags for an image via url and set a localId
133+
134+
```js
135+
Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg', {
136+
'localId': 'myLocalId'
137+
}).then(
138+
handleResponse,
139+
handleError
140+
);
141+
```
142+
143+
#### Get tags for an image via url and restrict the tags returned
144+
145+
```js
146+
Clarifai.getTagsByUrl(
147+
'https://samples.clarifai.com/wedding.jpg',
148+
{
149+
'selectClasses': ['people', 'dress', 'wedding']
150+
}
151+
).then(
152+
handleResponse,
153+
handleError
154+
);
155+
```
156+
157+
### Info
158+
159+
#### Get API info
160+
161+
```js
162+
Clarifai.getInfo().then(
163+
handleResponse,
164+
handleError
165+
);
166+
```
167+
168+
### Languages
169+
170+
#### Get supported languages
171+
172+
```js
173+
Clarifai.getLanguages().then(
174+
handleResponse,
175+
handleError
176+
);
177+
```
178+
179+
### Color
180+
181+
#### Get colors for an image via url
182+
183+
```js
184+
Clarifai.getColorsByUrl('https://samples.clarifai.com/wedding.jpg').then(
185+
handleResponse,
186+
handleError
187+
);
188+
```
189+
190+
#### Get colors for an image via image bytes
191+
192+
```js
193+
Clarifai.getColorsByImageBytes('R0lGODlhZAHIAPcAAKeno6Oinc3Do6iVeMe7o1ZEM...').then(
194+
handleResponse,
195+
handleError
196+
);
197+
```
198+
199+
### Usage
200+
201+
#### Get API usage
202+
203+
```js
204+
Clarifai.getUsage().then(
205+
handleResponse,
206+
handleError
207+
);
208+
```
209+
210+
### Feedback
211+
212+
#### Send feedback to the API
213+
214+
```js
215+
Clarifai.createFeedback('https://samples.clarifai.com/wedding.jpg', {
216+
'addTags': ['family', 'friends',],
217+
'removeTags': ['military', 'protest'],
218+
}).then(
219+
handleResponse,
220+
handleError
221+
);
222+
```
223+
224+
### Token
225+
226+
#### Get a token
227+
228+
**Note:** You should not have to call this directly in most cases. Any method that needs a token will call
229+
it for you.
230+
231+
```js
232+
Clarifai.getToken().then(
233+
function(response) {
234+
console.log(response);
235+
},
236+
function(err){
237+
console.log(err);
238+
}
239+
);
240+
```
241+
242+
#### Set a token
243+
244+
```js
245+
var tokenSetBoolean = Clarifai.setToken('some-token-string');
246+
```
247+
248+
#### Delete a token
249+
250+
```js
251+
Clarifai.deleteToken();
252+
```
253+
254+
### Promises and Callbacks
255+
256+
All methods return promises. If you'd rather user callbacks, just pass in a callback function as the last
257+
param to any method. If there are multiple params and some are optional, you'll need to pass in `null` for
258+
those.

examples/browser-example.html

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<html>
2+
<head>
3+
<title>Browser Example</title>
4+
</head>
5+
<body>
6+
<script type="text/javascript" src="/clarifai-@@VERSION.js"></script>
7+
<script type="text/javascript">
8+
Clarifai.initialize({
9+
'clientId': '@@CLIENT_ID',
10+
'clientSecret': '@@CLIENT_SECRET'
11+
});
12+
13+
// get a token
14+
function getToken() {
15+
Clarifai.getToken().then(
16+
handleResponse,
17+
handleError
18+
);
19+
};
20+
21+
// get tags with an array of images
22+
function getTags() {
23+
Clarifai.getTagsByUrl([
24+
'https://samples.clarifai.com/wedding.jpg',
25+
'https://samples.clarifai.com/cookies.jpeg'
26+
]).then(
27+
handleResponse,
28+
handleError
29+
);
30+
};
31+
32+
// select which tags are returned
33+
function selectClasses() {
34+
Clarifai.getTagsByUrl(
35+
'https://samples.clarifai.com/wedding.jpg',
36+
{
37+
'selectClasses': ['people', 'dress', 'wedding']
38+
}
39+
).then(
40+
handleResponse,
41+
handleError
42+
);
43+
};
44+
45+
// get api info
46+
function getInfo() {
47+
Clarifai.getInfo().then(
48+
handleResponse,
49+
handleError
50+
);
51+
};
52+
53+
// get supported languages
54+
function getLanguages() {
55+
Clarifai.getLanguages().then(
56+
handleResponse,
57+
handleError
58+
);
59+
};
60+
61+
// get colors
62+
function getColors() {
63+
Clarifai.getColorsByUrl('https://samples.clarifai.com/wedding.jpg').then(
64+
handleResponse,
65+
handleError
66+
);
67+
};
68+
69+
// get api usage
70+
function getUsage() {
71+
Clarifai.getUsage().then(
72+
handleResponse,
73+
handleError
74+
);
75+
};
76+
77+
// create feedback
78+
function createFeedback() {
79+
Clarifai.createFeedback('https://samples.clarifai.com/wedding.jpg', {
80+
'addTags': ['family', 'friends',],
81+
'removeTags': ['military', 'protest'],
82+
}).then(
83+
handleResponse,
84+
handleError
85+
);
86+
};
87+
88+
function handleResponse(response){
89+
console.log('promise response:', response);
90+
};
91+
92+
function handleError(err){
93+
console.log('promise error:', err);
94+
};
95+
96+
getTags();
97+
selectClasses();
98+
getColors();
99+
getUsage();
100+
getLanguages();
101+
getInfo();
102+
createFeedback();
103+
104+
105+
106+
</script>
107+
</body>
108+
</html>

0 commit comments

Comments
 (0)