-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
158 lines (125 loc) · 5.4 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Import dependencies
import pandas as pd
import numpy as np
import os
import simplejson
from sqlalchemy.sql import select, column, text
from sqlalchemy.sql.expression import func
from flask import (Flask, render_template, jsonify, request, redirect, session)
from models import create_classes
from flask_sqlalchemy import SQLAlchemy
# NLP libraries
import re
import string
import unicodedata
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
nltk.download('stopwords')
stop_words_nltk = set(stopwords.words('english'))
from nltk.stem.porter import PorterStemmer
stemmer = PorterStemmer()
# Machine learning
from joblib import load
import pickle
import sklearn
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
# Setup the Flask application.
app = Flask(__name__)
# Set the secret key to some random bytes. https://flask.palletsprojects.com/en/1.1.x/quickstart/
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
"""
Database connection setup: Let's look for an environment variable 'DATABASE_URL'.
If there isn't one, we'll use a connection to a sqlite database.
"""
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', '') or "sqlite:///db.sqlite"
# Remove tracking modifications
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Use the `flask_sqlalchemy` library we'll create our variable `db` that is the connection to our database
db = SQLAlchemy(app)
"""
From `models.py` we call `create_classes` that we will have a reference to the class
we defined `Feedback` that is bound to the underlying database table.
"""
Cuisine, Feedback = create_classes(db)
# create route that renders index.html template
@app.route("/")
def home():
cuisine_list = ['African', 'American', 'British', 'Caribbean', 'Chinese',
'East European', 'French', 'Greek', 'Indian', 'Irish', 'Italian', 'Japanese',
'Korean', 'Mexican', 'Nordic', 'North African', 'Pakistani', 'Portuguese',
'South American', 'Spanish', 'Thai and South-East Asian', 'Turkish and Middle Eastern']
return render_template("index.html", cuisine_list=cuisine_list)
# Create a function to remove accented characters
def remove_accented_chars(matchobj):
text = matchobj.group()
new_text = unicodedata.normalize('NFKD', text).encode('ascii', 'ignore').decode('utf-8', 'ignore')
return new_text
# List of words to remove from ingredients' text
words_to_remove = [
"tbsp", "roughly", "chopped", "tsp", "finely", "oz", "plus", "optional",
"extra", "fresh", "freshly", "ground", "thinly", "sliced", "clove", "pint",
"cut", "kg", "lb", "cm", "ml", "mm", "small", "large", "medium", "diced", "slice",
"pinch", "peeled", "grated", "removed", "handful", "piece", "crushed", "red", "dried",
"drained", "rinsed", "halved", "trimmed", "deseeded", "x", "beaten", "available", "supermarket"]
# Create a function to clean ingredient text
def preprocess(text):
text = text.lower()
text = re.sub(r'\w*[\d¼½¾⅓⅔⅛⅜⅝]\w*', '', text)
text = text.translate(str.maketrans('', '', string.punctuation))
text = re.sub(r'[£×–‘’“”⁄]', '', text)
text = re.sub(r'[âãäçèéêîïñóôûüōưấớ]', remove_accented_chars, text)
words = word_tokenize(text)
words = [word for word in words if not word in stop_words_nltk]
words = [word for word in words if not word in words_to_remove]
words = [stemmer.stem(word) for word in words]
processed_text = ' '.join([word for word in words])
return processed_text
# Create route that requests the ingredients' text and return a predicted cuisine
@app.route("/predict", methods=["POST"])
def predict():
data = request.json
ingredient_text = data["ingredients"]
# create panda series from received data
try:
X_cleaned = pd.Series([preprocess(ingredient_text)])
except Exception as e:
print("Error Parsing Input Data")
print(e)
return "Error"
# Load the trained model
model = load("model/trained_model.joblib")
# Load the vectorized vocabulary
transformer = TfidfTransformer()
loaded_vec = CountVectorizer(
decode_error="replace",vocabulary=pickle.load(open("model/feature.pkl", "rb")))
X_transformed = transformer.fit_transform(loaded_vec.fit_transform(X_cleaned))
# convert nparray to list so that we can serialise as json
result = model.predict(X_transformed).tolist()
session["prediction"] = result[0]
return jsonify({"result": result})
# Create route that requests the feedback from customers
# and update to the database table 'feedback'
@app.route("/feedback", methods=["POST"])
def feedback():
feedback = request.json
ingredient_text = feedback["ingredientText"]
predicted_cuisine = session.pop('prediction', None)
actual_chosen_cuisine = feedback["chosenCuisine"]
actual_entered_cuisine = feedback["enteredCuisine"]
recipe_name = feedback["recipeName"]
recipe_link = feedback["recipeLink"]
feedback_data = Feedback(
ingredient_text=ingredient_text,
predicted_cuisine=predicted_cuisine,
actual_chosen_cuisine=actual_chosen_cuisine,
actual_entered_cuisine=actual_entered_cuisine,
recipe_name=recipe_name,
recipe_link=recipe_link)
db.session.add(feedback_data)
db.session.commit()
return jsonify({"loaded": True})
if __name__ == "__main__":
app.run(debug=True)