exact_data2023/application/functions/sentiment.py

32 lines
806 B
Python
Raw Normal View History

2023-05-27 15:10:30 +02:00
from transformers import AutoTokenizer
from transformers import pipeline
model = 'application/models/sentiment_model'
tokenizer = AutoTokenizer.from_pretrained('application/tokenizers/sentiment_tokenizer')
# tokenizer = AutoTokenizer.from_pretrained("sdadas/polish-gpt2-small")
def sentiment_prediction(data):
pipe = pipeline('text-classification', model=model, tokenizer = tokenizer)
result = pipe(data)
2023-05-27 16:44:44 +02:00
return result
def count_predictions(predictions):
l0 = 0
l1 = 0
l2 = 0
all = {}
for i in predictions:
if i['label'] == 'LABEL_0':
l0 += 1
if i['label'] == 'LABEL_1':
l1 += 1
if i['label'] == 'LABEL_2':
l2 += 1
all['positive'] = l1
all['negative'] = l0
all['neutral'] = l2
return all