Spaces:
Runtime error
Runtime error
Commit
·
eb7cf2b
1
Parent(s):
0036af6
Update app.py
Browse files
app.py
CHANGED
@@ -1,188 +1,69 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
# Initialize the tokenizer and models for sentiment analysis
|
22 |
-
|
23 |
-
tokenizer1 = AutoTokenizer.from_pretrained(model1_path)
|
24 |
-
|
25 |
-
model1 = AutoModelForSequenceClassification.from_pretrained(model1_path)
|
26 |
-
|
27 |
-
tokenizer2 = AutoTokenizer.from_pretrained(model2_path)
|
28 |
-
|
29 |
-
model2 = AutoModelForSequenceClassification.from_pretrained(model2_path)
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
|
34 |
# Define a function to preprocess the text data
|
35 |
-
|
36 |
def preprocess(text):
|
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 |
-
scores_ = output[0][0].detach().numpy()
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
# Apply softmax activation function to obtain probability distribution over the labels
|
89 |
-
|
90 |
-
scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
# Format the output dictionary with the predicted scores
|
95 |
-
|
96 |
-
labels = ['Negative', 'Positive']
|
97 |
-
|
98 |
-
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
# Return the scores
|
103 |
-
|
104 |
-
return scores
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
# Define a function to perform sentiment analysis on the input text using model 2
|
110 |
-
|
111 |
-
def sentiment_analysis_model2(text):
|
112 |
-
|
113 |
-
# Preprocess the input text
|
114 |
-
|
115 |
-
text = preprocess(text)
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
# Tokenize the input text using the pre-trained tokenizer
|
121 |
-
|
122 |
-
encoded_input = tokenizer2(text, return_tensors='pt')
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
# Feed the tokenized input to the pre-trained model and obtain output
|
127 |
-
|
128 |
-
output = model2(**encoded_input)
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
# Obtain the prediction scores for the output
|
133 |
-
|
134 |
-
scores_ = output[0][0].detach().numpy()
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
# Apply softmax activation function to obtain probability distribution over the labels
|
139 |
-
|
140 |
-
scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
# Format the output dictionary with the predicted scores
|
145 |
-
|
146 |
-
labels = ['Negative', 'Neutral', 'Positive']
|
147 |
-
|
148 |
-
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
# Return the scores
|
153 |
-
|
154 |
-
return scores
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
# Define the Streamlit app
|
160 |
-
|
161 |
-
def app():
|
162 |
-
|
163 |
-
# Define the app title
|
164 |
-
|
165 |
-
st.title("Sentiment Analysis")
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
# Define the input field
|
171 |
-
|
172 |
-
text_input = st.text_input("Enter text:")
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
# Define the model selection dropdown
|
178 |
-
|
179 |
-
model_selection = st.selectbox("Select a model:", ["Model 1", "Model 2"])
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
# Perform sentiment analysis when the submit button is clicked
|
185 |
-
|
186 |
-
if st.button("Submit"):
|
187 |
-
|
188 |
-
if text_input
|
|
|
1 |
+
import numpy as np
|
2 |
+
from scipy.special import softmax
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import (
|
5 |
+
AutoTokenizer,
|
6 |
+
AutoConfig,
|
7 |
+
AutoModelForSequenceClassification,
|
8 |
+
TFAutoModelForSequenceClassification)
|
9 |
+
# Define the model path where the pre-trained model is saved on the Hugging Face model hub
|
10 |
+
model_path = "Winnie-Kay/Finetuned_bert_model"
|
11 |
+
|
12 |
+
# Initialize the tokenizer for the pre-trained model
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
14 |
+
|
15 |
+
# Load the configuration for the pre-trained model
|
16 |
+
config = AutoConfig.from_pretrained(model_path)
|
17 |
+
|
18 |
+
# Load the pre-trained model
|
19 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Define a function to preprocess the text data
|
|
|
22 |
def preprocess(text):
|
23 |
+
new_text = []
|
24 |
+
# Replace user mentions with '@user'
|
25 |
+
for t in text.split(" "):
|
26 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
27 |
+
# Replace links with 'http'
|
28 |
+
t = 'http' if t.startswith('http') else t
|
29 |
+
new_text.append(t)
|
30 |
+
# Join the preprocessed text
|
31 |
+
return " ".join(new_text)
|
32 |
+
|
33 |
+
# Define a function to perform sentiment analysis on the input text
|
34 |
+
def sentiment_analysis(text):
|
35 |
+
# Preprocess the input text
|
36 |
+
text = preprocess(text)
|
37 |
+
|
38 |
+
# Tokenize the input text using the pre-trained tokenizer
|
39 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
40 |
+
|
41 |
+
# Feed the tokenized input to the pre-trained model and obtain output
|
42 |
+
output = model(**encoded_input)
|
43 |
+
|
44 |
+
# Obtain the prediction scores for the output
|
45 |
+
scores_ = output[0][0].detach().numpy()
|
46 |
+
|
47 |
+
# Apply softmax activation function to obtain probability distribution over the labels
|
48 |
+
scores_ = softmax(scores_)
|
49 |
+
|
50 |
+
# Format the output dictionary with the predicted scores
|
51 |
+
labels = ['Negative', 'Neutral', 'Positive']
|
52 |
+
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
53 |
+
|
54 |
+
# Return the scores
|
55 |
+
return scores
|
56 |
+
|
57 |
+
# Define a Gradio interface to interact with the model
|
58 |
+
demo = gr.Interface(
|
59 |
+
fn=sentiment_analysis, # Function to perform sentiment analysis
|
60 |
+
inputs=gr.Textbox(placeholder="Write your tweet here..."), # Text input field
|
61 |
+
outputs="label", # Output type (here, we only display the label with the highest score)
|
62 |
+
interpretation="default", # Interpretation mode
|
63 |
+
examples=[["Have Fun with it...will be updated soon!"]],# Example input(s) to display on the interface
|
64 |
+
image=gr.Image("https://www.reputationx.com/hubfs/what-is-sentiment-analysis-cover.jpg"),
|
65 |
+
css= "body {background-color: black}"
|
66 |
+
)
|
67 |
+
|
68 |
+
# Launch the Gradio interface
|
69 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|