This repository was archived by the owner on May 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (32 loc) · 1.46 KB
/
index.js
File metadata and controls
37 lines (32 loc) · 1.46 KB
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
// import * as tf from '@tensorflow/tfjs';
const express = require('express');
const tf = require('@tensorflow/tfjs-node');
const tokenize = require('./tokenize');
let model;
const app = express();
app.use(express.urlencoded({extended:false}));
app.use(express.json());
app.set('port', 3000);
app.post('/predict', async (req, res) => {
const message = req.body.message;
const result = await loadModel(message);
res.json(result).end();
})
const loadModel = async (message) => {
const lowercaseSentenceArray = message.toLowerCase().replace(/[^\w\s]/g, ' ').split(' ');
const padding = lowercaseSentenceArray.splice(0, 19);
const inputTensor = tf.tensor(tokenize(padding));
const prediction = model.predict(inputTensor);
const result = prediction.dataSync()[1] > 0.5 ? 'SPAM' : 'NO SPAM';
console.log(`${message}: ${result}`);
return { result, prediction: prediction.dataSync()[1] };
}
// loadModel("mano a que hora vienes por el pan");
// loadModel("BCP te informa que tienes una deuda pendiente, ingresa a este enlace http");
// loadModel("Tu tarjeta ha sido bloqueada, por favor enviame tu toquen");
// loadModel("tu tarjeta ha sido bloqueada, por favor enviame tu token o comunicate al 999");
// loadModel("llamaste a pablo? avisame porfa");
app.listen(app.get('port'), async ()=> {
console.log(`Server listening on port ${app.get('port')}`);
model = await tf.loadLayersModel('https://assetshub.blob.core.windows.net/models/model.json');
});