File size: 2,519 Bytes
a42610b 7005f78 a42610b 7005f78 a42610b 7005f78 a42610b bab1cdc edec6bb a42610b 7005f78 cae091c 7005f78 4a4406e 7005f78 cae091c 4a4406e cae091c 4a4406e cae091c 4a4406e 7005f78 |
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 73 74 75 76 |
import streamlit as st
from transformers import DistilBertTokenizer, DistilBertModel
import logging
logging.basicConfig(level=logging.ERROR)
import torch
MAX_LEN = 100
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased', truncation=True, do_lower_case=True)
class DistilBERTClass(torch.nn.Module):
def __init__(self):
super(DistilBERTClass, self).__init__()
self.l1 = DistilBertModel.from_pretrained("distilbert-base-uncased")
self.pre_classifier = torch.nn.Linear(768, 768)
self.dropout = torch.nn.Dropout(0.1)
self.classifier = torch.nn.Linear(768, 1)
def forward(self, input_ids, attention_mask, token_type_ids):
output_1 = self.l1(input_ids=input_ids, attention_mask=attention_mask)
hidden_state = output_1[0]
pooler = hidden_state[:, 0]
pooler = self.pre_classifier(pooler)
pooler = torch.nn.ReLU()(pooler)
pooler = self.dropout(pooler)
output = self.classifier(pooler)
return output
model_DB = DistilBERTClass()
loaded_model_path = './model_DB_1.pt'
model_DB.load_state_dict(torch.load(loaded_model_path, map_location=torch.device('cpu')))
model_DB.to(device)
def sentiment_analysis_DB(input):
inputs = tokenizer.encode_plus(
input,
None,
add_special_tokens=True,
max_length=100,
pad_to_max_length=True,
return_token_type_ids=True
)
ids = torch.tensor([inputs['input_ids']]) # Convert to PyTorch tensor
mask = torch.tensor([inputs['attention_mask']]) # Convert to PyTorch tensor
token_type_ids = torch.tensor([inputs["token_type_ids"]]) # Convert to PyTorch tensor
# Assuming model_DB is a PyTorch model
output = model_DB(ids, mask, token_type_ids)
print('Raw output is ', output)
sigmoid_output = torch.sigmoid(output)
print('Sigmoid output is ', sigmoid_output)
# Assuming you want to use a threshold of 0.5
result = 1 if sigmoid_output.item() > 0.5 else 0
return result
# Streamlit app
st.title("Sentiment Analysis App")
# User input
user_input = st.text_area("Enter some text:")
# Button to trigger sentiment analysis
if st.button("Analyze Sentiment"):
# Perform sentiment analysis
result = sentiment_analysis_DB(user_input)
# Display result
if result == 1:
st.success("Positive sentiment detected!")
else:
st.error("Negative sentiment detected.") |