Reputation: 35
My chatbot is working fine on my local system, but I am getting an error when I deploy it on Heroku.
2021-05-10T08:22:26.384671+00:00 heroku[router]: at=info method=POST path="/get" host=ihaci-
chat.herokuapp.com request_id=33f6310b-fc0f-4dfb-be87-aaedaca496df fwd="203.189.245.148" dyno=web.1
connect=0ms service=7ms status=500 bytes=463 protocol=https
2021-05-10T08:22:26.382886+00:00 app[web.1]: [2021-05-10 08:22:26,382] ERROR in app: Exception on
/get [POST]
2021-05-10T08:22:26.382924+00:00 app[web.1]: Traceback (most recent call last):
2021-05-10T08:22:26.382925+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-
packages/flask/app.py", line 2447, in wsgi_app
2021-05-10T08:22:26.382928+00:00 app[web.1]: response = self.full_dispatch_request()
2021-05-10T08:22:26.382929+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-
packages/flask/app.py", line 1952, in full_dispatch_request
2021-05-10T08:22:26.382929+00:00 app[web.1]: rv = self.handle_user_exception(e)
2021-05-10T08:22:26.382930+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-
packages/flask/app.py", line 1821, in handle_user_exception
2021-05-10T08:22:26.382930+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2021-05-10T08:22:26.382930+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-
packages/flask/_compat.py", line 39, in reraise
2021-05-10T08:22:26.382931+00:00 app[web.1]: raise value
2021-05-10T08:22:26.382931+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-
packages/flask/app.py", line 1950, in full_dispatch_request
2021-05-10T08:22:26.382932+00:00 app[web.1]: rv = self.dispatch_request()
2021-05-10T08:22:26.382932+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-
packages/flask/app.py", line 1936, in dispatch_request
2021-05-10T08:22:26.382933+00:00 app[web.1]: return self.view_functions[rule.endpoint]
(**req.view_args)
2021-05-10T08:22:26.382933+00:00 app[web.1]: File "/app/app.py", line 55, in chatbot_response
2021-05-10T08:22:26.382934+00:00 app[web.1]: ints = predict_class(msg, model)
2021-05-10T08:22:26.382934+00:00 app[web.1]: File "/app/app.py", line 86, in predict_class
2021-05-10T08:22:26.382934+00:00 app[web.1]: p = bow(sentence, words, show_details=False)
2021-05-10T08:22:26.382935+00:00 app[web.1]: File "/app/app.py", line 71, in bow
2021-05-10T08:22:26.382935+00:00 app[web.1]: sentence_words = clean_up_sentence(sentence)
2021-05-10T08:22:26.382935+00:00 app[web.1]: File "/app/app.py", line 63, in clean_up_sentence
2021-05-10T08:22:26.382936+00:00 app[web.1]: sentence_words = nltk.word_tokenize(sentence)
2021-05-10T08:22:26.382936+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-
packages/nltk/tokenize/__init__.py", line 129, in word_tokenize
2021-05-10T08:22:26.382937+00:00 app[web.1]: sentences = [text] if preserve_line else
sent_tokenize(text, language)
2021-05-10T08:22:26.382937+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-
packages/nltk/tokenize/__init__.py", line 106, in sent_tokenize
2021-05-10T08:22:26.382938+00:00 app[web.1]: tokenizer =
load("tokenizers/punkt/{0}.pickle".format(language))
2021-05-10T08:22:26.382938+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-
packages/nltk/data.py", line 752, in load
2021-05-10T08:22:26.382939+00:00 app[web.1]: opened_resource = _open(resource_url)
2021-05-10T08:22:26.382939+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-
packages/nltk/data.py", line 877, in _open
2021-05-10T08:22:26.382939+00:00 app[web.1]: return find(path_, path + [""]).open()
2021-05-10T08:22:26.382940+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-
packages/nltk/data.py", line 585, in find
2021-05-10T08:22:26.382940+00:00 app[web.1]: raise LookupError(resource_not_found)
2021-05-10T08:22:26.382940+00:00 app[web.1]: LookupError:
2021-05-10T08:22:26.382941+00:00 app[web.1]:
**********************************************************************
2021-05-10T08:22:26.382941+00:00 app[web.1]: Resource [93mpunkt[0m not found.
2021-05-10T08:22:26.382942+00:00 app[web.1]: Please use the NLTK Downloader to obtain the resource:
2021-05-10T08:22:26.382942+00:00 app[web.1]:
2021-05-10T08:22:26.382942+00:00 app[web.1]: [31m>>> import nltk
2021-05-10T08:22:26.382943+00:00 app[web.1]: >>> nltk.download('punkt')
2021-05-10T08:22:26.382943+00:00 app[web.1]: [0m
2021-05-10T08:22:26.382944+00:00 app[web.1]: For more information see:
https://www.nltk.org/data.html
2021-05-10T08:22:26.382944+00:00 app[web.1]:
2021-05-10T08:22:26.382945+00:00 app[web.1]: Attempted to load
[93mtokenizers/punkt/PY3/english.pickle[0m
2021-05-10T08:22:26.382945+00:00 app[web.1]:
2021-05-10T08:22:26.382945+00:00 app[web.1]: Searched in:
2021-05-10T08:22:26.382945+00:00 app[web.1]: - '/app/nltk_data'
2021-05-10T08:22:26.382946+00:00 app[web.1]: - '/app/.heroku/python/nltk_data'
2021-05-10T08:22:26.382946+00:00 app[web.1]: - '/app/.heroku/python/share/nltk_data'
2021-05-10T08:22:26.382947+00:00 app[web.1]: - '/app/.heroku/python/lib/nltk_data'
2021-05-10T08:22:26.382947+00:00 app[web.1]: - '/usr/share/nltk_data'
2021-05-10T08:22:26.382947+00:00 app[web.1]: - '/usr/local/share/nltk_data'
2021-05-10T08:22:26.382948+00:00 app[web.1]: - '/usr/lib/nltk_data'
2021-05-10T08:22:26.382948+00:00 app[web.1]: - '/usr/local/lib/nltk_data'
2021-05-10T08:22:26.382948+00:00 app[web.1]: - ''
2021-05-10T08:22:26.382949+00:00 app[web.1]:
**********************************************************************
2021-05-10T08:22:26.382949+00:00 app[web.1]:
2021-05-10T08:22:26.384684+00:00 app[web.1]: 10.31.72.133 - - [10/May/2021:08:22:26 +0000] "POST /get
HTTP/1.1" 500 290 "https://ihaci-chat.herokuapp.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.56"
app.py
:
# libraries
import random
import numpy as np
import pickle
import json
from flask import Flask, render_template, request
from flask_ngrok import run_with_ngrok
import nltk
from keras.models import load_model
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
# chat initialization
model = load_model("chatbot_model.h5")
intents = json.loads(open('intents.json',encoding="utf8").read())
words = pickle.load(open("words.pkl", "rb"))
classes = pickle.load(open("classes.pkl", "rb"))
app = Flask(__name__)
run_with_ngrok(app)
@app.route("/")
def home():
return render_template("index.html")
@app.route("/get", methods=["POST"])
def chatbot_response():
msg = request.form["msg"]
#checks is a user has given a name, in order to give a personalized feedback
if msg.startswith('my name is'):
name = msg[11:]
ints = predict_class(msg, model)
res1 = getResponse(ints, intents)
res =res1.replace("{n}",name)
elif msg.startswith('hi my name is'):
name = msg[14:]
ints = predict_class(msg, model)
res1 = getResponse(ints, intents)
res =res1.replace("{n}",name)
#if no name is passed execute normally
else:
ints = predict_class(msg, model)
res = getResponse(ints, intents)
return res
# chat functionalities
def clean_up_sentence(sentence):
sentence_words = nltk.word_tokenize(sentence)
sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
return sentence_words
# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence
def bow(sentence, words, show_details=True):
# tokenize the pattern
sentence_words = clean_up_sentence(sentence)
# bag of words - matrix of N words, vocabulary matrix
bag = [0] * len(words)
for s in sentence_words:
for i, w in enumerate(words):
if w == s:
# assign 1 if current word is in the vocabulary position
bag[i] = 1
if show_details:
print("found in bag: %s" % w)
return np.array(bag)
def predict_class(sentence, model):
# filter out predictions below a threshold
p = bow(sentence, words, show_details=False)
res = model.predict(np.array([p]))[0]
ERROR_THRESHOLD = 0.25
results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
# sort by strength of probability
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
return return_list
def getResponse(ints, intents_json):
tag = ints[0]["intent"]
list_of_intents = intents_json["intents"]
for i in list_of_intents:
if i["tag"] == tag:
result = random.choice(i["responses"])
break
return result
if __name__ == "__main__":
app.run()
Can anyone help me to understand what this error exactly is? What should I do to solve this specific error?
Upvotes: 0
Views: 262
Reputation: 2710
Your Error is clear in its nature from Heroku logs, your chat bot is working fine in local system because your chat bot is using NLTK, and NLTK needs a resource which is already downloaded in your local system. The resource is however not found in the Heroku server itself.
Write the following script in a temp file and execute it once per deployment to make your chat bot run smoothly on Heroku. Also you can have a check of existence of this resource in your chat bot itself to download it automatically if it not found!
import nltk
nltk.download('punkt')
Upvotes: 3