Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,60 +15,75 @@ model2 = AutoModelForSequenceClassification.from_pretrained(model2_path)
|
|
| 15 |
|
| 16 |
# Define a function to preprocess the text data
|
| 17 |
def preprocess(text):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
# Define a function to perform sentiment analysis on the input text using model 1
|
| 29 |
def sentiment_analysis_model1(text):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Define a function to perform sentiment analysis on the input text using model 2
|
| 47 |
def sentiment_analysis_model2(text):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# Define the Streamlit app
|
| 65 |
def app():
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Define a function to preprocess the text data
|
| 17 |
def preprocess(text):
|
| 18 |
+
new_text = []
|
| 19 |
+
# Replace user mentions with '@user'
|
| 20 |
+
for t in text.split(" "):
|
| 21 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
| 22 |
+
# Replace links with 'http'
|
| 23 |
+
t = 'http' if t.startswith('http') else t
|
| 24 |
+
new_text.append(t)
|
| 25 |
+
# Join the preprocessed text
|
| 26 |
+
return " ".join(new_text)
|
| 27 |
|
| 28 |
# Define a function to perform sentiment analysis on the input text using model 1
|
| 29 |
def sentiment_analysis_model1(text):
|
| 30 |
+
# Preprocess the input text
|
| 31 |
+
text = preprocess(text)
|
| 32 |
+
|
| 33 |
+
# Tokenize the input text using the pre-trained tokenizer
|
| 34 |
+
encoded_input = tokenizer1(text, return_tensors='pt')
|
| 35 |
+
|
| 36 |
+
# Feed the tokenized input to the pre-trained model and obtain output
|
| 37 |
+
output = model1(**encoded_input)
|
| 38 |
+
|
| 39 |
+
# Obtain the prediction scores for the output
|
| 40 |
+
scores_ = output[0][0].detach().numpy()
|
| 41 |
+
|
| 42 |
+
# Apply softmax activation function to obtain probability distribution over the labels
|
| 43 |
+
scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
|
| 44 |
+
|
| 45 |
+
# Format the output dictionary with the predicted scores
|
| 46 |
+
labels = ['Negative', 'Positive']
|
| 47 |
+
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
| 48 |
+
|
| 49 |
+
# Return the scores
|
| 50 |
+
return scores
|
| 51 |
|
| 52 |
# Define a function to perform sentiment analysis on the input text using model 2
|
| 53 |
def sentiment_analysis_model2(text):
|
| 54 |
+
# Preprocess the input text
|
| 55 |
+
text = preprocess(text)
|
| 56 |
+
|
| 57 |
+
# Tokenize the input text using the pre-trained tokenizer
|
| 58 |
+
encoded_input = tokenizer2(text, return_tensors='pt')
|
| 59 |
+
|
| 60 |
+
# Feed the tokenized input to the pre-trained model and obtain output
|
| 61 |
+
output = model2(**encoded_input)
|
| 62 |
+
|
| 63 |
+
# Obtain the prediction scores for the output
|
| 64 |
+
scores_ = output[0][0].detach().numpy()
|
| 65 |
+
|
| 66 |
+
# Apply softmax activation function to obtain probability distribution over the labels
|
| 67 |
+
scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
|
| 68 |
+
|
| 69 |
+
# Format the output dictionary with the predicted scores
|
| 70 |
+
labels = ['Negative', 'Neutral', 'Positive']
|
| 71 |
+
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
| 72 |
+
|
| 73 |
+
# Return the scores
|
| 74 |
+
return scores
|
| 75 |
|
| 76 |
# Define the Streamlit app
|
| 77 |
def app():
|
| 78 |
+
# Define the app title
|
| 79 |
+
st.title("Sentiment Analysis")
|
| 80 |
+
|
| 81 |
+
# Define the input field
|
| 82 |
+
text_input = st.text_input("Enter text:")
|
| 83 |
+
|
| 84 |
+
# Define the model selection dropdown
|
| 85 |
+
model_selection = st.selectbox("Select a model:", ["Model 1", "Model 2"])
|
| 86 |
+
|
| 87 |
+
# Perform sentiment analysis when the submit button is clicked
|
| 88 |
+
if st.button("Submit"):
|
| 89 |
+
if text_input
|