check why im not getting a reply from my chatbot app ##app.py Create flask app.py from…
- User
check why im not getting a reply from my chatbot app ##app.py
Create flask app.py
from flask import Flask, render_template, request, jsonify import random import json import pickle import numpy as np import nltk nltk.download('punkt') nltk.download('wordnet') from nltk.tokenize import word_tokenize from nltk.stem import WordNetLemmatizer import tensorflow as tf
app = Flask(name, static_folder='static')
Load the model, words, classes, and intents
model = tf.keras.models.load_model("E:\Projects\chatbot\models\new_chat_model.h5") data = pickle.load(open("E:\Projects\chatbot\models\chat_data.pkl", "rb")) words = data["words"] classes = data["classes"] intents = json.load(open("E:\Projects\chatbot\data\data.json",'r'))["intents"]
def bag_of_words(sentence): bag = [0] * len(words) lemmatizer = WordNetLemmatizer() sentence_words = word_tokenize(sentence) sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
codefor word in sentence_words: if word.lower() in words: bag[words.index(word.lower())] = 1 return np.array(bag)def chatbot_response(message): results = model.predict(np.array([bag_of_words(message)])) results_index = np.argmax(results) tag = classes[results_index]
codefor intent in intents: if intent["tag"] == tag: response = random.choice(intent["responses"]) return response@app.route("/") def index(): return render_template("index.html")
@app.route("/get") def get_bot_response(): user_text = request.args.get("message") response = chatbot_response(user_text) return jsonify({"response": response})
if name == "main": app.run(debug=True)
##index.html
##script.js document.addEventListener("DOMContentLoaded", function(event) { // Selecting DOM elements const chatForm = document.getElementById("chat-form"); const chatInput = document.getElementById("user-input"); const chatbotMessages = document.getElementById("chatbot-messages"); const sendBtn = document.getElementById("send-btn");
code//Event listener for the chat form submitchatForm.addEventListener("submit", (event) => { event.preventDefault(); const userInput = chatInput.value; addUserMessage(userInput); sendUserMessage(userInput); chatInput.value = ""; scrollToBottom(); });
code - Assistant
I see an issue in your
index.htmlwhere the AJAX request is not using the correct query parameter.In the
get_bot_response()function inapp.py, you are trying to get the user's input usinguser_text = request.args.get("message").But in
index.html, when making the AJAX request, you are using the query parametermsglike this:$.get("/get", {msg: message}).You should change this line in
index.htmlto:javascript$.get("/get", {message: message}).done(function(data) {Additionally, in the response, you return a JSON object with a key "response". So, in the JavaScript code, you should properly extract the value. Change this line in
index.htmlas well:javascript$("#chat-area").append('<div class="bot-msg"><span class="msg">' + data.response + '</span></div>');After making these changes, you should be able to get a reply from your chatbot app.
- User
- Assistant
I have modified the
index.htmlcode to fix the issue with the chatbot not getting a reply. The changes include fixing the query parameter and extracting the response properly from the returned JSON object.Here's the modified
index.htmlfile:html