DeeeTeeee01 commited on
Commit
1f9eb21
·
1 Parent(s): 4ded591

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -69
app.py CHANGED
@@ -1,18 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # import streamlit as st
2
  # import transformers
3
  # import torch
4
 
5
  # # Load the model and tokenizer
6
- # model = transformers.AutoModelForSequenceClassification.from_pretrained("DeeeTeeee01/twitter-xlm-roberta-base-sentiment_dee")
7
- # tokenizer = transformers.AutoTokenizer.from_pretrained("DeeeTeeee01/twitter-xlm-roberta-base-sentiment_dee")
8
 
9
  # # Define the function for sentiment analysis
10
  # @st.cache_resource
11
  # def predict_sentiment(text):
12
- # # Load the pipeline.
13
  # pipeline = transformers.pipeline("sentiment-analysis")
14
 
15
- # # Predict the sentiment.
 
16
  # prediction = pipeline(text)
17
  # sentiment = prediction[0]["label"]
18
  # score = prediction[0]["score"]
@@ -29,23 +97,26 @@
29
 
30
  # # Add description and title
31
  # st.write("""
32
- # # Predict if your text is Positive, Negative or Nuetral ...
33
- # Please type your text and press ENTER key to know if your text is positive, negative, or neutral sentiment!
34
  # """)
35
 
36
-
37
  # # Add image
38
  # image = st.image("sentiment.jpeg", width=400)
39
 
40
  # # Get user input
41
  # text = st.text_input("Type here:")
42
 
 
 
 
43
  # # Define the CSS style for the app
44
  # st.markdown(
45
  # """
46
  # <style>
47
  # body {
48
- # background-color: #f5f5f5;
 
49
  # }
50
  # h1 {
51
  # color: #4e79a7;
@@ -56,7 +127,7 @@
56
  # )
57
 
58
  # # Show sentiment output
59
- # if text:
60
  # sentiment, score = predict_sentiment(text)
61
  # if sentiment == "Positive":
62
  # st.success(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
@@ -65,72 +136,47 @@
65
  # else:
66
  # st.warning(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
67
 
 
68
  import streamlit as st
69
  import transformers
70
- import torch
71
 
72
- # Load the model and tokenizer
73
  model = transformers.AutoModelForSequenceClassification.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
74
  tokenizer = transformers.AutoTokenizer.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
75
 
76
- # Define the function for sentiment analysis
77
  @st.cache_resource
78
  def predict_sentiment(text):
79
- # Load the pipeline
80
- pipeline = transformers.pipeline("sentiment-analysis")
81
-
82
- # Predict the sentiment
83
- prediction = pipeline(text)
84
- sentiment = prediction[0]["label"]
85
- score = prediction[0]["score"]
86
-
87
- return sentiment, score
88
-
89
- # Setting the page configurations
90
- st.set_page_config(
91
- page_title="Sentiment Analysis App",
92
- page_icon=":smile:",
93
- layout="wide",
94
- initial_sidebar_state="auto",
95
- )
96
-
97
- # Add description and title
98
- st.write("""
99
- # Predict if your text is Positive, Negative or Neutral ...
100
- Please type your text and click the Predict button to know if your text has a positive, negative or neutral sentiment!
101
- """)
102
-
103
- # Add image
104
- image = st.image("sentiment.jpeg", width=400)
105
-
106
- # Get user input
107
- text = st.text_input("Type here:")
108
-
109
- # Add Predict button
110
- predict_button = st.button("Predict")
111
-
112
- # Define the CSS style for the app
113
- st.markdown(
114
- """
115
- <style>
116
- body {
117
- background: linear-gradient(to right, #4e79a7, #86a8e7);
118
- color: lightblue;
119
- }
120
- h1 {
121
- color: #4e79a7;
122
- }
123
- </style>
124
- """,
125
- unsafe_allow_html=True
126
- )
127
-
128
- # Show sentiment output
129
  if predict_button and text:
