saisi commited on
Commit
3cf7d5a
·
1 Parent(s): d0745c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -48
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
-     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
-     # Tokenize the input text using the pre-trained tokenizer
33
-     encoded_input = tokenizer1(text, return_tensors='pt')
34
-     # Feed the tokenized input to the pre-trained model and obtain output
35
-     output = model1(**encoded_input)
36
-     # Obtain the prediction scores for the output
37
-     scores_ = output[0][0].detach().numpy()
38
-     # Apply softmax activation function to obtain probability distribution over the labels
39
-     scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
40
-     # Format the output dictionary with the predicted scores
41
-     labels = ['Negative', 'Positive']
42
-     scores = {l:float(s) for (l,s) in zip(labels, scores_) }
43
-     # Return the scores
44
-     return scores
 
 
 
 
 
 
45
 
46
  # Define a function to perform sentiment analysis on the input text using model 2
47
  def sentiment_analysis_model2(text):
48
-     # Preprocess the input text
49
-     text = preprocess(text)
50
-     # Tokenize the input text using the pre-trained tokenizer
51
-     encoded_input = tokenizer2(text, return_tensors='pt')
52
-     # Feed the tokenized input to the pre-trained model and obtain output
53
-     output = model2(**encoded_input)
54
-     # Obtain the prediction scores for the output
55
-     scores_ = output[0][0].detach().numpy()
56
-     # Apply softmax activation function to obtain probability distribution over the labels
57
-     scores_ = torch.nn.functional.softmax(torch.from_numpy(scores_), dim=0).numpy()
58
-     # Format the output dictionary with the predicted scores
59
-     labels = ['Negative', 'Neutral', 'Positive']
60
-     scores = {l:float(s) for (l,s) in zip(labels, scores_) }
61
-     # Return the scores
62
-     return scores
 
 
 
 
 
 
63
 
64
  # Define the Streamlit app
65
  def app():
66
-     # Define the app title
67
-     st.title("Sentiment Analysis")
68
-     # Define the input field
69
-     text_input = st.text_input("Enter text:")
70
-     # Define the model selection dropdown
71
-     model_selection = st.selectbox("Select a model:", ["Model 1", "Model 2"])
72
-     # Perform sentiment analysis when the submit button is clicked
73
-     if st.button("Submit"):
74
-         if text_input
 
 
 
 
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