Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,85 @@
|
|
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
|
17 |
-
def
|
18 |
-
|
19 |
-
|
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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 =
|
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(
|
|
|
48 |
|
49 |
-
# Save
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
# Load existing data
|
53 |
try:
|
54 |
-
data = pd.read_csv("data.csv"
|
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()
|
@@ -70,5 +94,23 @@ try:
|
|
70 |
ax.imshow(wordcloud, interpolation="bilinear")
|
71 |
ax.axis("off")
|
72 |
st.pyplot(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
except FileNotFoundError:
|
74 |
-
st.write("No data available yet.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import nltk
|
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
+
from textblob import TextBlob
|
7 |
+
from transformers import pipeline
|
8 |
from wordcloud import WordCloud
|
9 |
+
import os
|
10 |
|
11 |
# Ensure necessary NLTK datasets are downloaded
|
12 |
nltk.download('punkt')
|
13 |
|
14 |
+
# Load pre-trained NLP model for classification
|
15 |
+
classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
|
16 |
+
|
17 |
# Function to analyze sentiment
|
18 |
def analyze_sentiment(text):
|
19 |
analysis = TextBlob(text)
|
20 |
return analysis.sentiment.polarity # Returns sentiment score (-1 to 1)
|
21 |
|
22 |
+
# Function to categorize microaggressions using AI
|
23 |
+
def classify_microaggression(text):
|
24 |
+
result = classifier(text)[0]
|
25 |
+
return result["label"], result["score"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Streamlit UI
|
28 |
+
st.title("MicroAggression Insight Tool 🚀")
|
29 |
+
st.write("Analyze, categorize, and visualize reported microaggressions using AI and psychological research methods.")
|
30 |
|
31 |
+
# **Ethical Research Consent Notice**
|
32 |
+
st.markdown("### Ethical Considerations")
|
33 |
+
st.write("By submitting your responses, you agree to participate in this study anonymously. Your data will be stored securely for research purposes.")
|
34 |
+
|
35 |
+
# **Experimental Study: Perception & Acceptability Ratings**
|
36 |
+
st.subheader("Microaggression Perception Experiment")
|
37 |
+
st.write("How acceptable do you find the following statement?")
|
38 |
+
|
39 |
+
# User Input for Microaggression Statement
|
40 |
user_input = st.text_area("Enter a microaggression example:")
|
41 |
|
42 |
+
rating = st.slider("Rate on a scale of 1 (Not Acceptable) to 5 (Completely Acceptable)", 1, 5)
|
43 |
+
emotional_impact = st.slider("How emotionally impactful is this? (1 = Not at all, 5 = Very Strong)", 1, 5)
|
44 |
+
|
45 |
if st.button("Analyze"):
|
46 |
if user_input:
|
47 |
sentiment_score = analyze_sentiment(user_input)
|
48 |
+
category, confidence = classify_microaggression(user_input)
|
49 |
|
50 |
# Display results
|
51 |
+
st.write(f"**Predicted Category:** {category} (Confidence: {confidence:.2f})")
|
52 |
st.write(f"**Sentiment Score:** {sentiment_score:.2f} (Negative: -1, Neutral: 0, Positive: 1)")
|
53 |
+
st.write(f"**Perceived Acceptability:** {rating}/5")
|
54 |
+
st.write(f"**Emotional Impact:** {emotional_impact}/5")
|
55 |
|
56 |
# Store input in a dataframe
|
57 |
+
df = pd.DataFrame([[user_input, category, sentiment_score, rating, emotional_impact]],
|
58 |
+
columns=["Text", "Category", "Sentiment", "Acceptability", "Impact"])
|
59 |
|
60 |
+
# Save data to CSV
|
61 |
+
file_path = "data.csv"
|
62 |
+
if not os.path.exists(file_path):
|
63 |
+
df.to_csv(file_path, index=False)
|
64 |
+
else:
|
65 |
+
df.to_csv(file_path, mode='a', header=False, index=False)
|
66 |
+
|
67 |
+
# **Focus Group & Open-ended Research Input**
|
68 |
+
st.subheader("📢 Focus Group: Share Your Perspective")
|
69 |
+
response = st.text_area("Describe how this statement made you feel:")
|
70 |
+
if st.button("Submit Response"):
|
71 |
+
if response:
|
72 |
+
with open("qualitative_responses.csv", "a") as f:
|
73 |
+
f.write(response + "\n")
|
74 |
+
st.success("Response recorded! ✅")
|
75 |
+
|
76 |
+
# **Data Analysis & Visualization**
|
77 |
+
st.subheader("📊 Data Insights & Visualizations")
|
78 |
|
|
|
79 |
try:
|
80 |
+
data = pd.read_csv("data.csv")
|
81 |
|
82 |
if not data.empty:
|
|
|
|
|
83 |
# Show category distribution
|
84 |
st.write("### Microaggression Category Distribution")
|
85 |
category_counts = data["Category"].value_counts()
|
|
|
94 |
ax.imshow(wordcloud, interpolation="bilinear")
|
95 |
ax.axis("off")
|
96 |
st.pyplot(fig)
|
97 |
+
|
98 |
+
# **Correlation Heatmap**
|
99 |
+
st.write("### Correlation Between Sentiment, Acceptability, and Impact")
|
100 |
+
plt.figure(figsize=(6, 4))
|
101 |
+
sns.heatmap(data[["Sentiment", "Acceptability", "Impact"]].corr(), annot=True, cmap="coolwarm", fmt=".2f")
|
102 |
+
st.pyplot(plt)
|
103 |
+
|
104 |
+
# **Show Data Table**
|
105 |
+
if st.checkbox("Show Data Table"):
|
106 |
+
st.dataframe(data)
|
107 |
except FileNotFoundError:
|
108 |
+
st.write("No data available yet. Submit responses to populate insights.")
|
109 |
+
|
110 |
+
# **Download Data Option for Researchers**
|
111 |
+
st.subheader("📥 Download Data for Research")
|
112 |
+
if os.path.exists("data.csv"):
|
113 |
+
with open("data.csv", "rb") as file:
|
114 |
+
st.download_button(label="Download CSV Data", data=file, file_name="microaggression_data.csv", mime="text/csv")
|
115 |
+
|
116 |
+
st.write("🔬 Developed as a research tool for understanding microaggressions in social psychology.")
|