Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stenas committed May 27, 2021
1 parent 81dd606 commit c21eb74
Show file tree
Hide file tree
Showing 8 changed files with 3,035 additions and 104 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log
ecosystem.config.js
.gitignore
105 changes: 2 additions & 103 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,104 +1,3 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
.idea/
/ecosystem.config.js
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:lts
WORKDIR /var/www/html
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package.json ./

RUN npm install pm2 -g
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
COPY . .

EXPOSE 8088
CMD ["pm2-runtime", "app.js","--name=sentence_classify"]
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# sentences-classify
# sentences-classify

Service in Node JS to classify sentences, only EN.
## Usage
Build image, run container, show logs
```
docker-compose build && docker-compose up -d && docker-compose logs -f
```
## Endpoints
Classify sentence
```
Method: POST
Parameter: sentence
URL: http://[HOST]:[PORT]/sentence
```
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.
a
## License
[MIT](https://choosealicense.com/licenses/mit/)
53 changes: 53 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const express = require('express')
require('@tensorflow/tfjs');
const toxicity = require('@tensorflow-models/toxicity');
const bodyParser = require('body-parser');

const app = express()
const port = 8088

// The minimum prediction confidence.
const threshold = 0.9;

function loadToxicity(req, res, body){
toxicity.load(threshold).then(model => {
model.classify([body.sentence]).then(predictions => {
// `predictions` is an array of objects, one for each prediction head,
// that contains the raw probabilities for each input along with the
// final prediction in `match` (either `true` or `false`).
// If neither prediction exceeds the threshold, `match` is `null`.
let hasToxic = false;
let toxicSentences = []
let message = 'Your sentence contains no toxic words';
for(i=0;i<7;i++) {
if (predictions[i].results[0].match === true) {
hasToxic = true;
message = 'Your sentence contains toxic words';
toxicSentences.push({'label':predictions[i].label.toUpperCase(),'pecentage':predictions[i].results[0].probabilities[1].toFixed(4)*100+"%"})
}
}

res.status(200).json({
error:false,
message:message,
data:toxicSentences
})

});
});
}

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.get('/', (req, res) => {
res.send("<h1>Sentence Classify Service</h1>")
});

app.post('/sentence', (req, res) => {
loadToxicity(req, res, req.body);
})

app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`)
})
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.5'

services:
sentence-classify-app:
build:
context: '.'
args:
uid: 3462346345
container_name: sentence-classify-app
ports:
- 8088:8088
Loading

0 comments on commit c21eb74

Please sign in to comment.