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))