vendredi 19 juin 2020

Pyhton Script getting stuck when called with node.js child_process

I am trying to use a trained model predictor in python into my web app using node.js using child_process, I am able to run normal commands however when I try to use pickle or joblib to load my trained data set, the nodemon server gets stuck.

Node-Js Code:

const express = require('express'); //used for server making
const ejs = require('ejs');//used to apply javascript into html code
const lodash = require('lodash');//used for text variations
const bodyParser = require('body-parser');//used to recives data from forms


const app=express()

app.set('view engine','ejs');


app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));

app.get("/",(req,res)=>{
  res.render("home");
});

function callPythonCode(name){
  var spawn = require("child_process").spawn;
  var process = spawn('python',["AnimePredictor.py",name] );

  process.stdout.on('data',data=>{
    // res.send(data.toString())
    console.log(data.toString());
  });


};

app.post("/",(req,res)=>{
  const name = req.body.Name;
  callPythonCode(name);
})




app.listen(process.env.PORT ||3000,function(){
  console.log("server started at port 3000");
})

And this is the python code:

import pickle
import sys
import joblib

print("working")

# with open("public/animeTrain.pickle",'rb') as f:
#     anime,indices=pickle.load(f)

anime,indices = joblib.load("joblib.sav")
print("working2")

def get_index_from_name(name):
    return anime[anime["name"]==name].index.tolist()[0]

print("working3")


all_anime_names = list(anime.name.values)


def print_similar_animes(query=None,id=None):
    if id:
        for id in indices[id][1:]:
            print(anime.iloc[id]["name"])
    if query:
        found_id = get_index_from_name(query)
        for id in indices[found_id][1:]:
            print(anime.iloc[id]["name"])


print_similar_animes(sys.argv[1])

I am using pickle so that I don't have to train it over and over again, but any kind of alternative would be appreciated.




Aucun commentaire:

Enregistrer un commentaire