File size: 1,232 Bytes
bdab8dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c74ce45
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import AutoTokenizer, RobertaForSequenceClassification
import numpy as np
import torch

st.title("CS482 Project Sentiment Analysis")

text = st.text_area(label="Text to be analyzed", value="This sentiment analysis app is great!")

selected_model = st.radio(label="Model", options=["Model 1", "Model 2"])

analyze_button = st.button(label="Analyze")

st.markdown("**:red[Sentiment:]**")

with st.spinner(text="Analyzing..."):
    if analyze_button:
        if selected_model=="Model 1":
            tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-emotion")
            model = RobertaForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-emotion")
        else:
            tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
            model = RobertaForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
        inputs = tokenizer(text, return_tensors="pt")
        with torch.no_grad():
            logits = model(**inputs).logits
        prediction_id = logits.argmax().item()
        results = model.config.id2label[prediction_id]
        st.write(results)