Spaces:
Runtime error
Runtime error
Jeffrey Rathgeber Jr
commited on
milestone3update
Browse files
app.py
CHANGED
@@ -6,18 +6,86 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
6 |
import torch
|
7 |
import torch.nn.functional as F
|
8 |
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
|
23 |
#------------------------------------------------------------------------
|
@@ -45,28 +113,4 @@ st.write('You selected:', option)
|
|
45 |
# tokenizer = AutoTokenizer.from_pretrained(save_directory)
|
46 |
# model = AutoModelForSequenceClassification.from_pretrained(save_directory)
|
47 |
|
48 |
-
#------------------------------------------------------------------------
|
49 |
-
|
50 |
-
if option == 'Pipeline':
|
51 |
-
# pipeline
|
52 |
-
preds = classifier(textIn)
|
53 |
-
preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds]
|
54 |
-
st.write('According to Pipeline, input text is ', preds[0]['label'], ' with a confidence of ', preds[0]['score'])
|
55 |
-
|
56 |
-
if option == 'TextBlob':
|
57 |
-
# textblob
|
58 |
-
polarity = TextBlob(textIn).sentiment.polarity
|
59 |
-
subjectivity = TextBlob(textIn).sentiment.subjectivity
|
60 |
-
sentiment = ''
|
61 |
-
if polarity < 0:
|
62 |
-
sentiment = 'Negative'
|
63 |
-
elif polarity == 0:
|
64 |
-
sentiment = 'Neutral'
|
65 |
-
else:
|
66 |
-
sentiment = 'Positive'
|
67 |
-
|
68 |
-
st.write('According to TextBlob, input text is ', sentiment, ' and a subjectivity score (from 0 being objective to 1 being subjective) of ', subjectivity)
|
69 |
-
|
70 |
-
|
71 |
-
if option == 'MILESTONE 3: FINE-TUNED':
|
72 |
-
...
|
|
|
6 |
import torch
|
7 |
import torch.nn.functional as F
|
8 |
|
9 |
+
textIn = st.text_input("Input Text Here:", "I really like the color of your car!")
|
10 |
|
11 |
+
# option = st.selectbox('Which pre-trained model would you like for your sentiment analysis?',('Pipeline', 'TextBlob', 'MILESTONE 3'))
|
12 |
+
option = st.selectbox('Which pre-trained model would you like for your sentiment analysis?',('MILESTONE 3', 'Pipeline'))
|
13 |
|
14 |
+
st.write('You selected:', option)
|
15 |
|
16 |
+
if option == 'MILESTONE 3':
|
17 |
+
polarity = TextBlob(textIn).sentiment.polarity
|
18 |
+
subjectivity = TextBlob(textIn).sentiment.subjectivity
|
19 |
+
sentiment = ''
|
20 |
+
if polarity < 0:
|
21 |
+
sentiment = 'Negative'
|
22 |
+
elif polarity == 0:
|
23 |
+
sentiment = 'Neutral'
|
24 |
+
else:
|
25 |
+
sentiment = 'Positive'
|
26 |
+
st.write('According to TextBlob, input text is ', sentiment, ' and a subjectivity score (from 0 being objective to 1 being subjective) of ', subjectivity)
|
27 |
|
28 |
+
# model_name_0 = "Rathgeberj/milestone3_0"
|
29 |
+
# model_0 = AutoModelForSequenceClassification.from_pretrained(model_name_0)
|
30 |
+
# tokenizer_0 = AutoTokenizer.from_pretrained(model_name_0)
|
31 |
+
# classifier_0 = pipeline(task="sentiment-analysis", model=model_0, tokenizer=tokenizer_0)
|
32 |
|
33 |
+
# model_name_1 = "Rathgeberj/milestone3_1"
|
34 |
+
# model_1 = AutoModelForSequenceClassification.from_pretrained(model_name_1)
|
35 |
+
# tokenizer_1 = AutoTokenizer.from_pretrained(model_name_1)
|
36 |
+
# classifier_1 = pipeline(task="sentiment-analysis", model=model_1, tokenizer=tokenizer_1)
|
37 |
+
|
38 |
+
# model_name_2 = "Rathgeberj/milestone3_2"
|
39 |
+
# model_2 = AutoModelForSequenceClassification.from_pretrained(model_name_2)
|
40 |
+
# tokenizer_2 = AutoTokenizer.from_pretrained(model_name_2)
|
41 |
+
# classifier_2 = pipeline(task="sentiment-analysis", model=model_2, tokenizer=tokenizer_2)
|
42 |
+
|
43 |
+
# model_name_3 = "Rathgeberj/milestone3_3"
|
44 |
+
# model_3 = AutoModelForSequenceClassification.from_pretrained(model_name_3)
|
45 |
+
# tokenizer_3 = AutoTokenizer.from_pretrained(model_name_3)
|
46 |
+
# classifier_3 = pipeline(task="sentiment-analysis", model=model_3, tokenizer=tokenizer_3)
|
47 |
+
|
48 |
+
# model_name_4 = "Rathgeberj/milestone3_4"
|
49 |
+
# model_4 = AutoModelForSequenceClassification.from_pretrained(model_name_4)
|
50 |
+
# tokenizer_4 = AutoTokenizer.from_pretrained(model_name_4)
|
51 |
+
# classifier_4 = pipeline(task="sentiment-analysis", model=model_4, tokenizer=tokenizer_4)
|
52 |
+
|
53 |
+
# model_name_5 = "Rathgeberj/milestone3_5"
|
54 |
+
# model_5 = AutoModelForSequenceClassification.from_pretrained(model_name_5)
|
55 |
+
# tokenizer_5 = AutoTokenizer.from_pretrained(model_name_5)
|
56 |
+
# classifier_5 = pipeline(task="sentiment-analysis", model=model_5, tokenizer=tokenizer_5)
|
57 |
+
|
58 |
+
# models = [model_0, model_1, model_2, model_3, model_4, model_5]
|
59 |
+
# tokenizers = [tokenizer_0, tokenizer_1, tokenizer_2, tokenizer_3, tokenizer_4, tokenizer_5]
|
60 |
+
# classifiers = [classifier_0, classifier_1, classifier_2, classifier_3, classifier_4, classifier_5]
|
61 |
+
|
62 |
+
# X_train = [textIn]
|
63 |
+
# batch = tokenizer(X_train, padding=True, truncation=True, max_length=512, return_tensors="pt")
|
64 |
+
|
65 |
+
|
66 |
+
# st.write('TESTING2')
|
67 |
+
|
68 |
+
if option == 'Pipeline':
|
69 |
+
|
70 |
+
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
71 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
72 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
73 |
+
classifier = pipeline(task="sentiment-analysis", model=model, tokenizer=tokenizer)
|
74 |
+
preds = classifier(textIn)
|
75 |
+
preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds]
|
76 |
+
st.write('According to Pipeline, input text is ', preds[0]['label'], ' with a confidence of ', preds[0]['score'])
|
77 |
+
|
78 |
+
# if option == 'TextBlob':
|
79 |
+
# polarity = TextBlob(textIn).sentiment.polarity
|
80 |
+
# subjectivity = TextBlob(textIn).sentiment.subjectivity
|
81 |
+
# sentiment = ''
|
82 |
+
# if polarity < 0:
|
83 |
+
# sentiment = 'Negative'
|
84 |
+
# elif polarity == 0:
|
85 |
+
# sentiment = 'Neutral'
|
86 |
+
# else:
|
87 |
+
# sentiment = 'Positive'
|
88 |
+
# st.write('According to TextBlob, input text is ', sentiment, ' and a subjectivity score (from 0 being objective to 1 being subjective) of ', subjectivity)
|
89 |
|
90 |
|
91 |
#------------------------------------------------------------------------
|
|
|
113 |
# tokenizer = AutoTokenizer.from_pretrained(save_directory)
|
114 |
# model = AutoModelForSequenceClassification.from_pretrained(save_directory)
|
115 |
|
116 |
+
#------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|