Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi Patrick I have used node js as a backend for the project #10

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
# Chatbot Deployment with Flask and JavaScript

In this tutorial we deploy the chatbot I created in [this](https://github.com/python-engineer/pytorch-chatbot) tutorial with Flask and JavaScript.

This gives 2 deployment options:
- Deploy within Flask app with jinja2 template
- Serve only the Flask prediction API. The used html and javascript files can be included in any Frontend application (with only a slight modification) and can run completely separate from the Flask App then.

## Initial Setup:
This repo currently contains the starter files.
# Chatbot Deployment with Python and JavaScript

Clone repo and create a virtual environment
```
Expand Down Expand Up @@ -38,15 +29,14 @@ the following command to test it in the console.
$ (venv) python chat.py
```

Now for deployment follow my tutorial to implement `app.py` and `app.js`.
## Node setup:

for this node must be installed in the system

$ npm init

## Watch the Tutorial
[![Alt text](https://img.youtube.com/vi/a37BL0stIuM/hqdefault.jpg)](https://youtu.be/a37BL0stIuM)
[https://youtu.be/a37BL0stIuM](https://youtu.be/a37BL0stIuM)
$ npm install express cors ejs child_process

## Note
In the video we implement the first approach using jinja2 templates within our Flask app. Only slight modifications are needed to run the frontend separately. I put the final frontend code for a standalone frontend application in the [standalone-frontend](/standalone-frontend) folder.

## Credits:
This repo was used for the frontend code:
https://github.com/hitchcliff/front-end-chatjs
to run server use command
$ node server.js
12 changes: 12 additions & 0 deletions get_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from chat import get_response
import sys

# Access command-line arguments
args = sys.argv[1:]

# Check if any arguments were provided
if len(args) > 0:
process_var = args[0]
print(get_response(args[0]))
else:
print("No command-line argument provided.")
89 changes: 89 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const { exec } = require("child_process");
const express = require("express");
const cors = require('cors')
const app = express();


// vvimp
const corsOpts = {
origin: '*',

methods: [
'GET',
'POST',
],

allowedHeaders: [
'Content-Type',
],
};

app.use(cors(corsOpts));

// Define the path to your Python script
const pythonScriptPath = "get_response.py";

// Create a function to execute the command and process the output
const executeScript = async (argument) => {
try {
const { stdout, stderr } = await new Promise((resolve, reject) => {
exec(
`python ${pythonScriptPath} ${argument}`,
(error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve({ stdout, stderr });
}
}
);
});

// Process the output
const output = stdout.trim();

// Use the output outside the exec function
console.log("This is the output:", output);

return output;
} catch (error) {
console.error("Error executing Python script:", error);

return "error has occured...";
}
};

// Call the function t`o execute the script pass the argument
executeScript("hi");

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

app.use(express.json());

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

app.post("/request", async (req, res) => {
console.log(req);

// Access the message from the request body
const message = req.body.message;

// Process the message (example: convert to uppercase)
const answer = await executeScript(message);

// Create the response object
const response = {
answer: answer
};

// Send the response as JSON
res.json(response);


});

app.listen(3000, () => {
console.log("server is listening on port 3000");
});
4 changes: 2 additions & 2 deletions standalone-frontend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Chatbox {
let msg1 = { name: "User", message: text1 }
this.messages.push(msg1);

fetch('http://127.0.0.1:5000/predict', {
fetch('http://localhost:3000/request', {
method: 'POST',
body: JSON.stringify({ message: text1 }),
mode: 'cors',
Expand Down Expand Up @@ -88,4 +88,4 @@ class Chatbox {


const chatbox = new Chatbox();
chatbox.display();
chatbox.display();
56 changes: 30 additions & 26 deletions standalone-frontend/base.html
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="style.css" />

<head>
<meta charset="UTF-8">
<head>
<meta charset="UTF-8" />
<title>Chatbot</title>
</head>
<body>
<div class="container">
<div class="chatbox">
</head>
<body>
<div class="container">
<div class="chatbox">
<div class="chatbox__support">
<div class="chatbox__header">
<div class="chatbox__image--header">
<img src="https://img.icons8.com/color/48/000000/circled-user-female-skin-type-5--v1.png" alt="image">
</div>
<div class="chatbox__content--header">
<h4 class="chatbox__heading--header">Chat support</h4>
<p class="chatbox__description--header">Hi. My name is Sam. How can I help you?</p>
</div>
<div class="chatbox__header">
<div class="chatbox__image--header">
<img
src="https://img.icons8.com/color/48/000000/circled-user-female-skin-type-5--v1.png"
alt="image"
/>
</div>
<div class="chatbox__messages">
<div></div>
</div>
<div class="chatbox__footer">
<input type="text" placeholder="Write a message...">
<button class="chatbox__send--footer send__button">Send</button>
<div class="chatbox__content--header">
<h4 class="chatbox__heading--header">Chat support</h4>
<p class="chatbox__description--header">
Hi. My name is Bot. How can I help you?
</p>
</div>
</div>
<div class="chatbox__messages">
<div></div>
</div>
<div class="chatbox__footer">
<input type="text" placeholder="Write a message..." />
<button class="chatbox__send--footer send__button">Send</button>
</div>
</div>
<div class="chatbox__button">
<button><img src="./images/chatbox-icon.svg" /></button>
<button><img src="./images/chatbox-icon.svg" /></button>
</div>
</div>
</div>
</div>

<script src="./app.js"></script>

</body>
</html>
</body>
</html>