|
import streamlit as st |
|
from transformers import AutoTokenizer, AutoConfig, AutoModelForSequenceClassification |
|
from scipy.special import softmax |
|
|
|
|
|
|
|
model_path = "https://huggingface.co/Ephicho/NLP_Capstone" |
|
tokenizer = AutoTokenizer.from_pretrained(model_path) |
|
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_ = output[0][0].detach().numpy() |
|
scores_ = softmax(scores_) |
|
|
|
|
|
labels = ['Negative', 'Neutral', 'Positive'] |
|
scores = {l: float(s) for (l, s) in zip(labels, scores_)} |
|
|
|
return scores |
|
|
|
|
|
st.title("Sentiment Analysis App") |
|
st.write(" Sentiment analysis, whether it's positive,negative, or neutral") |
|
|
|
|
|
|
|
input_text = st.text_area("Write your tweet here...") |
|
|
|
|
|
if st.button("Analyze Sentiment"): |
|
if input_text: |
|
|
|
scores = sentiment_analysis(input_text) |
|
|
|
|
|
st.text("Sentiment Scores:") |
|
for label, score in scores.items(): |
|
st.text(f"{label}: {score:.2f}") |
|
|
|
|
|
sentiment_label = max(scores, key=scores.get) |
|
|
|
|
|
sentiment_mapping = { |
|
"Negative": "Negative", |
|
"Neutral": "Neutral", |
|
"Positive": "Positive" |
|
} |
|
sentiment_readable = sentiment_mapping.get(sentiment_label, "Unknown") |
|
|
|
|
|
st.text(f"Sentiment: {sentiment_readable}") |
|
|
|
|
|
if st.button("Clear Input"): |
|
input_text = "" |
|
|