How to avoid loading a machine learning model every time?

Viewed 173

I am trying to do something very simple. Get a text from command line. Evaluate its sentiment based on a certain model and return the sentiment and its associated score to the the console. How do I avoid loading the model every time?


import argparse
import sys
from flair.models import TextClassifier
from flair.data import Sentence

model=None
def load_model():
    global model
    model = TextClassifier.load('en-sentiment')

def sentiment_score(s, threshold = 0.33):

    sentence = Sentence(s)
    model.predict(sentence)
    total_sentiment = sentence.labels[0]
    assert total_sentiment.value in ['POSITIVE', 'NEGATIVE']
    sign = 1 if total_sentiment.value == 'POSITIVE' else -1
    score = total_sentiment.score * sign

    if score > threshold:
        return ('POSITIVE', score)
    elif score < -threshold:
        return ('NEGATIVE', score)
    else:
        return ('NEUTRAL', score)

def get_input():
    try:
        parser = argparse.ArgumentParser()
        parser.add_argument("text", help="display the sentiment of a text.", type=str)
        args = parser.parse_args()
        return args.text

    except:
        e = sys.exc_info()[0]
        print(e)


if __name__ == "__main__":
    load_model()
    text = get_input()
    print(sentiment_score(text))
1 Answers

You could write a simple Flask app and create a basic HTML page (which would be you front end of the web app) and you already have your backend. The flask app will act as a bridge between your back end and the front end. It will take input from the user and pass it to the back end algorithm. After the results are predicted, it will take those results and pass them to the front end for displaying.

There are a lot of Flask deployment tutorials on Youtube.

Related