Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import nltk | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
from textblob import TextBlob | |
from transformers import pipeline | |
from wordcloud import WordCloud | |
import os | |
# Ensure necessary NLTK datasets are downloaded | |
nltk.download('punkt') | |
# Load pre-trained NLP model for classification | |
classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english") | |
# Function to analyze sentiment | |
def analyze_sentiment(text): | |
analysis = TextBlob(text) | |
return analysis.sentiment.polarity # Returns sentiment score (-1 to 1) | |
# Function to categorize microaggressions using AI | |
def classify_microaggression(text): | |
result = classifier(text)[0] | |
return result["label"], result["score"] | |
# Streamlit UI | |
st.title("MicroAggression Insight Tool π") | |
st.write("Analyze, categorize, and visualize reported microaggressions using AI and psychological research methods.") | |
# **Ethical Research Consent Notice** | |
st.markdown("### Ethical Considerations") | |
st.write("By submitting your responses, you agree to participate in this study anonymously. Your data will be stored securely for research purposes.") | |
# **Experimental Study: Perception & Acceptability Ratings** | |
st.subheader("Microaggression Perception Experiment") | |
st.write("How acceptable do you find the following statement?") | |
# User Input for Microaggression Statement | |
user_input = st.text_area("Enter a microaggression example:") | |
rating = st.slider("Rate on a scale of 1 (Not Acceptable) to 5 (Completely Acceptable)", 1, 5) | |
emotional_impact = st.slider("How emotionally impactful is this? (1 = Not at all, 5 = Very Strong)", 1, 5) | |
if st.button("Analyze"): | |
if user_input: | |
sentiment_score = analyze_sentiment(user_input) | |
category, confidence = classify_microaggression(user_input) | |
# Display results | |
st.write(f"**Predicted Category:** {category} (Confidence: {confidence:.2f})") | |
st.write(f"**Sentiment Score:** {sentiment_score:.2f} (Negative: -1, Neutral: 0, Positive: 1)") | |
st.write(f"**Perceived Acceptability:** {rating}/5") | |
st.write(f"**Emotional Impact:** {emotional_impact}/5") | |
# Store input in a dataframe | |
df = pd.DataFrame([[user_input, category, sentiment_score, rating, emotional_impact]], | |
columns=["Text", "Category", "Sentiment", "Acceptability", "Impact"]) | |
# Save data to CSV | |
file_path = "data.csv" | |
if not os.path.exists(file_path): | |
df.to_csv(file_path, index=False) | |
else: | |
df.to_csv(file_path, mode='a', header=False, index=False) | |
# **Focus Group & Open-ended Research Input** | |
st.subheader("π’ Focus Group: Share Your Perspective") | |
response = st.text_area("Describe how this statement made you feel:") | |
if st.button("Submit Response"): | |
if response: | |
with open("qualitative_responses.csv", "a") as f: | |
f.write(response + "\n") | |
st.success("Response recorded! β ") | |
# **Data Analysis & Visualization** | |
st.subheader("π Data Insights & Visualizations") | |
try: | |
data = pd.read_csv("data.csv") | |
if not data.empty: | |
# Show category distribution | |
st.write("### Microaggression Category Distribution") | |
category_counts = data["Category"].value_counts() | |
fig, ax = plt.subplots() | |
category_counts.plot(kind='bar', ax=ax) | |
st.pyplot(fig) | |
# Generate a word cloud | |
st.write("### Common Words in Microaggressions") | |
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(" ".join(data["Text"])) | |
fig, ax = plt.subplots() | |
ax.imshow(wordcloud, interpolation="bilinear") | |
ax.axis("off") | |
st.pyplot(fig) | |
# **Correlation Heatmap** | |
st.write("### Correlation Between Sentiment, Acceptability, and Impact") | |
plt.figure(figsize=(6, 4)) | |
sns.heatmap(data[["Sentiment", "Acceptability", "Impact"]].corr(), annot=True, cmap="coolwarm", fmt=".2f") | |
st.pyplot(plt) | |
# **Show Data Table** | |
if st.checkbox("Show Data Table"): | |
st.dataframe(data) | |
except FileNotFoundError: | |
st.write("No data available yet. Submit responses to populate insights.") | |
# **Download Data Option for Researchers** | |
st.subheader("π₯ Download Data for Research") | |
if os.path.exists("data.csv"): | |
with open("data.csv", "rb") as file: | |
st.download_button(label="Download CSV Data", data=file, file_name="microaggression_data.csv", mime="text/csv") | |
st.write("π¬ Developed as a research tool for understanding microaggressions in social psychology.") |