Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +97 -40
src/streamlit_app.py
CHANGED
|
@@ -1,63 +1,120 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from datasets import load_dataset
|
| 3 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
def load_data():
|
| 12 |
-
ds = load_dataset("aug6th/streamlit_sample", split="train")
|
| 13 |
-
return ds
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
col1.metric("์ด ์ํ ์",
|
| 24 |
-
col2.metric("
|
| 25 |
-
col3.metric("
|
|
|
|
| 26 |
|
| 27 |
st.markdown("---")
|
| 28 |
|
| 29 |
-
# ---
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
st.markdown("---")
|
| 38 |
|
| 39 |
-
# ---
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
st.markdown("---")
|
| 52 |
|
| 53 |
-
# ---
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
if
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
from collections import Counter
|
| 6 |
+
from wordcloud import WordCloud
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
# -----------------------------
|
| 10 |
+
# Page Config
|
| 11 |
+
# -----------------------------
|
| 12 |
+
st.set_page_config(
|
| 13 |
+
page_title="Korean Hate Speech Dashboard",
|
| 14 |
+
layout="wide",
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
st.title("๐ฐ๐ท Korean Hate Speech Analytics Dashboard")
|
| 18 |
+
|
| 19 |
+
# -----------------------------
|
| 20 |
+
# Load Dataset (cached)
|
| 21 |
+
# -----------------------------
|
| 22 |
+
@st.cache_data
|
| 23 |
+
def load_data():
|
| 24 |
+
ds = load_dataset("jeanlee/kmhas_korean_hate_speech")
|
| 25 |
+
df = pd.DataFrame(ds["train"])
|
| 26 |
+
return df
|
| 27 |
|
| 28 |
+
df = load_data()
|
| 29 |
|
| 30 |
+
# -----------------------------
|
| 31 |
+
# Preprocessing
|
| 32 |
+
# -----------------------------
|
| 33 |
+
df["length"] = df["text"].apply(len)
|
| 34 |
|
| 35 |
+
# Multi-label explode
|
| 36 |
+
df_exploded = df.explode("label")
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# -----------------------------
|
| 39 |
+
# KPI Section
|
| 40 |
+
# -----------------------------
|
| 41 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 42 |
|
| 43 |
+
total_samples = len(df)
|
| 44 |
+
avg_length = df["length"].mean()
|
| 45 |
|
| 46 |
+
label_counts = df_exploded["label"].value_counts()
|
| 47 |
+
top_label = label_counts.idxmax()
|
| 48 |
+
hate_ratio = 1 - (label_counts.get("clean", 0) / total_samples)
|
| 49 |
|
| 50 |
+
col1.metric("์ด ์ํ ์", f"{total_samples:,}")
|
| 51 |
+
col2.metric("ํ์ค ๋น์จ", f"{hate_ratio:.2%}")
|
| 52 |
+
col3.metric("ํ๊ท ํ
์คํธ ๊ธธ์ด", f"{avg_length:.1f}")
|
| 53 |
+
col4.metric("์ต๋ค ๋ผ๋ฒจ", top_label)
|
| 54 |
|
| 55 |
st.markdown("---")
|
| 56 |
|
| 57 |
+
# -----------------------------
|
| 58 |
+
# Charts Section
|
| 59 |
+
# -----------------------------
|
| 60 |
+
left, right = st.columns(2)
|
| 61 |
+
|
| 62 |
+
with left:
|
| 63 |
+
fig1 = px.bar(
|
| 64 |
+
label_counts,
|
| 65 |
+
x=label_counts.index,
|
| 66 |
+
y=label_counts.values,
|
| 67 |
+
title="๋ผ๋ฒจ ๋ถํฌ",
|
| 68 |
+
)
|
| 69 |
+
st.plotly_chart(fig1, use_container_width=True)
|
| 70 |
+
|
| 71 |
+
with right:
|
| 72 |
+
fig2 = px.histogram(
|
| 73 |
+
df,
|
| 74 |
+
x="length",
|
| 75 |
+
nbins=50,
|
| 76 |
+
title="ํ
์คํธ ๊ธธ์ด ๋ถํฌ",
|
| 77 |
+
)
|
| 78 |
+
st.plotly_chart(fig2, use_container_width=True)
|
| 79 |
|
| 80 |
st.markdown("---")
|
| 81 |
|
| 82 |
+
# -----------------------------
|
| 83 |
+
# Label Filter Section
|
| 84 |
+
# -----------------------------
|
| 85 |
+
st.subheader("๐ ๋ผ๋ฒจ ํํฐ๋ง")
|
| 86 |
|
| 87 |
+
selected_label = st.selectbox(
|
| 88 |
+
"๋ผ๋ฒจ ์ ํ",
|
| 89 |
+
sorted(df_exploded["label"].unique())
|
| 90 |
+
)
|
| 91 |
|
| 92 |
+
filtered_df = df[df["label"].apply(lambda x: selected_label in x)]
|
| 93 |
+
|
| 94 |
+
st.write(f"์ ํ๋ ๋ผ๋ฒจ ์ํ ์: {len(filtered_df):,}")
|
| 95 |
+
st.dataframe(filtered_df[["text", "label"]].head(100), use_container_width=True)
|
| 96 |
|
| 97 |
st.markdown("---")
|
| 98 |
|
| 99 |
+
# -----------------------------
|
| 100 |
+
# WordCloud Section
|
| 101 |
+
# -----------------------------
|
| 102 |
+
st.subheader("โ๏ธ Word Cloud")
|
| 103 |
|
| 104 |
+
text_data = " ".join(filtered_df["text"].tolist())
|
| 105 |
|
| 106 |
+
if len(text_data) > 0:
|
| 107 |
+
wordcloud = WordCloud(
|
| 108 |
+
font_path="/usr/share/fonts/truetype/nanum/NanumGothic.ttf",
|
| 109 |
+
background_color="white",
|
| 110 |
+
width=800,
|
| 111 |
+
height=400
|
| 112 |
+
).generate(text_data)
|
| 113 |
|
| 114 |
+
fig_wc, ax = plt.subplots()
|
| 115 |
+
ax.imshow(wordcloud, interpolation="bilinear")
|
| 116 |
+
ax.axis("off")
|
| 117 |
+
|
| 118 |
+
st.pyplot(fig_wc)
|
| 119 |
+
else:
|
| 120 |
+
st.info("ํด๋น ๋ผ๋ฒจ์ ๋ํ ํ
์คํธ๊ฐ ์์ต๋๋ค.")
|