Commit
·
7005f78
1
Parent(s):
a42610b
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,48 @@
|
|
1 |
from DistilBERT import model_DB
|
2 |
import streamlit as st
|
3 |
-
from transformers import
|
|
|
|
|
4 |
import torch
|
5 |
|
|
|
6 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
7 |
-
tokenizer =
|
8 |
|
9 |
def sentiment_analysis_DB(input):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from DistilBERT import model_DB
|
2 |
import streamlit as st
|
3 |
+
from transformers import DistilBertTokenizer, DistilBertModel
|
4 |
+
import logging
|
5 |
+
logging.basicConfig(level=logging.ERROR)
|
6 |
import torch
|
7 |
|
8 |
+
MAX_LEN = 100
|
9 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
10 |
+
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased', truncation=True, do_lower_case=True)
|
11 |
|
12 |
def sentiment_analysis_DB(input):
|
13 |
+
inputs = tokenizer.encode_plus(
|
14 |
+
input,
|
15 |
+
None,
|
16 |
+
add_special_tokens=True,
|
17 |
+
max_length=MAX_LEN,
|
18 |
+
pad_to_max_length=True,
|
19 |
+
return_token_type_ids=True
|
20 |
+
)
|
21 |
+
ids = inputs['input_ids']
|
22 |
+
mask = inputs['attention_mask']
|
23 |
+
token_type_ids = inputs["token_type_ids"]
|
24 |
+
output = model_DB(ids, mask, token_type_ids)
|
25 |
+
final_outputs = np.array(output)
|
26 |
+
final_outputs = final_outputs[0]
|
27 |
+
if final_outputs == True:
|
28 |
+
result = 1
|
29 |
+
else:
|
30 |
+
result = 0
|
31 |
+
return result
|
32 |
+
|
33 |
+
# Streamlit app
|
34 |
+
st.title("Sentiment Analysis App")
|
35 |
+
|
36 |
+
# User input
|
37 |
+
user_input = st.text_area("Enter some text:")
|
38 |
+
|
39 |
+
# Button to trigger sentiment analysis
|
40 |
+
if st.button("Analyze Sentiment"):
|
41 |
+
# Perform sentiment analysis
|
42 |
+
result = sentiment_analysis_DB(user_input)
|
43 |
+
|
44 |
+
# Display result
|
45 |
+
if result == 1:
|
46 |
+
st.success("Positive sentiment detected!")
|
47 |
+
else:
|
48 |
+
st.error("Negative sentiment detected.")
|