Serverless Examples – A collection of boilerplates and examples of serverless architectures built with the Serverless Framework on AWS Lambda, Microsoft Azure, Google Cloud Functions, and more.
Description:
I'm encountering an issue with my Flask application where I'm unable to retrieve a response from the bot when sending a query through the index.html page. Here are the details:
Problem:
When I enter a query in the input field on the index.html page and click "Send," the request is made to the "/get" endpoint in my Flask application, but I'm not receiving any response from the bot.
Steps to reproduce:
Open the index.html page in a browser.
Enter a query in the input field.
Click "Send."
Check the browser console for any errors.
Error messages:
Failed to load resource: net::ERR_NAME_NOT_RESOLVED
Failed to load resource: the server responded with a status of 403 (Forbidden)Expected behavior:
After sending a query, I should receive a response from the bot displayed in the chat area on the index.html page.
Environment details:
Operating system: Windows
Browser: Google
Python version: 3.12
Here are the main project files:
app.py
import constants
import nltk
import ssl
import tempfile
import random
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import string
import sys
import warnings
import requests
import awsgi
import json
from flask import Flask, render_template,jsonify
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
# Use a temporary directory for NLTK data
temp_dir = tempfile.mkdtemp()
nltk.data.path.append(temp_dir)
# Download necessary NLTK resources
nltk.download('punkt', download_dir=temp_dir)
def get_formalities_reply(formality):
if any(remove_punctuation_marks(formality).lower() in remove_punctuation_marks(greet).lower() for greet in constants.GREETING_INPUTS):
return random.choice(constants.GREETING_REPLIES)
elif any(remove_punctuation_marks(formality).lower() in remove_punctuation_marks(thanks).lower() for thanks in constants.THANKS_INPUTS):
return random.choice(constants.THANKS_REPLIES)
def get_lemmatized_tokens(text):
normalized_tokens = nltk.word_tokenize(remove_punctuation_marks(text.lower()))
return [nltk.stem.WordNetLemmatizer().lemmatize(normalized_token) for normalized_token in normalized_tokens]
corpus = open('corpus.txt', 'r', errors='ignore').read().lower()
documents = nltk.sent_tokenize(corpus)
def get_query_reply(query):
documents.append(query)
tfidf_results = TfidfVectorizer(tokenizer=get_lemmatized_tokens, stop_words='english').fit_transform(documents)
cosine_similarity_results = cosine_similarity(tfidf_results[-1], tfidf_results).flatten()
best_index = cosine_similarity_results.argsort()[-2]
documents.remove(query)
if cosine_similarity_results[best_index] == 0:
return "I am sorry! I don't understand you..."
else:
return documents[best_index]
def remove_punctuation_marks(text):
punctuation_marks = dict((ord(punctuation_mark), None) for punctuation_mark in string.punctuation)
return text.translate(punctuation_marks)
app = Flask(__name__)
#app.static_folder = 'static'
def get_headers():
return {
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
}
@app.route("/")
def home():
print("Attempting to render index.html") # Debugging statement
return render_template("index.html")
#return "<!doctype html>\n<html lang=en>\n<title>Test TIle</title>\n<h1>Test heading</h1>\n<p>Test message</p>\n"
@app.route("/get")
def get_bot_response():
print("Inside get_bot_response function") # Add this line
user_text = request.args.get('msg')
print("Received message:", user_text) # Print the user's message
bot_response = get_query_reply(user_text)
print("Bot's response:", bot_response) # Print the bot's response
headers = get_headers() # Get the headers
return jsonify(bot_response), 200, headers
def lambda_handler(event, context):
print("Event is: ",event)
print("Context is: ",context)
if event == {}:
#event = {}
#event['httpMethod'] = 'GET'
#event['path'] = '/'
#event['queryStringParameters'] = {}
#event_data=json.dumps(event)
#print("Event_data is: ",event_data)
#event = {'httpMethod':'GET','path':'/','queryStringParameters':{}}
print("Event now is: ",event)
print("App is: ",app)
return awsgi.response(app, event, context)
headers = get_headers()
response['headers'] = get_headers() # Add headers to the response
return response
else:
event = {'httpMethod':'GET','path':'/','queryStringParameters':{}}
return awsgi.response(app, event, context)
headers = get_headers()
response['headers'] = get_headers() # Add headers to the response
return response
It works fine on SAM remote invoke, but when I entered the URL provided by sam deploy --guided after building it using sam build to get the page displayed on the browser, the page displays the user-entered message appropriately. However, there is no response. I get the following error:
Sam-cli commands:
sam build
sam deploy --guided
sam remote invoke
I would appreciate any assistance in resolving this issue. Thank you!
Description: I'm encountering an issue with my Flask application where I'm unable to retrieve a response from the bot when sending a query through the index.html page. Here are the details:
Problem: When I enter a query in the input field on the index.html page and click "Send," the request is made to the "/get" endpoint in my Flask application, but I'm not receiving any response from the bot.
Steps to reproduce: Open the index.html page in a browser. Enter a query in the input field. Click "Send." Check the browser console for any errors.
Error messages: Failed to load resource: net::ERR_NAME_NOT_RESOLVED Failed to load resource: the server responded with a status of 403 (Forbidden)Expected behavior: After sending a query, I should receive a response from the bot displayed in the chat area on the index.html page.
Environment details: Operating system: Windows Browser: Google Python version: 3.12
Here are the main project files: app.py
Main file index.html :
It works fine on SAM remote invoke, but when I entered the URL provided by sam deploy --guided after building it using sam build to get the page displayed on the browser, the page displays the user-entered message appropriately. However, there is no response. I get the following error: Sam-cli commands: sam build sam deploy --guided sam remote invoke
I would appreciate any assistance in resolving this issue. Thank you!