Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import nltk
|
| 4 |
+
from textblob import TextBlob
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from wordcloud import WordCloud
|
| 7 |
+
|
| 8 |
+
# Ensure necessary NLTK datasets are downloaded
|
| 9 |
+
nltk.download('punkt')
|
| 10 |
+
|
| 11 |
+
# Function to analyze sentiment
|
| 12 |
+
def analyze_sentiment(text):
|
| 13 |
+
analysis = TextBlob(text)
|
| 14 |
+
return analysis.sentiment.polarity # Returns sentiment score (-1 to 1)
|
| 15 |
+
|
| 16 |
+
# Function to categorize microaggressions (basic NLP)
|
| 17 |
+
def categorize_microaggression(text):
|
| 18 |
+
keywords = {
|
| 19 |
+
"Microinvalidation": ["you're overreacting", "stop being so sensitive", "I don’t see color"],
|
| 20 |
+
"Microinsult": ["you’re so articulate", "where are you really from", "you must be good at math"],
|
| 21 |
+
"Microassault": ["racial slur", "explicit insult", "offensive joke"]
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
for category, phrases in keywords.items():
|
| 25 |
+
for phrase in phrases:
|
| 26 |
+
if phrase in text.lower():
|
| 27 |
+
return category
|
| 28 |
+
return "Uncategorized"
|
| 29 |
+
|
| 30 |
+
# Streamlit UI
|
| 31 |
+
st.title("MicroAggression Insight Tool")
|
| 32 |
+
st.write("Analyze, categorize, and visualize reported microaggressions.")
|
| 33 |
+
|
| 34 |
+
# Collect user input
|
| 35 |
+
user_input = st.text_area("Enter a microaggression example:")
|
| 36 |
+
|
| 37 |
+
if st.button("Analyze"):
|
| 38 |
+
if user_input:
|
| 39 |
+
sentiment_score = analyze_sentiment(user_input)
|
| 40 |
+
category = categorize_microaggression(user_input)
|
| 41 |
+
|
| 42 |
+
# Display results
|
| 43 |
+
st.write(f"**Predicted Category:** {category}")
|
| 44 |
+
st.write(f"**Sentiment Score:** {sentiment_score:.2f} (Negative: -1, Neutral: 0, Positive: 1)")
|
| 45 |
+
|
| 46 |
+
# Store input in a dataframe
|
| 47 |
+
df = pd.DataFrame({"Text": [user_input], "Category": [category], "Sentiment": [sentiment_score]})
|
| 48 |
+
|
| 49 |
+
# Save locally (optional)
|
| 50 |
+
df.to_csv("data.csv", mode='a', header=False, index=False)
|
| 51 |
+
|
| 52 |
+
# Load existing data
|
| 53 |
+
try:
|
| 54 |
+
data = pd.read_csv("data.csv", names=["Text", "Category", "Sentiment"])
|
| 55 |
+
|
| 56 |
+
if not data.empty:
|
| 57 |
+
st.subheader("Data Insights")
|
| 58 |
+
|
| 59 |
+
# Show category distribution
|
| 60 |
+
st.write("### Microaggression Category Distribution")
|
| 61 |
+
category_counts = data["Category"].value_counts()
|
| 62 |
+
fig, ax = plt.subplots()
|
| 63 |
+
category_counts.plot(kind='bar', ax=ax)
|
| 64 |
+
st.pyplot(fig)
|
| 65 |
+
|
| 66 |
+
# Generate a word cloud
|
| 67 |
+
st.write("### Common Words in Microaggressions")
|
| 68 |
+
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(" ".join(data["Text"]))
|
| 69 |
+
fig, ax = plt.subplots()
|
| 70 |
+
ax.imshow(wordcloud, interpolation="bilinear")
|
| 71 |
+
ax.axis("off")
|
| 72 |
+
st.pyplot(fig)
|
| 73 |
+
except FileNotFoundError:
|
| 74 |
+
st.write("No data available yet.")
|