Spaces:
Runtime error
Runtime error
File size: 3,172 Bytes
18ef847 8c483b2 18ef847 8c483b2 18ef847 eb7cf2b 087fa98 18ef847 087fa98 e5238cb de68813 18ef847 23b9d9a 73e88e8 18ef847 73e88e8 18ef847 73e88e8 18ef847 |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
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 |