Winnie-Kay's picture
Update app.py
73e88e8
raw
history blame
3.17 kB
import streamlit as st
import transformers
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# Define the paths of the pre-trained models
model1_path = "saisi/finetuned-Sentiment-classfication-ROBERTA-Base-model"
model2_path = "saisi/finetuned-Sentiment-classfication-DISTILBERT-model"
# Initialize the tokenizer and models for sentiment analysis
tokenizer1 = AutoTokenizer.from_pretrained(model1_path)
model1 = AutoModelForSequenceClassification.from_pretrained(model1_path)
tokenizer2 = AutoTokenizer.from_pretrained(model2_path)
model2 = AutoModelForSequenceClassification.from_pretrained(model2_path)
# Define a function to preprocess the text data
def preprocess(text):
new_text = []
for t in text.split(" "):
t = '@user' if t.startswith('@') and len(t) > 1 else t
# Replace links with 'http'
t = 'http' if t.startswith('http') else t
new_text.append(t)
# Join the preprocessed text
return " ".join(new_text)
# Define a function to perform sentiment analysis on the input text using model 1
def sentiment_analysis_model1(text):
text = preprocess(text)
# Tokenize the input text using the pre-trained tokenizer
encoded_input = tokenizer1(text, return_tensors='pt')
# Feed the tokenized input to the pre-trained model and obtain output
output = model1(**encoded_input)
# Obtain the prediction scores for the output
scores_ = output[0][0].detach().numpy()
# Apply softmax activation function to obtain probability distribution over the labels
scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
# Format the output dictionary with the predicted scores
labels = ['Negative', 'Positive']
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
# Return the scores
return scores
# Define a function to perform sentiment analysis on the input text using model 2
def sentiment_analysis_model2(text):
text = preprocess(text)
# Tokenize the input text using the pre-trained tokenizer
encoded_input = tokenizer1(text, return_tensors='pt')
# Feed the tokenized input to the pre-trained model and obtain output
output = model1(**encoded_input)
# Obtain the prediction scores for the output
scores_ = output[0][0].detach().numpy()
# Apply softmax activation function to obtain probability distribution over the labels
scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
# Format the output dictionary with the predicted scores
labels = ['Negative', 'Positive']
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
# Return the scores
return scores
# Define the Streamlit app
def app():
  # Define the app title
st.title("Sentiment Analysis")
# Define the input field
text_input = st.text_input("Enter text:")
# Define the model selection dropdown
model_selection = st.selectbox("Select a model:", ["Model 1", "Model 2"])
    # Perform sentiment analysis when the submit button is clicked
    if st.button("Submit"):
        if text_input