Exemplo n.º 1
0
def handle_data():
    if request.method == 'POST':
        return render_template("search.html")
    if request.method == 'GET':
        query = request.args.get('content')

        model = NLPModel()

        clf_path = 'lib/models/SentimentClassifier.pkl'
        with open(clf_path, 'rb') as f:
            model.clf = pickle.load(f)

        vec_path = 'lib/models/TFIDFVectorizer.pkl'
        with open(vec_path, 'rb') as f:
            model.vectorizer = pickle.load(f)

        user_query = query
        uq_vectorized = model.vectorizer_transform(np.array([user_query]))
        prediction = model.predict(uq_vectorized)
    # print(prediction)
        pred_proba = model.predict_proba(uq_vectorized)

        confidence = round(pred_proba[0], 3)
        print(prediction,confidence)

        if prediction == 0:
            filename = 'cry.jpg'
            return send_file(filename, mimetype='image/jpg')
        else:
            filename = 'smile.jpg'
            return send_file(filename, mimetype='image/jpg')
Exemplo n.º 2
0
Arquivo: app.py Projeto: zzsza/TIL
import pickle
import numpy as np
from model import NLPModel

app = Flask(__name__)
api = Api(app)

model = NLPModel()

clf_path = 'lib/models/SentimentClassifier.pkl'
with open(clf_path, 'rb') as f:
    model.clf = pickle.load(f)

vec_path = 'lib/models/TFIDFVectorizer.pkl'
with open(vec_path, 'rb') as f:
    model.vectorizer = pickle.load(f)

# argument parsing
parser = reqparse.RequestParser()
parser.add_argument('query')


@app.route('/')
def main():
    return "Main Page\nIf you use curl, using 'curl -X GET http://127.0.0.1:5000/prediction -d query='that movie was boring''"


class PredictSentiment(Resource):
    def get(self):
        # use parser and find the user's query
        args = parser.parse_args()