130
- sentiment, score = predict_sentiment(text)
131
- if sentiment == "Positive":
132
- st.success(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
133
- elif sentiment == "Negative":
134
- st.error(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
135
- else:
136
- st.warning(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
 
 
1
+ # # import streamlit as st
2
+ # # import transformers
3
+ # # import torch
4
+
5
+ # # # Load the model and tokenizer
6
+ # # model = transformers.AutoModelForSequenceClassification.from_pretrained("DeeeTeeee01/twitter-xlm-roberta-base-sentiment_dee")
7
+ # # tokenizer = transformers.AutoTokenizer.from_pretrained("DeeeTeeee01/twitter-xlm-roberta-base-sentiment_dee")
8
+
9
+ # # # Define the function for sentiment analysis
10
+ # # @st.cache_resource
11
+ # # def predict_sentiment(text):
12
+ # # # Load the pipeline.
13
+ # # pipeline = transformers.pipeline("sentiment-analysis")
14
+
15
+ # # # Predict the sentiment.
16
+ # # prediction = pipeline(text)
17
+ # # sentiment = prediction[0]["label"]
18
+ # # score = prediction[0]["score"]
19
+
20
+ # # return sentiment, score
21
+
22
+ # # # Setting the page configurations
23
+ # # st.set_page_config(
24
+ # # page_title="Sentiment Analysis App",
25
+ # # page_icon=":smile:",
26
+ # # layout="wide",
27
+ # # initial_sidebar_state="auto",
28
+ # # )
29
+
30
+ # # # Add description and title
31
+ # # st.write("""
32
+ # # # Predict if your text is Positive, Negative or Nuetral ...
33
+ # # Please type your text and press ENTER key to know if your text is positive, negative, or neutral sentiment!
34
+ # # """)
35
+
36
+
37
+ # # # Add image
38
+ # # image = st.image("sentiment.jpeg", width=400)
39
+
40
+ # # # Get user input
41
+ # # text = st.text_input("Type here:")
42
+
43
+ # # # Define the CSS style for the app
44
+ # # st.markdown(
45
+ # # """
46
+ # # <style>
47
+ # # body {
48
+ # # background-color: #f5f5f5;
49
+ # # }
50
+ # # h1 {
51
+ # # color: #4e79a7;
52
+ # # }
53
+ # # </style>
54
+ # # """,
55
+ # # unsafe_allow_html=True
56
+ # # )
57
+
58
+ # # # Show sentiment output
59
+ # # if text:
60
+ # # sentiment, score = predict_sentiment(text)
61
+ # # if sentiment == "Positive":
62
+ # # st.success(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
63
+ # # elif sentiment == "Negative":
64
+ # # st.error(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
65
+ # # else:
66
+ # # st.warning(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
67
+
68
  # import streamlit as st
69
  # import transformers
70
  # import torch
71
 
72
  # # Load the model and tokenizer
73
+ # model = transformers.AutoModelForSequenceClassification.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
74
+ # tokenizer = transformers.AutoTokenizer.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
75
 
76
  # # Define the function for sentiment analysis
77
  # @st.cache_resource
78
  # def predict_sentiment(text):
79
+ # # Load the pipeline
80
  # pipeline = transformers.pipeline("sentiment-analysis")
81
 
82
+
83
+ # # Predict the sentiment
84
  # prediction = pipeline(text)
85
  # sentiment = prediction[0]["label"]
86
  # score = prediction[0]["score"]
 
97
 
98
  # # Add description and title
99
  # st.write("""
100
+ # # Predict if your text is Positive, Negative or Neutral ...
101
+ # Please type your text and click the Predict button to know if your text has a positive, negative or neutral sentiment!
102
  # """)
103
 
 
104
  # # Add image
105
  # image = st.image("sentiment.jpeg", width=400)
106
 
107
  # # Get user input
108
  # text = st.text_input("Type here:")
109
 
110
+ # # Add Predict button
111
+ # predict_button = st.button("Predict")
112
+
113
  # # Define the CSS style for the app
114
  # st.markdown(
115
  # """
116
  # <style>
117
  # body {
118
+ # background: linear-gradient(to right, #4e79a7, #86a8e7);
119
+ # color: lightblue;
120
  # }
121
  # h1 {
122
  # color: #4e79a7;
 
127
  # )
128
 
129
  # # Show sentiment output
130
+ # if predict_button and text:
131
  # sentiment, score = predict_sentiment(text)
132
  # if sentiment == "Positive":
133
  # st.success(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
 
136
  # else:
137
  # st.warning(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
138
 
139
+
140
  import streamlit as st
141
  import transformers
 
142
 
143
+ # Load model and tokenizer
144
  model = transformers.AutoModelForSequenceClassification.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
145
  tokenizer = transformers.AutoTokenizer.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
146
 
 
147
  @st.cache_resource
148
  def predict_sentiment(text):
149
+
150
+ # Get full model outputs
151
+ outputs = model(text)
152
+
153
+ # Extract probabilities
154
+ negative = outputs[0][0]
155
+ positive = outputs[0][1]
156
+ neutral = outputs[0][2]
157
+
158
+ return negative, positive, neutral
159
+
160
+ # Page config
161
+ st.set_page_config(page_title="Sentiment Analysis", page_icon=":smile:")
162
+
163
+ # Title and intro text
164
+ st.header("Predict Text Sentiment")
165
+ st.write("Enter text below to classify its sentiment as Positive, Negative or Neutral")
166
+
167
+ # Input text
168
+ text = st.text_input("Enter text:")
169
+
170
+ # Predict button
171
+ predict_button = st.button("Predict")
172
+
173
+ # Prediction output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  if predict_button and text:
175
+
176
+ # Get probabilities
177
+ negative, positive, neutral = predict_sentiment(text)
178
+
179
+ # Display probabilities
180
+ st.metric("Negative", f"{negative*100:.2f}%")
181
+ st.metric("Positive", f"{positive*100:.2f}%")
182
+ st.metric("Neutral", f"{neutral*100:.2f}%")