Multiple models added
Browse files- app.py +14 -10
- milestone3.py +12 -0
- requirements.txt +1 -1
app.py
CHANGED
@@ -6,19 +6,23 @@ st.header("This app is to analyze the sentiments behind a text. Currently it use
|
|
6 |
pre-trained models without fine-tuning.")
|
7 |
|
8 |
user_input = st.text_input("Enter your text:", value="Missing Sophie.Z...")
|
9 |
-
st.selectbox("Please select a model:",
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
if st.button("Analyze"):
|
14 |
-
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
15 |
|
|
|
16 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
17 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
18 |
-
|
19 |
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
else:
|
24 |
st.write("Go on! Try the app!")
|
|
|
6 |
pre-trained models without fine-tuning.")
|
7 |
|
8 |
user_input = st.text_input("Enter your text:", value="Missing Sophie.Z...")
|
9 |
+
user_model = st.selectbox("Please select a model:",
|
10 |
+
("distilbert-base-uncased-finetuned-sst-2-english",
|
11 |
+
"cardiffnlp/twitter-roberta-base-sentiment",
|
12 |
+
"finiteautomata/bertweet-base-sentiment-analysis"))
|
|
|
|
|
13 |
|
14 |
+
def analyze(model_name, text):
|
15 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
16 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
17 |
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
18 |
+
return classifier(text)
|
19 |
+
|
20 |
+
|
21 |
+
if st.button("Analyze"):
|
22 |
+
if not user_input:
|
23 |
+
st.write("Please enter a text.")
|
24 |
+
else:
|
25 |
+
with st.spinner("Hang on.... Analyzing..."):
|
26 |
+
st.write(analyze(user_model, user_input))
|
27 |
else:
|
28 |
st.write("Go on! Try the app!")
|
milestone3.py
CHANGED
@@ -14,10 +14,22 @@ res = classifier(["I am very happy now.", "Not happy now."])
|
|
14 |
for result in res:
|
15 |
print(result)
|
16 |
|
|
|
17 |
tokens = tokenizer.tokenize("I am very happy now.")
|
|
|
|
|
18 |
token_ids = tokenizer.convert_tokens_to_ids(tokens)
|
|
|
|
|
19 |
input_ids = tokenizer("I am very happy now.")
|
20 |
|
21 |
print(f'Tokens:{tokens}')
|
22 |
print(f'TokenIDs:{token_ids}')
|
23 |
print(f'InputIDs:{input_ids}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
for result in res:
|
15 |
print(result)
|
16 |
|
17 |
+
# Separate each word as a token
|
18 |
tokens = tokenizer.tokenize("I am very happy now.")
|
19 |
+
|
20 |
+
# Generate a list of IDs, each ID for each token
|
21 |
token_ids = tokenizer.convert_tokens_to_ids(tokens)
|
22 |
+
|
23 |
+
# Return a dict with IDs
|
24 |
input_ids = tokenizer("I am very happy now.")
|
25 |
|
26 |
print(f'Tokens:{tokens}')
|
27 |
print(f'TokenIDs:{token_ids}')
|
28 |
print(f'InputIDs:{input_ids}')
|
29 |
+
|
30 |
+
X_train = ["We are very happy to show you the Transformers library.",
|
31 |
+
"Hope you don't hate it"]
|
32 |
+
|
33 |
+
batch = tokenizer(X_train, padding=True, truncation=True, max_length=512, return_tensors="pt")
|
34 |
+
|
35 |
+
|
requirements.txt
CHANGED
@@ -1,2 +1,2 @@
|
|
1 |
transformers
|
2 |
-
|
|
|
1 |
transformers
|
2 |
+
torch
|