diff --git a/routes/processes/nishauri.js b/routes/processes/nishauri.js index e291365..9052aa9 100644 --- a/routes/processes/nishauri.js +++ b/routes/processes/nishauri.js @@ -4,10 +4,23 @@ const request = require('request'); const https = require('https'); const moment = require("moment"); const base64 = require("base64util"); +const {spawn} = require('child_process'); + require("dotenv").config(); + +//import { client_chat } from "@gradio/client"; +//const client_chat = require('@gradio/client', { force: true }); +//const dynamic = new Function('modulePath', 'return import(modulePath)'); + + +//const { client_chat } = require("@gradio/client"); + + + //const Op = require("sequelize"); const { Op } = require("sequelize"); var bcrypt = require('bcrypt'); + //const Sequelize = require("sequelize"); //const Sequelize = require('sequelize'); @@ -1766,35 +1779,29 @@ router.post('/bmi_calculator', async (req, res) => { router.post('/chat', async(req, res) => { const question_ = req.body.question; const userid = req.body.user_id; + var dataToSend; + var log_activity_=NLogs.create({ user_id:base64.decode(userid), access:'CHAT'}); - - //client_payload='{"question": "hello"}'; - - const url_details = { - url: process.env.CHAT_URL+'chatbot', - // json: true, - form: { question: question_ }, - "rejectUnauthorized": false, - } - request.post(url_details, (err, res_, body) => { - if (err) { - return console.log(err) - } - //var obj_ = body; - //console.log(body); - var log_activity_=NLogs.create({ user_id:base64.decode(userid), access:'CHAT'}); - - var obj = JSON.parse(body); - return res - .status(200) - .json({ + // spawn new child process to call the python script + const python = spawn('python3', ['./routes/processes/nishauri_chatbot.py',question_]); + + python.stdout.on('data', function (data) { + console.log('Pipe data from python script ...'.data); + dataToSend = data.toString(); + }); + // in close event we are sure that stream from child process is closed + python.on('close', (code) => { + console.log(`child process close all stdio with code ${code}`); + // send data to browser + + res.status(200).json({ success: true, - msg: obj.response, + msg: dataToSend, question:question_ }); - //var sp_status=[]; - }); + }); + }); diff --git a/routes/processes/nishauri_chatbot.py b/routes/processes/nishauri_chatbot.py new file mode 100644 index 0000000..027455d --- /dev/null +++ b/routes/processes/nishauri_chatbot.py @@ -0,0 +1,21 @@ +# import system library +import sys + +# take passed variable values +question = sys.argv[1]; +#token = sys.argv[2]; + + +# calcluate the total using variable values and print the output +def chat_nishauri(question): + from gradio_client import Client; + client = Client("Nishauri/ChatBot", verbose=False); + result = client.predict( + question, # str in 'question' Textbox component + api_name="/predict" + ) + print(result); + #print('re'); + +# call the function and pass variables to it +chat_nishauri(question); \ No newline at end of file