Update app.py
Browse files
app.py
CHANGED
@@ -26,15 +26,19 @@ model_options = {
|
|
26 |
# ✅ 页面配置
|
27 |
st.set_page_config(page_title="Emoji Offensive Text Detector", page_icon="🚨", layout="wide")
|
28 |
|
29 |
-
# ✅
|
30 |
with st.sidebar:
|
31 |
-
st.header("🧠
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
# 初始化会话历史
|
38 |
if "history" not in st.session_state:
|
39 |
st.session_state.history = []
|
40 |
|
@@ -50,85 +54,75 @@ def classify_emoji_text(text: str):
|
|
50 |
result = classifier(translated_text)[0]
|
51 |
label = result["label"]
|
52 |
score = result["score"]
|
53 |
-
reasoning =
|
54 |
-
f"The sentence was flagged as '{label}' due to potentially offensive phrases. "
|
55 |
-
"Consider replacing emotionally charged, ambiguous, or abusive terms."
|
56 |
-
)
|
57 |
-
|
58 |
-
st.session_state.history.append({
|
59 |
-
"text": text,
|
60 |
-
"translated": translated_text,
|
61 |
-
"label": label,
|
62 |
-
"score": score,
|
63 |
-
"reason": reasoning
|
64 |
-
})
|
65 |
-
return translated_text, label, score, reasoning
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
st.markdown("---")
|
70 |
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
st.markdown("
|
75 |
-
|
|
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
image = Image.open(uploaded_file)
|
80 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
81 |
-
with st.spinner("Extracting text via OCR..."):
|
82 |
-
ocr_text = pytesseract.image_to_string(image, lang="chi_sim+eng").strip()
|
83 |
-
st.text_area("Extracted Text:", value=ocr_text, height=120)
|
84 |
-
text = ocr_text
|
85 |
-
|
86 |
-
if st.button("🚦 Analyze Text") and text:
|
87 |
-
with st.spinner("Processing..."):
|
88 |
try:
|
89 |
translated, label, score, reason = classify_emoji_text(text)
|
90 |
-
st.
|
91 |
-
st.code(translated)
|
92 |
-
|
93 |
-
st.
|
94 |
-
st.
|
|
|
95 |
st.info(reason)
|
|
|
96 |
except Exception as e:
|
97 |
-
st.error(f"
|
98 |
-
|
99 |
-
|
100 |
-
st.markdown("
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# ✅ 页面配置
|
27 |
st.set_page_config(page_title="Emoji Offensive Text Detector", page_icon="🚨", layout="wide")
|
28 |
|
29 |
+
# ✅ 页面布局
|
30 |
with st.sidebar:
|
31 |
+
st.header("🧠 Navigation")
|
32 |
+
section = st.radio("Select Mode:", ["📍 Text Moderation", "📊 Text Analysis"])
|
33 |
+
|
34 |
+
if section == "📍 Text Moderation":
|
35 |
+
selected_model = st.selectbox("Choose classification model", list(model_options.keys()))
|
36 |
+
selected_model_id = model_options[selected_model]
|
37 |
+
classifier = pipeline("text-classification", model=selected_model_id, device=0 if torch.cuda.is_available() else -1)
|
38 |
+
|
39 |
+
elif section == "📊 Text Analysis":
|
40 |
+
st.markdown("You can view the violation distribution chart and editing suggestions.")
|
41 |
|
|
|
42 |
if "history" not in st.session_state:
|
43 |
st.session_state.history = []
|
44 |
|
|
|
54 |
result = classifier(translated_text)[0]
|
55 |
label = result["label"]
|
56 |
score = result["score"]
|
57 |
+
reasoning = f"The sentence was flagged as '{label}' due to potentially offensive phrases. Consider replacing emotionally charged, ambiguous, or abusive terms."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
st.session_state.history.append({"text": text, "translated": translated_text, "label": label, "score": score, "reason": reasoning})
|
60 |
+
return translated_text, label, score, reasoning
|
|
|
61 |
|
62 |
+
# ✅ Section logic
|
63 |
+
if section == "📍 Text Moderation":
|
64 |
+
st.title("📍 Offensive Text Classification")
|
65 |
+
st.markdown("### ✍️ Input your sentence:")
|
66 |
+
default_text = "你是🐷"
|
67 |
+
text = st.text_area("Enter sentence with emojis:", value=default_text, height=150)
|
68 |
|
69 |
+
if st.button("🚦 Analyze"):
|
70 |
+
with st.spinner("🔍 Processing..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
try:
|
72 |
translated, label, score, reason = classify_emoji_text(text)
|
73 |
+
st.markdown("### 🔄 Translated sentence:")
|
74 |
+
st.code(translated, language="text")
|
75 |
+
|
76 |
+
st.markdown(f"### 🎯 Prediction: {label}")
|
77 |
+
st.markdown(f"### 📊 Confidence Score: {score:.2%}")
|
78 |
+
st.markdown(f"### 🧠 Model Explanation:")
|
79 |
st.info(reason)
|
80 |
+
|
81 |
except Exception as e:
|
82 |
+
st.error(f"❌ An error occurred during processing:\n\n{e}")
|
83 |
+
|
84 |
+
st.markdown("---")
|
85 |
+
st.markdown("### 🖼️ Or upload a screenshot of bullet comments:")
|
86 |
+
|
87 |
+
uploaded_file = st.file_uploader("Upload an image (JPG/PNG)", type=["jpg", "jpeg", "png"])
|
88 |
+
|
89 |
+
if uploaded_file is not None:
|
90 |
+
image = Image.open(uploaded_file)
|
91 |
+
st.image(image, caption="Uploaded Screenshot", use_column_width=True)
|
92 |
+
|
93 |
+
with st.spinner("🧠 Extracting text via OCR..."):
|
94 |
+
ocr_text = pytesseract.image_to_string(image, lang="chi_sim+eng")
|
95 |
+
st.markdown("#### 📋 Extracted Text:")
|
96 |
+
st.code(ocr_text.strip())
|
97 |
+
|
98 |
+
translated, label, score, reason = classify_emoji_text(ocr_text.strip())
|
99 |
+
st.markdown("### 🔄 Translated sentence:")
|
100 |
+
st.code(translated, language="text")
|
101 |
+
|
102 |
+
st.markdown(f"### 🎯 Prediction: {label}")
|
103 |
+
st.markdown(f"### 📊 Confidence Score: {score:.2%}")
|
104 |
+
st.markdown("### 🧠 Model Explanation:")
|
105 |
+
st.info(reason)
|
106 |
+
|
107 |
+
elif section == "📊 Text Analysis":
|
108 |
+
st.title("📊 Violation Analysis Dashboard")
|
109 |
+
if st.session_state.history:
|
110 |
+
df = pd.DataFrame(st.session_state.history)
|
111 |
+
# 已移除 Offensive Category Distribution 饼图
|
112 |
+
|
113 |
+
st.markdown("### 🧾 Offensive Terms & Suggestions")
|
114 |
+
for item in st.session_state.history:
|
115 |
+
st.markdown(f"- 🔹 **Input:** {item['text']}")
|
116 |
+
st.markdown(f" - ✨ **Translated:** {item['translated']}")
|
117 |
+
st.markdown(f" - ❗ **Label:** {item['label']} with **{item['score']:.2%}** confidence")
|
118 |
+
st.markdown(f" - 🔧 **Suggestion:** {item['reason']}")
|
119 |
+
|
120 |
+
radar_df = pd.DataFrame({
|
121 |
+
"Category": ["Insult", "Abuse", "Discrimination", "Hate Speech", "Vulgarity"],
|
122 |
+
"Score": [0.7, 0.4, 0.3, 0.5, 0.6]
|
123 |
+
})
|
124 |
+
radar_fig = px.line_polar(radar_df, r='Score', theta='Category', line_close=True, title="⚠️ Risk Radar by Category")
|
125 |
+
radar_fig.update_traces(line_color='black') # 将雷达图线条改为黑色
|
126 |
+
st.plotly_chart(radar_fig)
|
127 |
+
else:
|
128 |
+
st.info("⚠️ No classification data available yet.")
|