-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredict.py
executable file
·64 lines (46 loc) · 2.38 KB
/
predict.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
#!/usr/bin/env python3
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
import numpy as np
import argparse
import os
import sys
# disable parallelism warning
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# get the directory where the model was saved to
model = AutoModelForSequenceClassification.from_pretrained('./custom_model/')
# load the tokenizer by pointing to the same directory as the pretrained model
tokenizer = AutoTokenizer.from_pretrained('./custom_model/')
#---------------------------------------------------------------------------------------------------
def get_args():
"""
Get command line arguments
"""
parser = argparse.ArgumentParser(description="Classify students emails based on mail content",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('email_text',
type=str,
help='Enter the content of the email you want to classify in enclosed quotes')
args = parser.parse_args()
return args
#-------------------------------------------------------------------------------------------------------
def main():
args = get_args()
text = args.email_text
# check that the required argument contains relevant words for better prediction
if ('email' in text) | ('e-mail' in text) | ("mail" in text) | ('mailed' in text) \
| ('share' in text) | ('shared' in text) | ("sharing" in text):
classifier = pipeline(task='text-classification', model=model, tokenizer=tokenizer)
predicted = classifier(text)
predicted_label = predicted[0]['label']
print(' The mail to classify is: \n', ''.join(text), '\n --------------------------------------\
------------------------------------------------')
show_pred_class = np.where(predicted_label == 'LABEL_1',
'Student wants to know if can share',
'Student has shared')
print('\n The mail is classified as: \n', show_pred_class)
else:
print("please ensure your email body contains at least one of the following keywords: \
\n email \n e-mail \n share \n shared \n sharing")
# ----------------------------------------------------------------------------------------------------------
if __name__ == '__main__':
main()