Update app.py
Browse files
app.py
CHANGED
@@ -26,22 +26,18 @@ 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 |
-
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 |
|
|
|
45 |
|
46 |
def classify_emoji_text(text: str):
|
47 |
prompt = f"输入:{text}\n输出:"
|
@@ -59,70 +55,69 @@ def classify_emoji_text(text: str):
|
|
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 |
-
#
|
63 |
-
|
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 |
-
|
90 |
-
|
91 |
-
|
|
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
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"
|
103 |
-
st.markdown(
|
104 |
-
st.markdown("### 🧠 Model Explanation:")
|
105 |
st.info(reason)
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
"
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("🧠 Configuration")
|
32 |
+
selected_model = st.selectbox("Choose classification model", list(model_options.keys()))
|
33 |
+
selected_model_id = model_options[selected_model]
|
34 |
+
classifier = pipeline("text-classification", model=selected_model_id, device=0 if torch.cuda.is_available() else -1)
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# 初始化历史记录
|
37 |
if "history" not in st.session_state:
|
38 |
st.session_state.history = []
|
39 |
|
40 |
+
# 分类函数
|
41 |
|
42 |
def classify_emoji_text(text: str):
|
43 |
prompt = f"输入:{text}\n输出:"
|
|
|
55 |
st.session_state.history.append({"text": text, "translated": translated_text, "label": label, "score": score, "reason": reasoning})
|
56 |
return translated_text, label, score, reasoning
|
57 |
|
58 |
+
# 主页面:输入与分析共存
|
59 |
+
st.title("🚨 Emoji Offensive Text Detector & Analysis Dashboard")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
# 文本输入
|
62 |
+
st.subheader("1. 输入与分类")
|
63 |
+
default_text = "你是🐷"
|
64 |
+
text = st.text_area("Enter sentence with emojis:", value=default_text, height=150)
|
65 |
|
66 |
+
if st.button("🚦 Analyze Text"):
|
67 |
+
with st.spinner("🔍 Processing..."):
|
68 |
+
try:
|
69 |
+
translated, label, score, reason = classify_emoji_text(text)
|
70 |
+
st.markdown("**Translated sentence:**")
|
|
|
|
|
71 |
st.code(translated, language="text")
|
72 |
+
st.markdown(f"**Prediction:** {label}")
|
73 |
+
st.markdown(f"**Confidence Score:** {score:.2%}")
|
74 |
+
st.markdown("**Model Explanation:**")
|
|
|
75 |
st.info(reason)
|
76 |
+
except Exception as e:
|
77 |
+
st.error(f"❌ An error occurred:\n{e}")
|
78 |
+
|
79 |
+
# 图片上传与 OCR
|
80 |
+
st.markdown("---")
|
81 |
+
st.subheader("2. 图片 OCR & 分类")
|
82 |
+
uploaded_file = st.file_uploader("Upload an image (JPG/PNG)", type=["jpg","jpeg","png"])
|
83 |
+
if uploaded_file:
|
84 |
+
image = Image.open(uploaded_file)
|
85 |
+
st.image(image, caption="Uploaded Screenshot", use_column_width=True)
|
86 |
+
with st.spinner("🧠 Extracting text via OCR..."):
|
87 |
+
ocr_text = pytesseract.image_to_string(image, lang="chi_sim+eng").strip()
|
88 |
+
if ocr_text:
|
89 |
+
st.markdown("**Extracted Text:**")
|
90 |
+
st.code(ocr_text)
|
91 |
+
translated, label, score, reason = classify_emoji_text(ocr_text)
|
92 |
+
st.markdown("**Translated sentence:**")
|
93 |
+
st.code(translated, language="text")
|
94 |
+
st.markdown(f"**Prediction:** {label}")
|
95 |
+
st.markdown(f"**Confidence Score:** {score:.2%}")
|
96 |
+
st.markdown("**Model Explanation:**")
|
97 |
+
st.info(reason)
|
98 |
+
else:
|
99 |
+
st.info("⚠️ No text detected in the image.")
|
100 |
+
|
101 |
+
# 分析仪表盘
|
102 |
+
st.markdown("---")
|
103 |
+
st.subheader("3. Violation Analysis Dashboard")
|
104 |
+
if st.session_state.history:
|
105 |
+
# 展示历史记录
|
106 |
+
df = pd.DataFrame(st.session_state.history)
|
107 |
+
st.markdown("### 🧾 Offensive Terms & Suggestions")
|
108 |
+
for item in st.session_state.history:
|
109 |
+
st.markdown(f"- 🔹 **Input:** {item['text']}")
|
110 |
+
st.markdown(f" - ✨ **Translated:** {item['translated']}")
|
111 |
+
st.markdown(f" - ❗ **Label:** {item['label']} with **{item['score']:.2%}** confidence")
|
112 |
+
st.markdown(f" - 🔧 **Suggestion:** {item['reason']}")
|
113 |
+
|
114 |
+
# 雷达图
|
115 |
+
radar_df = pd.DataFrame({
|
116 |
+
"Category": ["Insult","Abuse","Discrimination","Hate Speech","Vulgarity"],
|
117 |
+
"Score": [0.7,0.4,0.3,0.5,0.6]
|
118 |
+
})
|
119 |
+
radar_fig = px.line_polar(radar_df, r='Score', theta='Category', line_close=True, title="⚠️ Risk Radar by Category")
|
120 |
+
radar_fig.update_traces(line_color='black')
|
121 |
+
st.plotly_chart(radar_fig)
|
122 |
+
else:
|
123 |
+
st.info("⚠️ No classification data available yet.")
|