Spaces:
Runtime error
Runtime error
Commit
·
29d3362
1
Parent(s):
452b39b
added hate speech classification
Browse files- .gitignore +1 -0
- EDxHuggingface.py +24 -9
- requirements.txt +2 -0
.gitignore
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
.env
|
| 2 |
.DS_Store
|
|
|
|
|
|
| 1 |
.env
|
| 2 |
.DS_Store
|
| 3 |
+
tumai
|
EDxHuggingface.py
CHANGED
|
@@ -10,7 +10,8 @@ load_dotenv()
|
|
| 10 |
# AI model code
|
| 11 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 12 |
|
| 13 |
-
|
|
|
|
| 14 |
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 15 |
|
| 16 |
# Set page title
|
|
@@ -21,9 +22,9 @@ description = "The GoEmotions Dashboard is a web-based user interface for analyz
|
|
| 21 |
st.markdown(description)
|
| 22 |
|
| 23 |
def query(payload):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
return json.loads(
|
| 27 |
|
| 28 |
# Define color map for each emotion category
|
| 29 |
color_map = {
|
|
@@ -57,6 +58,9 @@ color_map = {
|
|
| 57 |
'neutral': ['#1f77b4', '#aec7e8', '#ff7f0e', '#d62728']
|
| 58 |
}
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
# Define default options
|
| 61 |
default_options = [
|
| 62 |
"I'm so excited for my vacation next week!",
|
|
@@ -64,6 +68,12 @@ default_options = [
|
|
| 64 |
"I just received great news from my doctor!",
|
| 65 |
"I can't wait to see my best friend tomorrow.",
|
| 66 |
"I'm feeling so lonely and sad today."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
]
|
| 68 |
|
| 69 |
|
|
@@ -76,15 +86,17 @@ text_input = st.text_input("Enter text to analyze emotions:", selected_option)
|
|
| 76 |
# Add submit button
|
| 77 |
if st.button("Submit"):
|
| 78 |
|
| 79 |
-
# Call API and get predicted probabilities for each emotion category
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# Sort the predicted probabilities in descending order
|
| 84 |
-
|
| 85 |
|
| 86 |
# Get the top 4 emotion categories and their scores
|
| 87 |
-
top_emotions =
|
| 88 |
top_scores = [e['score'] for e in top_emotions]
|
| 89 |
|
| 90 |
# Normalize the scores so that they add up to 100%
|
|
@@ -126,3 +138,6 @@ if st.button("Submit"):
|
|
| 126 |
# Display gauge charts
|
| 127 |
st.plotly_chart(fig, use_container_width=True)
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# AI model code
|
| 11 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 12 |
|
| 13 |
+
API_URL_ED = "https://api-inference.huggingface.co/models/bhadresh-savani/bert-base-go-emotion"
|
| 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
|
|
|
|
| 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)
|
| 27 |
+
return (json.loads(response_ED.content.decode("utf-8")),json.loads(response_HS.content.decode("utf-8")))
|
| 28 |
|
| 29 |
# Define color map for each emotion category
|
| 30 |
color_map = {
|
|
|
|
| 58 |
'neutral': ['#1f77b4', '#aec7e8', '#ff7f0e', '#d62728']
|
| 59 |
}
|
| 60 |
|
| 61 |
+
# Labels for Hate Speech Classification
|
| 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!",
|
|
|
|
| 68 |
"I just received great news from my doctor!",
|
| 69 |
"I can't wait to see my best friend tomorrow.",
|
| 70 |
"I'm feeling so lonely and sad today."
|
| 71 |
+
"I'm so angry at my neighbor for being so rude.",
|
| 72 |
+
"You are so annoying!",
|
| 73 |
+
"You people from small towns are so dumb.",
|
| 74 |
+
"If you don't agree with me, you are a moron.",
|
| 75 |
+
"I hate you so much!",
|
| 76 |
+
"If you don't listen to me, I'll beat you up!",
|
| 77 |
]
|
| 78 |
|
| 79 |
|
|
|
|
| 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}
|
| 91 |
+
response_ED, response_HS = query(payload)
|
| 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%
|
|
|
|
| 138 |
# Display gauge charts
|
| 139 |
st.plotly_chart(fig, use_container_width=True)
|
| 140 |
|
| 141 |
+
# Display Hate Speech Classification
|
| 142 |
+
hate_detection = label_hs[predicted_probabilities_HS[0]['label']]
|
| 143 |
+
st.text(f"Hate Speech Classification: {hate_detection}")
|
requirements.txt
CHANGED
|
@@ -2,3 +2,5 @@ plotly==5.3.1
|
|
| 2 |
streamlit==1.3.0
|
| 3 |
requests==2.26.0
|
| 4 |
python-dotenv==0.19.1
|
|
|
|
|
|
|
|
|
| 2 |
streamlit==1.3.0
|
| 3 |
requests==2.26.0
|
| 4 |
python-dotenv==0.19.1
|
| 5 |
+
protobuf==3.20.*
|
| 6 |
+
altair<5
|