Spaces:
Sleeping
Sleeping
SudhanshuBlaze
commited on
Merge pull request #3 from SudhanshuBlaze/hatespeech
Browse files- .streamlit/config.toml +2 -0
- EDxHuggingface.py +100 -76
- requirements.txt +1 -1
.streamlit/config.toml
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
[theme]
|
2 |
+
base="light"
|
EDxHuggingface.py
CHANGED
@@ -14,13 +14,14 @@ API_URL_ED = "https://api-inference.huggingface.co/models/bhadresh-savani/bert-b
|
|
14 |
API_URL_HS = "https://api-inference.huggingface.co/models/IMSyPP/hate_speech_en"
|
15 |
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
16 |
|
|
|
|
|
|
|
|
|
|
|
17 |
# Set page title
|
18 |
st.title("GoEmotions Dashboard - Analyzing Emotions in Text")
|
19 |
|
20 |
-
# Add page description
|
21 |
-
description = "The GoEmotions Dashboard is a web-based user interface for analyzing emotions in text. The dashboard is powered by a pre-trained natural language processing model that can detect emotions in text input. Users can input any text and the dashboard will display the detected emotions in a set of gauges, with each gauge representing the intensity of a specific emotion category. The gauge colors are based on a predefined color map for each emotion category. This dashboard is useful for anyone who wants to understand the emotional content of a text, including content creators, marketers, and researchers."
|
22 |
-
st.markdown(description)
|
23 |
-
|
24 |
def query(payload):
|
25 |
response_ED = requests.request("POST", API_URL_ED, headers=headers, json=payload)
|
26 |
response_HS = requests.request("POST", API_URL_HS, headers=headers, json=payload)
|
@@ -62,6 +63,16 @@ color_map = {
|
|
62 |
label_hs = {"LABEL_0": "Acceptable", "LABEL_1": "inappropriate", "LABEL_2": "Offensive", "LABEL_3": "Violent"}
|
63 |
|
64 |
# Define default options
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
default_options = [
|
66 |
"I'm so excited for my vacation next week!",
|
67 |
"I'm feeling so stressed about work.",
|
@@ -76,15 +87,23 @@ default_options = [
|
|
76 |
"If you don't listen to me, I'll beat you up!",
|
77 |
]
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
#
|
81 |
-
|
82 |
|
83 |
-
#
|
84 |
-
|
85 |
|
86 |
-
#
|
87 |
-
if
|
88 |
|
89 |
# Call API and get predicted probabilities for each emotion category and hate speech classification
|
90 |
payload = {"inputs": text_input, "use_cache": True, "wait_for_model": True}
|
@@ -92,71 +111,76 @@ if st.button("Submit"):
|
|
92 |
predicted_probabilities_ED = response_ED[0]
|
93 |
predicted_probabilities_HS = response_HS[0]
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
'
|
125 |
-
'
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
'
|
132 |
-
'
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
|
|
|
14 |
API_URL_HS = "https://api-inference.huggingface.co/models/IMSyPP/hate_speech_en"
|
15 |
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
16 |
|
17 |
+
st.set_page_config(
|
18 |
+
page_title="GoEmotions Dashboard",
|
19 |
+
layout="wide"
|
20 |
+
)
|
21 |
+
|
22 |
# Set page title
|
23 |
st.title("GoEmotions Dashboard - Analyzing Emotions in Text")
|
24 |
|
|
|
|
|
|
|
|
|
25 |
def query(payload):
|
26 |
response_ED = requests.request("POST", API_URL_ED, headers=headers, json=payload)
|
27 |
response_HS = requests.request("POST", API_URL_HS, headers=headers, json=payload)
|
|
|
63 |
label_hs = {"LABEL_0": "Acceptable", "LABEL_1": "inappropriate", "LABEL_2": "Offensive", "LABEL_3": "Violent"}
|
64 |
|
65 |
# Define default options
|
66 |
+
|
67 |
+
def format_option(option):
|
68 |
+
# Set the maximum length of the option text you want to display
|
69 |
+
max_length = 200
|
70 |
+
if len(option) > max_length:
|
71 |
+
# Truncate the option text and add ellipsis at the end
|
72 |
+
return option[:max_length] + '...'
|
73 |
+
else:
|
74 |
+
return option
|
75 |
+
|
76 |
default_options = [
|
77 |
"I'm so excited for my vacation next week!",
|
78 |
"I'm feeling so stressed about work.",
|
|
|
87 |
"If you don't listen to me, I'll beat you up!",
|
88 |
]
|
89 |
|
90 |
+
with st.sidebar:
|
91 |
+
|
92 |
+
# Add page description
|
93 |
+
description = "The GoEmotions Dashboard is a web-based user interface for analyzing emotions in text. The dashboard is powered by a pre-trained natural language processing model that can detect emotions in text input. Users can input any text and the dashboard will display the detected emotions in a set of gauges, with each gauge representing the intensity of a specific emotion category. The gauge colors are based on a predefined color map for each emotion category. This dashboard is useful for anyone who wants to understand the emotional content of a text, including content creators, marketers, and researchers."
|
94 |
+
#st.markdown(description)
|
95 |
+
|
96 |
+
# Create dropdown with default options
|
97 |
+
selected_option = st.selectbox("Select a default option or enter your own text:", default_options, format_func=lambda x: format_option(x))
|
98 |
|
99 |
+
# Display text input with selected option as default value
|
100 |
+
text_input = st.text_area("Enter text to analyze emotions:", value = selected_option, height=100)
|
101 |
|
102 |
+
# Add submit button
|
103 |
+
submit = st.button("Submit")
|
104 |
|
105 |
+
# If submit button is clicked
|
106 |
+
if submit:
|
107 |
|
108 |
# Call API and get predicted probabilities for each emotion category and hate speech classification
|
109 |
payload = {"inputs": text_input, "use_cache": True, "wait_for_model": True}
|
|
|
111 |
predicted_probabilities_ED = response_ED[0]
|
112 |
predicted_probabilities_HS = response_HS[0]
|
113 |
|
114 |
+
ED, _, HS = st.columns([5,2,3])
|
115 |
+
|
116 |
+
with ED:
|
117 |
+
# Sort the predicted probabilities in descending order
|
118 |
+
sorted_probs_ED = sorted(predicted_probabilities_ED, key=lambda x: x['score'], reverse=True)
|
119 |
+
|
120 |
+
# Get the top 4 emotion categories and their scores
|
121 |
+
top_emotions = sorted_probs_ED[:4]
|
122 |
+
top_scores = [e['score'] for e in top_emotions]
|
123 |
+
|
124 |
+
# Normalize the scores so that they add up to 100%
|
125 |
+
total = sum(top_scores)
|
126 |
+
normalized_scores = [score/total * 100 for score in top_scores]
|
127 |
+
|
128 |
+
# Create the gauge charts for the top 4 emotion categories using the normalized scores
|
129 |
+
fig = make_subplots(rows=2, cols=2, specs=[[{'type': 'indicator'}, {'type': 'indicator'}],
|
130 |
+
[{'type': 'indicator'}, {'type': 'indicator'}]],
|
131 |
+
vertical_spacing=0.4)
|
132 |
+
|
133 |
+
for i, emotion in enumerate(top_emotions):
|
134 |
+
category = emotion['label']
|
135 |
+
color = color_map[category]
|
136 |
+
value = normalized_scores[i]
|
137 |
+
row = i // 2 + 1
|
138 |
+
col = i % 2 + 1
|
139 |
+
fig.add_trace(go.Indicator(
|
140 |
+
domain={'x': [0, 1], 'y': [0, 1]},
|
141 |
+
value=value,
|
142 |
+
mode="gauge+number",
|
143 |
+
title={'text': category.capitalize()},
|
144 |
+
gauge={'axis': {'range': [None, 100]},
|
145 |
+
'bar': {'color': color[3]},
|
146 |
+
'bgcolor': 'white',
|
147 |
+
'borderwidth': 2,
|
148 |
+
'bordercolor': color[1],
|
149 |
+
'steps': [{'range': [0, 33], 'color': color[0]},
|
150 |
+
{'range': [33, 66], 'color': color[1]},
|
151 |
+
{'range': [66, 100], 'color': color[2]}],
|
152 |
+
'threshold': {'line': {'color': "black", 'width': 4},
|
153 |
+
'thickness': 0.5,
|
154 |
+
'value': 50}}), row=row, col=col)
|
155 |
+
|
156 |
+
|
157 |
+
# Update layout
|
158 |
+
fig.update_layout(height=400, margin=dict(t=50, b=5, l=0, r=0))
|
159 |
+
|
160 |
+
# Display gauge charts
|
161 |
+
st.text("")
|
162 |
+
st.text("")
|
163 |
+
st.text("")
|
164 |
+
st.subheader("Emotion Detection")
|
165 |
+
st.text("")
|
166 |
+
st.plotly_chart(fig, use_container_width=True)
|
167 |
+
|
168 |
+
with _:
|
169 |
+
st.text("")
|
170 |
+
|
171 |
+
with HS:
|
172 |
+
# Display Hate Speech Classification
|
173 |
+
hate_detection = label_hs[predicted_probabilities_HS[0]['label']]
|
174 |
+
st.text("")
|
175 |
+
st.text("")
|
176 |
+
st.text("")
|
177 |
+
st.subheader("Hate Speech Analysis")
|
178 |
+
st.text("")
|
179 |
+
st.image(f"assets/{hate_detection}.jpg", width=200)
|
180 |
+
st.text("")
|
181 |
+
st.text("")
|
182 |
+
st.text("")
|
183 |
+
st.text("")
|
184 |
+
st.markdown(f"#### The given text is {hate_detection}")
|
185 |
|
186 |
|
requirements.txt
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
plotly==5.3.1
|
2 |
-
streamlit==1.
|
3 |
requests==2.26.0
|
4 |
python-dotenv==0.19.1
|
5 |
protobuf==3.20.*
|
|
|
1 |
plotly==5.3.1
|
2 |
+
streamlit==1.22.0
|
3 |
requests==2.26.0
|
4 |
python-dotenv==0.19.1
|
5 |
protobuf==3.20.*
|