Flask API too slow.. only serving 20 requests per sec? [duplicate] - python

This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 3 years ago.
My Flask API which deploys keras model is too slow. It only serves 180 requests in 10 seconds.
The API has 3 .py files.
server_side.py:
def predict():
if request.method == "POST":
comment = request.form["comment"]
movies = [int(x) for x in comment.split(",")]
movies, g_input, t_input, movie_copy = prepare_inputs(
movies)
#print(movies[:10],g_input,t_input)
preds = make_prediction(movies, g_input, t_input, MODEL)
most_similar = preds[0].argsort()[-(10 + len(movie_copy)) :][::-1]
watched_movies, rec_movies = result(movie_copy, most_similar, DF)
context = {
"table1": [watched_movies.to_html(classes="data")],
"title1": watched_movies.columns.values,
"table2": [rec_movies.to_html(classes="data")],
"title2": rec_movies.columns.values,
}
return render_template("prediction.html", context=context)
if __name__ == "__main__":
global MODEL
MODEL = MODEL
app.run(threaded=False)
prediction_helper.py:
has two methods, one to predict and another to prepare inputs to the required length and shape.
def make_prediction(movies, g_input, t_input, model):
return model.predict(
[np.array([movies,]), np.array([g_input,]), np.array([t_input,])]
)
def prepare_inputs(movie_input):
movies_copy = movie_input[:]
genres = ' '.join(DF.genres[DF["movieId"].isin(movie_input)].values)
genres = genres.split(" ")
tags = ' '.join(DF.tag[DF["movieId"].isin(movie_input)].values)
tags = tags.split(" ")
genres = [
list(GENRES_MAP.keys())[list(GENRES_MAP.values()).index(i)] for i in genres
]
genres = list(set(genres))
genres = pad(genres, INPUT_LENGTH["genre_len"])
tags = [
list(TAG_MAP.keys())[list(TAG_MAP.values()).index(i)]
for i in tags
if i not in ("em", "cs", "se")
]
tags = list(set(tags))
if len(tags) > 100:
tags = tags[0:100]
else:
tags = pad(tags, INPUT_LENGTH["tag_len"])
movie_input = pad(movie_input, INPUT_LENGTH["movie_len"])
return movie_input, genres, tags, movies_copy
data_loader.py:
has two methods, pad which pads a list to the required length and another which extracts rows based on the prediction indices.
import pickle
import pandas as pd
from keras.models import load_model
MODEL = load_model("final_train1.h5")
DF = pd.read_csv("new_df.csv")
DF["tag"] = DF["tag"].fillna("")
PICKLE_IN = open("genres_map.pickle", "rb")
GENRES_MAP = pickle.load(PICKLE_IN)
PICKLE_IN = open("tag_map.pickle", "rb")
TAG_MAP = pickle.load(PICKLE_IN)
INPUT_LENGTH = {"movie_len":2698, "genre_len":24, "tag_len":100}
def pad(lst, width):
lst.extend([0] * (width - len(lst)))
return lst
def result(movie_copy, most_similar, DF):
rec_movies = DF.set_index("movieId").loc[most_similar]
watched_movies = DF.set_index("movieId").loc[movie_copy]
return watched_movies, rec_movies

You shouldn't use flask built-in server that just designed for development purpose and not for production deployment. Instead, you should use an external server for production deployment like Waitress, uWSGI, Gunicorn or Nginx. You can read here for the options for flask production deployment: https://flask.palletsprojects.com/en/1.1.x/deploying/

Flask documentation says the development server is not for production. Use a production WSGI server such as Gunicorn along with Nginx.

Related

openai.error.InvalidRequestError: Engine not found

