File size: 2,031 Bytes
aa7f5a4
 
bd8d813
 
aa7f5a4
 
 
4b8f05e
 
aa7f5a4
734dda1
bd8d813
6716393
bd8d813
 
4b8f05e
 
bd8d813
 
 
 
6c5dac1
bd8d813
09de9ab
 
 
 
4b8f05e
6c5dac1
 
bd8d813
 
 
09de9ab
 
 
6c5dac1
 
 
 
 
 
 
 
 
 
4b8f05e
bd8d813
aa7f5a4
 
13c3404
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import streamlit as st  #Web App
from transformers import pipeline
from pysentimiento import create_analyzer

#title
st.title("Sentiment Analysis - Classify Sentiment of text")

model = st.selectbox("Which pretrained model would you like to use?",("roberta-large-mnli","twitter-XLM-roBERTa-base","bertweet-sentiment-analysis"))

data = []
text = st.text_input("Enter text here:","Artificial Intelligence is useful")
data.append(text)
if model == "roberta-large-mnli":
    #1
    if st.button("Run Sentiment Analysis of Text"): 
        model_path = "roberta-large-mnli"
        sentiment_pipeline = pipeline(model=model_path)
        result = sentiment_pipeline(data)
        label = result[0]["label"]
        score = result[0]["score"]
        st.write("The classification of the given text is " + label + " with a score of " + str(score))
elif model == "twitter-XLM-roBERTa-base":
    #2
    if st.button("Run Sentiment Analysis of Text"): 
        model_path = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
        sentiment_task = pipeline("sentiment-analysis", model=model_path, tokenizer=model_path)
        result = sentiment_task(text)
        label = result[0]["label"].capitalize()
        score = result[0]["score"]
        st.write("The classification of the given text is " + label + " with a score of " + str(score))

elif model == "bertweet-sentiment-analysis": 
    #3 
    if st.button("Run Sentiment Analysis of Text"): 
        analyzer = create_analyzer(task="sentiment", lang="en")
        result = analyzer.predict(text)
        if result.output == "POS": 
            label = "POSITIVE"
        elif result.output == "NEU": 
            label = "NEUTRAL"
        else: 
            label = "NEGATIVE"
        
        neg = result.probas["NEG"]
        pos = result.probas["POS"]
        neu = result.probas["NEU"]
        st.write("The classification of the given text is " + label + " with the scores broken down as: Positive - " + str(pos) + ", Neutral - " + str(neu) + ", Negative - " + str(neg))