|
import streamlit as st |
|
import pandas as pd |
|
from scipy.special import softmax |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoConfig |
|
|
|
|
|
model_path = "aaronayitey/Sentiment-classfication-ROBERTA-model" |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained('roberta-base') |
|
|
|
|
|
config = AutoConfig.from_pretrained(model_path) |
|
|
|
|
|
model = AutoModelForSequenceClassification.from_pretrained(model_path) |
|
|
|
|
|
def preprocess(text): |
|
new_text = [] |
|
|
|
for t in text.split(" "): |
|
t = '@user' if t.startswith('@') and len(t) > 1 else t |
|
|
|
t = 'http' if t.startswith('http') else t |
|
new_text.append(t) |
|
|
|
return " ".join(new_text) |
|
|
|
|
|
def sentiment_analysis(text): |
|
|
|
text = preprocess(text) |
|
|
|
|
|
encoded_input = tokenizer(text, return_tensors='pt') |
|
|
|
|
|
output = model(**encoded_input) |
|
|
|
|
|
scores = torch.softmax(output.logits, dim=1)[0].tolist() |
|
|
|
|
|
labels = ['Negative', 'Neutral', 'Positive'] |
|
scores = {l: float(s) for (l, s) in zip(labels, scores)} |
|
|
|
|
|
max_score_label = max(scores, key=scores.get) |
|
|
|
|
|
return max_score_label |
|
|
|
|
|
|
|
st.title("Sentiment Analysis") |
|
st.write("Enter a text, and we'll determine its sentiment!") |
|
|
|
|
|
user_input = st.text_area("Enter text here:") |
|
|
|
|
|
if st.button("Analyze Sentiment"): |
|
if user_input: |
|
|
|
sentiment_label = sentiment_analysis(user_input) |
|
st.write(f"Sentiment: {sentiment_label}") |
|
else: |
|
st.warning("Please enter some text for sentiment analysis.") |