DeeeTeeee01 commited on
Commit
ccbd4ac
·
1 Parent(s): 720ebfc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -85
app.py CHANGED
@@ -65,76 +65,9 @@
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"]
87
-
88
- return sentiment, score
89
-
90
- # Setting the page configurations
91
- st.set_page_config(
92
- page_title="Sentiment Analysis App",
93
- page_icon=":smile:",
94
- layout="wide",
95
- initial_sidebar_state="auto",
96
- )
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;
123
- }
124
- </style>
125
- """,
126
- unsafe_allow_html=True
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}%!")
134
- elif sentiment == "Negative":
135
- st.error(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
  # import streamlit as st
140
  # import transformers
@@ -145,15 +78,16 @@ if predict_button and text:
145
  # tokenizer = transformers.AutoTokenizer.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
146
 
147
  # # Define the function for sentiment analysis
148
- # @st.cache
149
  # def predict_sentiment(text):
150
  # # Load the pipeline
151
- # pipeline = transformers.pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
152
 
 
153
  # # Predict the sentiment
154
- # prediction = pipeline(text)[0]
155
- # sentiment = prediction["label"]
156
- # score = prediction["score"]
157
 
158
  # return sentiment, score
159
 
@@ -167,27 +101,19 @@ if predict_button and text:
167
 
168
  # # Add description and title
169
  # st.write("""
170
- # # Predict if your text is Positive, Negative, or Neutral ...
171
- # Please type your text and click the Predict button to know the sentiment!
172
  # """)
173
 
 
 
 
174
  # # Get user input
175
  # text = st.text_input("Type here:")
176
 
177
  # # Add Predict button
178
  # predict_button = st.button("Predict")
179
 
180
- # # Show sentiment output
181
- # if predict_button and text:
182
- # sentiment, score = predict_sentiment(text)
183
- # st.write(f"The sentiment is {sentiment} with a score of {score*100:.2f}% for each category.")
184
-
185
- # # Display individual percentages
186
- # st.write("Sentiment Breakdown:")
187
- # st.write(f"- Negative: {score['NEGATIVE']*100:.2f}%")
188
- # st.write(f"- Positive: {score['POSITIVE']*100:.2f}%")
189
- # st.write(f"- Neutral: {score['NEUTRAL']*100:.2f}%")
190
-
191
  # # Define the CSS style for the app
192
  # st.markdown(
193
  # """
@@ -204,3 +130,81 @@ if predict_button and text:
204
  # unsafe_allow_html=True
205
  # )
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  # else:
66
  # st.warning(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
67
 
 
 
 
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
 
 
 
 
 
 
 
 
 
71
 
72
  # import streamlit as st
73
  # import transformers
 
78
  # tokenizer = transformers.AutoTokenizer.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
79
 
80
  # # Define the function for sentiment analysis
81
+ # @st.cache_resource
82
  # def predict_sentiment(text):
83
  # # Load the pipeline
84
+ # pipeline = transformers.pipeline("sentiment-analysis")
85
 
86
+
87
  # # Predict the sentiment
88
+ # prediction = pipeline(text)
89
+ # sentiment = prediction[0]["label"]
90
+ # score = prediction[0]["score"]
91
 
92
  # return sentiment, score
93
 
 
101
 
102
  # # Add description and title
103
  # st.write("""
104
+ # # Predict if your text is Positive, Negative or Neutral ...
105
+ # Please type your text and click the Predict button to know if your text has a positive, negative or neutral sentiment!
106
  # """)
107
 
108
+ # # Add image
109
+ # image = st.image("sentiment.jpeg", width=400)
110
+
111
  # # Get user input
112
  # text = st.text_input("Type here:")
113
 
114
  # # Add Predict button
115
  # predict_button = st.button("Predict")
116
 
 
 
 
 
 
 
 
 
 
 
 
117
  # # Define the CSS style for the app
118
  # st.markdown(
119
  # """
 
130
  # unsafe_allow_html=True
131
  # )
132
 
133
+ # # Show sentiment output
134
+ # if predict_button and text:
135
+ # sentiment, score = predict_sentiment(text)
136
+ # if sentiment == "Positive":
137
+ # st.success(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
138
+ # elif sentiment == "Negative":
139
+ # st.error(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
140
+ # else:
141
+ # st.warning(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
142
+
143
+ import streamlit as st
144
+ import transformers
145
+ import torch
146
+
147
+ # Load the model and tokenizer
148
+ model = transformers.AutoModelForSequenceClassification.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
149
+ tokenizer = transformers.AutoTokenizer.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
150
+
151
+ # Define the function for sentiment analysis
152
+ @st.cache
153
+ def predict_sentiment(text):
154
+ # Load the pipeline
155
+ pipeline = transformers.pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
156
+
157
+ # Predict the sentiment
158
+ prediction = pipeline(text)[0]
159
+ sentiment = prediction["label"]
160
+ score = prediction["score"]
161
+
162
+ return sentiment, score
163
+
164
+ # Setting the page configurations
165
+ st.set_page_config(
166
+ page_title="Sentiment Analysis App",
167
+ page_icon=":smile:",
168
+ layout="wide",
169
+ initial_sidebar_state="auto",
170
+ )
171
+
172
+ # Add description and title
173
+ st.write("""
174
+ # Predict if your text is Positive, Negative, or Neutral ...
175
+ Please type your text and click the Predict button to know the sentiment!
176
+ """)
177
+
178
+ # Get user input
179
+ text = st.text_input("Type here:")
180
+
181
+ # Add Predict button
182
+ predict_button = st.button("Predict")
183
+
184
+ # Show sentiment output
185
+ if predict_button and text:
186
+ sentiment, score = predict_sentiment(text)
187
+ st.write(f"The sentiment is {sentiment} with a score of {score*100:.2f}% for each category.")
188
+
189
+ # Display individual percentages
190
+ st.write("Sentiment Breakdown:")
191
+ st.write(f"- Negative: {score['NEGATIVE']*100:.2f}%")
192
+ st.write(f"- Positive: {score['POSITIVE']*100:.2f}%")
193
+ st.write(f"- Neutral: {score['NEUTRAL']*100:.2f}%")
194
+
195
+ # Define the CSS style for the app
196
+ st.markdown(
197
+ """
198
+ <style>
199
+ body {
200
+ background: linear-gradient(to right, #4e79a7, #86a8e7);
201
+ color: lightblue;
202
+ }
203
+ h1 {
204
+ color: #4e79a7;
205
+ }
206
+ </style>
207
+ """,
208
+ unsafe_allow_html=True
209
+ )
210
+