|
import streamlit as st |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
|
|
|
@st.cache_resource |
|
def load_model(): |
|
model_name = 'cardiffnlp/twitter-xlm-roberta-base-sentiment' |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
labels = ['Negative', 'Neutral', 'Positive'] |
|
return tokenizer, model, labels |
|
|
|
tokenizer, model, labels = load_model() |
|
|
|
MAX_LENGTH = 128 |
|
|
|
def analyze_sentiment(text): |
|
inputs = tokenizer( |
|
text, |
|
return_tensors='pt', |
|
truncation=True, |
|
max_length=MAX_LENGTH |
|
) |
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
probs = torch.nn.functional.softmax(outputs.logits, dim=-1) |
|
sentiment = labels[torch.argmax(probs)] |
|
confidence = torch.max(probs).item() |
|
return sentiment, confidence |
|
|
|
|
|
st.title('Multilingual Sentiment Analysis') |
|
|
|
text = st.text_area('Enter text in any language:') |
|
|
|
if st.button('Analyze'): |
|
if text: |
|
sentiment, confidence = analyze_sentiment(text) |
|
st.write(f"**Sentiment:** {sentiment}") |
|
st.write(f"**Confidence:** {confidence:.2f}") |
|
else: |
|
st.write("Please enter some text to analyze.") |
|
|