|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
import streamlit as st |
|
import torch |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment") |
|
model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment") |
|
|
|
|
|
st.title("Sentiment Analysis App using GenAI Models") |
|
|
|
|
|
user_input = st.text_area("Enter text to analyze sentiment:") |
|
|
|
|
|
if st.button("Analyze"): |
|
if user_input: |
|
|
|
inputs = tokenizer(user_input, return_tensors="pt") |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
|
|
|
|
predicted_class = torch.argmax(outputs.logits, dim=1).item() |
|
sentiment = ["Negative", "Neutral", "Positive"][predicted_class] |
|
|
|
st.write(f"**Predicted Sentiment:** {sentiment}") |
|
else: |
|
st.warning("Please enter some text to analyze.") |
|
|