FredZhang7 commited on
Commit
1413ea5
·
1 Parent(s): 8a107f3

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +50 -0
README.md CHANGED
@@ -74,6 +74,56 @@ print('\033[1;32m' + prediction + '\033[0m' if prediction == 'safe' else '\033[1
74
  Output Example:
75
  ![prediction](./output_example.png)
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  <br>
78
 
79
  # Bias and Limitations
 
74
  Output Example:
75
  ![prediction](./output_example.png)
76
 
77
+ # Tensorflow.js
78
+ ```bash
79
+ npm i @tensorflow/tfjs-node
80
+ ```
81
+ ```javascript
82
+ const tf = require('@tensorflow/tfjs-node');
83
+ const fs = require('fs');
84
+ const { pipeline } = require('stream');
85
+ const { promisify } = require('util');
86
+
87
+
88
+ const download = async (url, path) => {
89
+ // Taken from https://levelup.gitconnected.com/how-to-download-a-file-with-node-js-e2b88fe55409
90
+
91
+ const streamPipeline = promisify(pipeline);
92
+ const response = await fetch(url);
93
+
94
+ if (!response.ok) {
95
+ throw new Error(`unexpected response ${response.statusText}`);
96
+ }
97
+
98
+ await streamPipeline(response.body, fs.createWriteStream(path));
99
+ };
100
+
101
+
102
+ async function run() {
103
+ // download saved model and variables from https://huggingface.co/FredZhang7/google-safesearch-mini/tree/main/tensorflow
104
+ if (!fs.existsSync('tensorflow')) {
105
+ fs.mkdirSync('tensorflow');
106
+ await download('https://huggingface.co/FredZhang7/google-safesearch-mini/resolve/main/tensorflow/saved_model.pb', 'tensorflow/saved_model.pb');
107
+ fs.mkdirSync('tensorflow/variables');
108
+ await download('https://huggingface.co/FredZhang7/google-safesearch-mini/resolve/main/tensorflow/variables/variables.data-00000-of-00001', 'tensorflow/variables/variables.data-00000-of-00001');
109
+ await download('https://huggingface.co/FredZhang7/google-safesearch-mini/resolve/main/tensorflow/variables/variables.index', 'tensorflow/variables/variables.index');
110
+ }
111
+
112
+ // load model and image
113
+ const model = await tf.node.loadSavedModel('./tensorflow/');
114
+ const image = tf.node.decodeImage(fs.readFileSync('cat.jpg'), 3);
115
+
116
+ // predict
117
+ const input = tf.expandDims(image, 0);
118
+ const tensor = model.predict(input);
119
+ const max = tensor.argMax(1);
120
+ const classes = ['nsfw_gore', 'nsfw_suggestive', 'safe'];
121
+ console.log('\x1b[32m%s\x1b[0m', classes[max.dataSync()[0]], '\n');
122
+ }
123
+
124
+ run();
125
+ ```
126
+
127
  <br>
128
 
129
  # Bias and Limitations