Spaces:
Runtime error
Runtime error
Commit
·
8454de8
1
Parent(s):
a39c327
Create app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,68 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
# if labell == 'label_0':
|
20 |
-
# sentiment = 'Negative'
|
21 |
-
# elif labell == 'label_1':
|
22 |
-
# sentiment = 'Neutral'
|
23 |
-
# elif labell == 'label_2':
|
24 |
-
# sentiment = 'Positive'
|
25 |
-
# else: sentiment = labell
|
26 |
-
# score = result[0]['score']
|
27 |
-
# return sentiment, score
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
# page_title="Sentiment Analysis App",
|
32 |
-
# page_icon=":smile:",
|
33 |
-
# layout="wide",
|
34 |
-
# initial_sidebar_state="auto",
|
35 |
-
# )
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
# # How Positive or Negative is your Text?
|
40 |
-
# Enter some text and we'll tell you if it has a positive, negative, or neutral sentiment!
|
41 |
-
# """ )
|
42 |
|
43 |
-
#
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
#
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
# color: #4e79a7;
|
59 |
-
# }
|
60 |
-
# </style>
|
61 |
-
# """,
|
62 |
-
# unsafe_allow_html=True
|
63 |
-
# )
|
64 |
-
|
65 |
-
|
66 |
-
# # Show sentiment output
|
67 |
-
# if text:
|
68 |
-
# sentiment, score = predict_sentiment(text)
|
69 |
-
# if sentiment == "Positive":
|
70 |
-
# st.success(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
|
71 |
-
# elif sentiment == "Negative":
|
72 |
-
# st.error(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
|
73 |
-
# else:
|
74 |
-
# 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("RICHARDMENSAH/twitter_xlm_roberta_base")
|
7 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained("RICHARDMENSAH/twitter_xlm_roberta_base")
|
8 |
|
9 |
+
# Define the function for sentiment analysis
|
10 |
+
def predict_sentiment(text):
|
11 |
+
result = model([text])
|
12 |
+
labell = result[0]['label']
|
13 |
+
if labell == 'label_0':
|
14 |
+
sentiment = 'Negative'
|
15 |
+
elif labell == 'label_1':
|
16 |
+
sentiment = 'Neutral'
|
17 |
+
elif labell == 'label_2':
|
18 |
+
sentiment = 'Positive'
|
19 |
+
else:
|
20 |
+
sentiment = labell
|
21 |
+
score = result[0]['score']
|
22 |
+
return sentiment, score
|
23 |
|
24 |
+
# Setting the page configurations
|
25 |
+
st.set_page_config(
|
26 |
+
page_title="Sentiment Analysis App",
|
27 |
+
page_icon=":smile:",
|
28 |
+
layout="wide",
|
29 |
+
initial_sidebar_state="auto",
|
30 |
+
)
|
31 |
|
32 |
+
# Add description and title
|
33 |
+
st.write("""
|
34 |
+
# How Positive or Negative is your Text?
|
35 |
+
Enter some text and we'll tell you if it has a positive, negative, or neutral sentiment!
|
36 |
+
""" )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
# Add image
|
39 |
+
image = st.image("https://i0.wp.com/thedatascientist.com/wp-content/uploads/2018/10/sentiment-analysis.png", width=400)
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
# Get user input
|
42 |
+
text = st.text_input("Enter some text here:")
|
|
|
|
|
|
|
43 |
|
44 |
+
# Define the CSS style for the app
|
45 |
+
st.markdown(
|
46 |
+
"""
|
47 |
+
<style>
|
48 |
+
body {
|
49 |
+
background-color: #f5f5f5;
|
50 |
+
}
|
51 |
|
52 |
+
h1 {
|
53 |
+
color: #4e79a7;
|
54 |
+
}
|
55 |
+
</style>
|
56 |
+
""",
|
57 |
+
unsafe_allow_html=True
|
58 |
+
)
|
59 |
|
60 |
+
# Show sentiment output
|
61 |
+
if text:
|
62 |
+
sentiment, score = predict_sentiment(text)
|
63 |
+
if sentiment == "Positive":
|
64 |
+
st.success(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
|
65 |
+
elif sentiment == "Negative":
|
66 |
+
st.error(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
|
67 |
+
else:
|
68 |
+
st.warning(f"The sentiment is {sentiment} with a score of {score*100:.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|