File size: 2,240 Bytes
fd73992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f428ba
fd73992
 
 
661f5cd
48fdd13
fd73992
 
 
 
 
 
 
 
 
 
39ead4b
5f428ba
fd73992
 
 
 
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
import streamlit as st
from transformers import pipeline, AutoTokenizer

# Define a list of pretrained models
sentimentAnalysisModels = {
    "Roberta": "deepset/roberta-base-squad2",
    "RESPIN": "RESPIN/Telugu_LanguageModels_for_ASR",
    "GPT2-n": "DunnBC22/gpt2-Causal_Language_Model-AG_News",
    "Roberta-Base (ADDITIONAL MODEL!)": "achimoraites/TextClassification-roberta-base_ag_news",
}

# Display a selection box for the user to choose a model
sentimentAnalysisLanguageSelections = st.selectbox("Please select one of these finetuned models from the dropdown", list(sentimentAnalysisModels.keys()))

# roBERTa specific label map
sentimentAnalysisLanguageMap = {"LABEL_0": "NEGATIVE",
                     "LABEL_1": "POSITIVE", "LABEL_2": "NEUTRAL"}

# Load the selected model and tokenizer
sentimentAnalysisModelName = sentimentAnalysisModels[sentimentAnalysisLanguageSelections]
sentimentAnalysisTokenizer = AutoTokenizer.from_pretrained(sentimentAnalysisModelName)
sentiment_pipeline = pipeline(
    "sentiment-analysis", model=sentimentAnalysisModelName, tokenizer=sentimentAnalysisTokenizer)

# Get user input and perform sentiment analysis
sentimentAnalysisTextInput = st.text_input("Please enter sample text for finetuned language model below:",
                           "I am grateful for Data Science Program at NJIT and to have amazing faculty to learn under!")
sentimentAnalysisSubmitButton = st.button("Press the submit button for final grading")

if sentimentAnalysisSubmitButton and sentimentAnalysisTextInput:
    sentimentAnalysisFinalOutput = sentiment_pipeline(sentimentAnalysisTextInput)
    if sentimentAnalysisLanguageSelections == "roBERTa":
        st.write("Roberta Sentiment Analysis Resultant Value:", sentimentAnalysisLanguageMap[sentimentAnalysisFinalOutput[0]["Roberta Label"]])
        st.write("Roberta Sentiment Analysis Resultant Score:", sentimentAnalysisFinalOutput[0]["Probability Assigned with Sentiment Analysis Label"])
    else:
        st.write("Sentiment Analysis Resultant Value:", sentimentAnalysisFinalOutput[0]["Sentiment Analysis Label"])
        st.write("Sentiment Analysis Resultant Score:", sentimentAnalysisFinalOutput[0]["Probability Assigned with Sentiment Analysis Label"])