File size: 1,113 Bytes
a4afdf5
36d5e6f
a4afdf5
36d5e6f
a4afdf5
 
 
36d5e6f
a4afdf5
36d5e6f
 
 
a4afdf5
36d5e6f
 
 
 
a4afdf5
 
 
 
 
 
 
 
 
 
 
36d5e6f
 
 
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
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import streamlit as st
import torch

# Load tokenizer and model from Hugging Face Hub
tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")

# Streamlit UI setup
st.title("Sentiment Analysis App using GenAI Models")

# Text input from the user
user_input = st.text_area("Enter text to analyze sentiment:")

# Prediction button
if st.button("Analyze"):
    if user_input:
        # Tokenize the user input
        inputs = tokenizer(user_input, return_tensors="pt")
        
        # Perform inference
        with torch.no_grad():
            outputs = model(**inputs)
        
        # Interpret the results
        predicted_class = torch.argmax(outputs.logits, dim=1).item()
        sentiment = ["Negative", "Neutral", "Positive"][predicted_class]  # Assuming 3 classes

        st.write(f"**Predicted Sentiment:** {sentiment}")
    else:
        st.warning("Please enter some text to analyze.")