File size: 2,543 Bytes
35f56ba
7749ef6
47ef74f
cfa2b70
cd87a42
 
 
d71bb22
cd87a42
 
 
 
 
 
e43f53b
47ef74f
 
26f67cd
c704d04
 
47ef74f
d5b90e7
dff0151
21d64ee
dff0151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21d64ee
d5b90e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85516be
dff0151
 
26f67cd
dff0151
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
72
import streamlit as st
import tensorflow as tf
from transformers import pipeline
from textblob import TextBlob
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn.functional as F

model_name = "distilbert-base-uncased-finetuned-sst-2-english"

model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

classifier = pipeline(task="sentiment-analysis", model=model, tokenizer=tokenizer)

textIn = st.text_input("Input Text Here:", "I really like the color of your car!")

option = st.selectbox('Which pre-trained model would you like for your sentiment analysis?',('Pipeline', 'TextBlob', 'MILESTONE 3: FINE-TUNED'))

st.write('You selected:', option)


#------------------------------------------------------------------------

# tokens = tokenizer.tokenize(textIn)
# token_ids = tokenizer.convert_tokens_to_ids(tokens)
# input_ids = tokenizer(textIn)


# X_train = [textIn]

# batch = tokenizer(X_train, padding=True, truncation=True, max_length=512, return_tensors="pt")
# # batch = torch.tensor(batchbatch["input_ids"])

# with torch.no_grad():
#     outputs = model(**batch, labels=torch.tensor([1, 0]))
#     predictions = F.softmax(outputs.logits, dim=1)
#     labels = torch.argmax(predictions, dim=1)
#     labels = [model.config.id2label[label_id] for label_id in labels.tolist()]

# # save_directory = "saved"
# tokenizer.save_pretrained(save_directory)
# model.save_pretrained(save_directory)

# tokenizer = AutoTokenizer.from_pretrained(save_directory)
# model = AutoModelForSequenceClassification.from_pretrained(save_directory)

#------------------------------------------------------------------------

if option == 'Pipeline':
    # pipeline
    preds = classifier(textIn)
    preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds]
    st.write('According to Pipeline, input text is ', preds[0]['label'], ' with a confidence of ', preds[0]['score'])

if option == 'TextBlob':
    # textblob
    polarity = TextBlob(textIn).sentiment.polarity
    subjectivity = TextBlob(textIn).sentiment.subjectivity
    sentiment = ''
    if polarity < 0:
        sentiment = 'Negative'
    elif polarity == 0:
        sentiment = 'Neutral'
    else:
        sentiment = 'Positive'

    st.write('According to TextBlob, input text is ', sentiment, ' and a subjectivity score (from 0 being objective to 1 being subjective) of ', subjectivity)


if option == 'MILESTONE 3: FINE-TUNED':
    ...