SudhanshuBlaze commited on
Commit
55a9445
2 Parent(s): 2d665fc 80310c3

Merge pull request #3 from SudhanshuBlaze/hatespeech

Browse files
Files changed (3) hide show
  1. .streamlit/config.toml +2 -0
  2. EDxHuggingface.py +100 -76
  3. 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
- # Create dropdown with default options
81
- selected_option = st.selectbox("Select a default option or enter your own text:", default_options)
82
 
83
- # Display text input with selected option as default value
84
- text_input = st.text_input("Enter text to analyze emotions:", selected_option)
85
 
86
- # Add submit button
87
- if st.button("Submit"):
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
- # Sort the predicted probabilities in descending order
96
- sorted_probs_ED = sorted(predicted_probabilities_ED, key=lambda x: x['score'], reverse=True)
97
-
98
- # Get the top 4 emotion categories and their scores
99
- top_emotions = sorted_probs_ED[:4]
100
- top_scores = [e['score'] for e in top_emotions]
101
-
102
- # Normalize the scores so that they add up to 100%
103
- total = sum(top_scores)
104
- normalized_scores = [score/total * 100 for score in top_scores]
105
-
106
- # Create the gauge charts for the top 4 emotion categories using the normalized scores
107
- fig = make_subplots(rows=2, cols=2, specs=[[{'type': 'indicator'}, {'type': 'indicator'}],
108
- [{'type': 'indicator'}, {'type': 'indicator'}]],
109
- vertical_spacing=0.4)
110
-
111
- for i, emotion in enumerate(top_emotions):
112
- category = emotion['label']
113
- color = color_map[category]
114
- value = normalized_scores[i]
115
- row = i // 2 + 1
116
- col = i % 2 + 1
117
- fig.add_trace(go.Indicator(
118
- domain={'x': [0, 1], 'y': [0, 1]},
119
- value=value,
120
- mode="gauge+number",
121
- title={'text': category.capitalize()},
122
- gauge={'axis': {'range': [None, 100]},
123
- 'bar': {'color': color[3]},
124
- 'bgcolor': 'white',
125
- 'borderwidth': 2,
126
- 'bordercolor': color[1],
127
- 'steps': [{'range': [0, 33], 'color': color[0]},
128
- {'range': [33, 66], 'color': color[1]},
129
- {'range': [66, 100], 'color': color[2]}],
130
- 'threshold': {'line': {'color': "black", 'width': 4},
131
- 'thickness': 0.5,
132
- 'value': 50}}), row=row, col=col)
133
-
134
-
135
- # Update layout
136
- fig.update_layout(height=400, margin=dict(t=50, b=5, l=0, r=0))
137
-
138
- # Display gauge charts
139
- st.text("")
140
- st.text("")
141
- st.text("")
142
- st.header("Emotion Detection")
143
- st.text("")
144
- st.plotly_chart(fig, use_container_width=True)
145
-
146
- # Display Hate Speech Classification
147
- hate_detection = label_hs[predicted_probabilities_HS[0]['label']]
148
- st.text("")
149
- st.text("")
150
- st.text("")
151
- st.header("Hate Speech Analysis")
152
- st.text("")
153
- col1, col2 = st.columns(2)
154
-
155
- col1.image(f"assets/{hate_detection}.jpg", width=200)
156
- col2.text("")
157
- col2.text("")
158
- col2.text("")
159
- col2.text("")
160
- col2.subheader(f"The given text is {hate_detection}")
 
 
 
 
 
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.0
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.*