Tried accessing the OpenAPI example - Explain code
But it shows error as -
InvalidRequestError: Engine not found
enter code response = openai.Completion.create(
engine="code-davinci-002",
prompt="class Log:\n def __init__(self, path):\n dirname = os.path.dirname(path)\n os.makedirs(dirname, exist_ok=True)\n f = open(path, \"a+\")\n\n # Check that the file is newline-terminated\n size = os.path.getsize(path)\n if size > 0:\n f.seek(size - 1)\n end = f.read(1)\n if end != \"\\n\":\n f.write(\"\\n\")\n self.f = f\n self.path = path\n\n def log(self, event):\n event[\"_event_id\"] = str(uuid.uuid4())\n json.dump(event, self.f)\n self.f.write(\"\\n\")\n\n def state(self):\n state = {\"complete\": set(), \"last\": None}\n for line in open(self.path):\n event = json.loads(line)\n if event[\"type\"] == \"submit\" and event[\"success\"]:\n state[\"complete\"].add(event[\"id\"])\n state[\"last\"] = event\n return state\n\n\"\"\"\nHere's what the above class is doing:\n1.",
temperature=0,
max_tokens=64,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=["\"\"\""]
)
I've been trying to access the engine named code-davinci-002 which is a private beta version engine. So without access it's not possible to access the engine. It seems only the GPT-3 models are of public usage. We need to need to join the OpenAI Codex Private Beta Waitlist in order to access Codex models through API.
Please note that your code is not very readable.
However, from the given error, I think it has to do with the missing colon : in the engine name.
Change this line from:
engine="code-davinci-002",
to
engine="code-davinci:002",
If you are using a finetuned model instead of an engine, you'd want to use model= instead of engine=.
response = openai.Completion.create(
model="<finetuned model>",
prompt=

Heroku 30s timeout error, how to make Django views variable creation as a background task (worker)?

I am facing a heroku 30s time out error with my django webapp.
The reason it is taking long is because of the views variables created (reddit api onto context)
Here is the code for the same.
def home(request):
reddit = praw.Reddit(client_id='myclientid', client_secret='mysecretcode',
user_agent='user agent name')
hot_posts = reddit.subreddit('AskReddit').top(time_filter="day", limit=7)
x = []
y = []
for post in hot_posts:
x.append(post.title)
y.append(post.url)
print(x)
print(y)
z = []
for url in y:
comments = []
submission = reddit.submission(url=url)
submission.comments.replace_more(limit=0)
for count in range(10):
comments.append(submission.comments[count].body)
z.append(comments)
top_EarthPorn = reddit.subreddit('EarthPorn').top(limit=100)
EarthPorn_links = []
for post in top_EarthPorn:
EarthPorn_links.append(post.url)
request.session['EarthPorn_links'] = EarthPorn_links
return render(request, template_name='base.html', context=context)
How do i make sure the context dict data is being created every hour or so as a background process? which libraries can one use to achieve so
I think this should work:
Put this at the end of your settings.py file:
SESSION_EXPIRE_SECONDS = 1500 # 1500 seconds = 25 minutes
So the session will expire after 25 minutes.

how to read information about ontology using owlready2

I am working in flask app and want to load ontology and print how many classes and how many individuals in this ontology
here is my code, it does not work
import flask
from owlready2 import *
app = flask.Flask(__name__)
app.config["DEBUG"] = True
#app.route('/', methods=['GET'])
def start():
onto_path.append("pizza.owl")
onto = get_ontology("pizza.owl")
onto.load()
print(onto.classes())
print(list(onto.individuals()))
html = ''
html += '<h2>clases: ' + onto.classes() + '</br>'
html += '<h3>individuals: ' + onto.individuals()
return html
#return "<h1>Distant Reading Archive</h1><p>This site is a prototype API for distant reading of science fiction novels.</p>"
app.run()
The method classes() and individuals() returns a generator, so you should cast the generator into a list, and ask for the length of that object.
n_classes = len(list(onto.classes()))
n_individuals = len(list(onto.individuals()))
After that, you should have the numbers on your variables and you can concatenate them with your HTML.

Why flask application is taking so long to run?

This is my home page and it takes 30 seconds to run. There are lot of graphs, wordcloud based on the dataset that have approx 1000 articles and some basic operation on sqlalchemy. But still it should not take that much time. How can I reduce the time?
#app.route('/home',methods=["get","post"])
def showjson():
folder = 'C:/Users/Mansi Dhingra/Desktop/Projects/api/news/static/images'
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
os.remove(file_path)
news_df = pd.read_csv('news_information1.csv')
news_df.to_sql('users', con=engine)
topic_l = engine.execute('''Select distinct Topic from users''').fetchall()
topic_list=[]
for tr in topic_l:
topic_list.append(tr[0])
search = request.form.get("search")
source_l=engine.execute('''Select distinct source from users''').fetchall()
source_list = []
for tr in source_l:
source_list.append(tr[0])
bank_l = engine.execute('''Select distinct bank from users''').fetchall()
bank_list = []
for tr in bank_l:
bank_list.append(tr[0])
end_date = engine.execute('''Select max(date) from users''').fetchall()
max_date=end_date[0][0]
sent_count = engine.execute('''Select Sentiment,Count(*) from users group by Sentiment''').fetchall()
sent_topic = []
sent_count1 = []
for tx in sent_count:
sent_topic.append(tx[0])
sent_count1.append(tx[1])
fig_sent=create_graphs(sent_topic,sent_count1,"sentiment")
list_words = fetch_sentiment_using_vader(news_df['clean_text'])
stopwords = stopwords_for_wordcount(news_df['clean_text'])
count_vectorizer = CountVectorizer(stop_words=stopwords[0])
fig_pos=plot_words(list_words[0], list_words[2], "positive")
fig_neg=plot_words(list_words[1], list_words[2], "negative")
fig_cat=count_category(news_df)
fig_pub=count_pub(news_df)
create_wordcloud( stopwords)
fig_tri=bigram_or_trigram(news_df['clean_text'], stopwords,"bigram")
images_list = os.listdir(os.path.join(app.static_folder, "images"))
return render_template('news_home.html',fig_pub=fig_pub,topic_list=topic_list,img=images_list,plt_pos=fig_pos,plt_tri=fig_tri,plt_neg=fig_neg,
bank_list=bank_list,source_list=source_list,max_date=max_date,fig_cat=fig_cat,fig_sent=fig_sent,search=search)
If you're using older than 1.0 version of Flask, then enable multi-threading. e.g. app.run(threaded=True). Otherwise, if you have version newer than 1.0 then you can use Flask's default WSGI toolkit Werkzeug profiler to find expensive functions for each request.
from werkzeug.middleware.profiler import ProfilerMiddleware
app = ProfilerMiddleware(app, restrictions=[20])
Doing above will show the 20 most expensive functions for each request. Inspect them and modify/refactor yourself.
Just a tip if you're considering taking it to production, default Flask isn't production-ready. There are a few additional steps to take a flask app to production. Some guidance here in the official Flask documentation: Deploy to Production. Gunicorn with NGINX is a very good option.

how to edit shopify fulfillment using python api

how to add tracking number and tracking url in existing shopify fulfillment using shopify python api.
code:
token = <token>
session = shopify.Session("<shop-name>.myshopify.com", token)
shopify.ShopifyResource.activate_session(session)
a = shopify.Fulfillment.find(2642971265,order_id = 3386372225)
a.tracking_url = "www.example.com/trackingid=123"
a.tracking_number = "1234"
shopify.Fulfillment.save(a)
after that it return true but changes are not reflecting anywhere
I forgot to add the other details i.e tracking_urls and tracking_numbers
a = shopify.Fulfillment.find(order_id = 3386116225)[0]
a.tracking_company = "pickrr"
a.tracking_number = '12345678'
l = []
l.append('12345678')
a.tracking_numbers = l
a.tracking_url = "pickrr.com/12345678"
l = []
l.append("pickrr.com/12345678")
a.tracking_urls = l
shopify.Fulfillment.save(a)
Now everything working fine

Categories

Resources