Spaces:
Runtime error
Runtime error
Commit
·
ac2a663
1
Parent(s):
07c91fa
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,69 @@
|
|
1 |
-
#
|
2 |
-
#
|
3 |
-
#
|
4 |
-
|
5 |
-
# #
|
6 |
-
#
|
7 |
-
#
|
8 |
-
|
9 |
-
# #
|
10 |
-
#
|
11 |
-
#
|
12 |
-
#
|
13 |
-
#
|
14 |
-
|
15 |
-
#
|
16 |
-
#
|
17 |
-
#
|
18 |
-
#
|
19 |
-
|
20 |
-
#
|
21 |
-
|
22 |
-
# #
|
23 |
-
#
|
24 |
-
#
|
25 |
-
#
|
26 |
-
#
|
27 |
-
#
|
28 |
-
#
|
29 |
-
|
30 |
-
# #
|
31 |
-
#
|
32 |
-
# #
|
33 |
-
#
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
-
# #
|
38 |
-
#
|
39 |
-
|
40 |
-
# #
|
41 |
-
#
|
42 |
-
|
43 |
-
# #
|
44 |
-
#
|
45 |
-
#
|
46 |
-
#
|
47 |
-
#
|
48 |
-
#
|
49 |
-
#
|
50 |
-
#
|
51 |
-
#
|
52 |
-
#
|
53 |
-
#
|
54 |
-
#
|
55 |
-
#
|
56 |
-
#
|
57 |
-
|
58 |
-
# #
|
59 |
-
#
|
60 |
-
#
|
61 |
-
#
|
62 |
-
#
|
63 |
-
#
|
64 |
-
#
|
65 |
-
#
|
66 |
-
#
|
67 |
|
68 |
# import streamlit as st
|
69 |
# import transformers
|
@@ -136,51 +136,71 @@
|
|
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 |
-
|
|
|
148 |
def predict_sentiment(text):
|
|
|
|
|
149 |
|
150 |
-
|
|
|
|
|
|
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
# # Get full model outputs
|
155 |
-
# outputs = model(text)
|
156 |
-
|
157 |
-
# Extract probabilities
|
158 |
-
negative = outputs[0][0]
|
159 |
-
positive = outputs[0][1]
|
160 |
-
neutral = outputs[0][2]
|
161 |
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
-
#
|
165 |
-
st.
|
|
|
|
|
|
|
166 |
|
167 |
-
#
|
168 |
-
st.
|
169 |
-
st.write("Enter text below to classify its sentiment as Positive, Negative or Neutral")
|
170 |
|
171 |
-
#
|
172 |
-
|
173 |
|
174 |
-
#
|
175 |
-
predict_button = st.button("Predict")
|
176 |
-
|
177 |
-
# Prediction output
|
178 |
if predict_button and text:
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
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
|
141 |
+
import torch
|
142 |
|
143 |
+
# Load the 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 |
+
# 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 |
|
160 |
+
# Setting the page configurations
|
161 |
+
st.set_page_config(
|
162 |
+
page_title="Sentiment Analysis App",
|
163 |
+
page_icon=":smile:",
|
164 |
+
layout="wide",
|
165 |
+
initial_sidebar_state="auto",
|
166 |
+
)
|
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 |
+
"""
|
194 |
+
<style>
|
195 |
+
body {
|
196 |
+
background: linear-gradient(to right, #4e79a7, #86a8e7);
|
197 |
+
color: lightblue;
|
198 |
+
}
|
199 |
+
h1 {
|
200 |
+
color: #4e79a7;
|
201 |
+
}
|
202 |
+
</style>
|
203 |
+
""",
|
204 |
+
unsafe_allow_html=True
|
205 |
+
)
|
206 |
+
|