-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
71 lines (57 loc) · 2.53 KB
/
gui.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
65
66
67
68
69
70
71
## This file contains code for connecting the model in the background to the GUI which is web interface (Anvil).
## Dependencies
# pip install transformers
# pip install anvil-uplink
# pip install datasets
import anvil.server
from transformers import pipeline
import torch
import torch.nn.functional as F
from torch.utils.data import Dataset
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# The key given here changes every session of the wen interface.
key = "L2AIJBPJKPLA6JQVXY67GMD2-A4M53V53WWH5NATO"
anvil.server.connect(key)
# @anvil.server.callable
# def predict_sentiment(input_text):
# save_directory = "/content/drive/MyDrive/DLNLP_Project/finetuned_models/distilbert-base-uncased-finetuned-sst-2-english-1"
# ft_model = AutoModelForSequenceClassification.from_pretrained(save_directory)
# ft_tokenizer = AutoTokenizer.from_pretrained(save_directory)
# classifier = pipeline("sentiment-analysis", model=ft_model, tokenizer=ft_tokenizer)
# result = classifier([input_text])
# sentiment = result[0]['label']
# score = result[0]['score']
# print(input_text)
# print(sentiment)
# #sentiment = "Positive"
# return sentiment, score
@anvil.server.callable
def predict_sentiment(input_text,selected_model):
if selected_model=="DistilBERT":
save_directory = "/content/drive/MyDrive/DLNLP_Project/finetuned_models/distilbert-base-uncased-finetuned-sst-2-english-1"
ft_model = AutoModelForSequenceClassification.from_pretrained(save_directory)
ft_tokenizer = AutoTokenizer.from_pretrained(save_directory)
classifier = pipeline("sentiment-analysis", model=ft_model, tokenizer=ft_tokenizer)
result = classifier([input_text])
sentiment = result[0]['label']
score = result[0]['score']
print(input_text)
print(sentiment)
return sentiment, score
elif selected_model=="mBERT":
save_directory = "/content/drive/MyDrive/DLNLP_Project/finetuned_models/mbert"
ft_model = AutoModelForSequenceClassification.from_pretrained(save_directory)
ft_tokenizer = AutoTokenizer.from_pretrained(save_directory)
classifier = pipeline("sentiment-analysis", model=ft_model, tokenizer=ft_tokenizer)
result = classifier([input_text])
star_value = result[0]['label']
score = result[0]['score']
if star_value=='1 star':
sentiment='NEGATIVE'
else:
sentiment='POSITIVE'
print(input_text)
print(sentiment)
return sentiment, score
# This line keeps the background process running and waiting for any input given from the GUI.
anvil.server.wait_forever()