DeeeTeeee01 commited on
Commit
44caf9f
·
1 Parent(s): 55c11e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -92
app.py CHANGED
@@ -69,98 +69,27 @@
69
 
70
 
71
 
72
- # import streamlit as st
73
- # import transformers
74
- # import torch
75
-
76
- # # Load the model and tokenizer
77
- # model = transformers.AutoModelForSequenceClassification.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
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
-
94
- # # Setting the page configurations
95
- # st.set_page_config(
96
- # page_title="Sentiment Analysis App",
97
- # page_icon=":smile:",
98
- # layout="wide",
99
- # initial_sidebar_state="auto",
100
- # )
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
- # """
120
- # <style>
121
- # body {
122
- # background: linear-gradient(to right, #4e79a7, #86a8e7);
123
- # color: lightblue;
124
- # }
125
- # h1 {
126
- # color: #4e79a7;
127
- # }
128
- # </style>
129
- # """,
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
 
146
  # Load the model and tokenizer
147
  model = transformers.AutoModelForSequenceClassification.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
148
  tokenizer = transformers.AutoTokenizer.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
149
 
150
- # Define the function for sentiment analysis without caching
 
151
  def predict_sentiment(text):
152
  # Load the pipeline
153
- pipeline = transformers.pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
154
 
 
155
  # Predict the sentiment
156
- prediction = pipeline(text)[0]
157
- sentiment = prediction["label"]
158
- score = prediction["score"]
159
 
160
- # Convert scores to a dictionary with labels as keys and percentages as values
161
- scores_dict = {label: score * 100 for label, score in zip(prediction["labels"], prediction["scores"])}
162
-
163
- return sentiment, scores_dict
164
 
165
  # Setting the page configurations
166
  st.set_page_config(
@@ -172,26 +101,19 @@ st.set_page_config(
172
 
173
  # Add description and title
174
  st.write("""
175
- # Predict if your text is Positive, Negative, or Neutral ...
176
- Please type your text and click the Predict button to know the sentiment!
177
  """)
178
 
 
 
 
179
  # Get user input
180
  text = st.text_input("Type here:")
181
 
182
  # Add Predict button
183
  predict_button = st.button("Predict")
184
 
185
- # Show sentiment output
186
- if predict_button and text:
187
- sentiment, scores_dict = predict_sentiment(text)
188
- st.write(f"The sentiment is {sentiment} with a score of {scores_dict[sentiment]:.2f}% for each category.")
189
-
190
- # Display individual percentages
191
- st.write("Sentiment Breakdown:")
192
- for label, score in scores_dict.items():
193
- st.write(f"- {label}: {score:.2f}%")
194
-
195
  # Define the CSS style for the app
196
  st.markdown(
197
  """
@@ -208,3 +130,14 @@ h1 {
208
  unsafe_allow_html=True
209
  )
210
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
 
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  import streamlit as st
73
  import transformers
74
+ import torch
75
 
76
  # Load the model and tokenizer
77
  model = transformers.AutoModelForSequenceClassification.from_pretrained("DeeeTeeee01/mytest_trainer_roberta-base")
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
 
94
  # Setting the page configurations
95
  st.set_page_config(
 
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
+