File size: 3,498 Bytes
18ef847
8c483b2
18ef847
8c483b2
18ef847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb7cf2b
 
 
087fa98
 
18ef847
087fa98
e5238cb
18ef847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import streamlit as st

import transformers

import torch

from transformers import AutoTokenizer, AutoModelForSequenceClassification




# Define the paths of the pre-trained models

model1_path = "saisi/finetuned-Sentiment-classfication-ROBERTA-Base-model"

model2_path = "saisi/finetuned-Sentiment-classfication-DISTILBERT-model"




# Initialize the tokenizer and models for sentiment analysis

tokenizer1 = AutoTokenizer.from_pretrained(model1_path)

model1 = AutoModelForSequenceClassification.from_pretrained(model1_path)

tokenizer2 = AutoTokenizer.from_pretrained(model2_path)

model2 = AutoModelForSequenceClassification.from_pretrained(model2_path)




# Define a function to preprocess the text data

def preprocess(text):
    new_text = []

    # Replace user mentions with '@user'

    for t in text.split(" "):

        t = '@user' if t.startswith('@') and len(t) > 1 else t

        # Replace links with 'http'

        t = 'http' if t.startswith('http') else t

        new_text.append(t)

    # Join the preprocessed text

    return " ".join(new_text)




# Define a function to perform sentiment analysis on the input text using model 1

def sentiment_analysis_model1(text):

    # Preprocess the input text

    text = preprocess(text)




    # Tokenize the input text using the pre-trained tokenizer

    encoded_input = tokenizer1(text, return_tensors='pt')

   

    # Feed the tokenized input to the pre-trained model and obtain output

    output = model1(**encoded_input)

   

    # Obtain the prediction scores for the output

    scores_ = output[0][0].detach().numpy()

   

    # Apply softmax activation function to obtain probability distribution over the labels

    scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()

   

    # Format the output dictionary with the predicted scores

    labels = ['Negative', 'Positive']

    scores = {l:float(s) for (l,s) in zip(labels, scores_) }

   

    # Return the scores

    return scores




# Define a function to perform sentiment analysis on the input text using model 2

def sentiment_analysis_model2(text):

    # Preprocess the input text

    text = preprocess(text)




    # Tokenize the input text using the pre-trained tokenizer

    encoded_input = tokenizer2(text, return_tensors='pt')

   

    # Feed the tokenized input to the pre-trained model and obtain output

    output = model2(**encoded_input)

   

    # Obtain the prediction scores for the output

    scores_ = output[0][0].detach().numpy()

   

    # Apply softmax activation function to obtain probability distribution over the labels

    scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()

   

    # Format the output dictionary with the predicted scores

    labels = ['Negative', 'Neutral', 'Positive']

    scores = {l:float(s) for (l,s) in zip(labels, scores_) }

   

    # Return the scores

    return scores




# Define the Streamlit app

def app():

    # Define the app title

    st.title("Sentiment Analysis")




    # Define the input field

    text_input = st.text_input("Enter text:")




    # Define the model selection dropdown

    model_selection = st.selectbox("Select a model:", ["Model 1", "Model 2"])




    # Perform sentiment analysis when the submit button is clicked

    if st.button("Submit"):

        if text_input