aeresd commited on
Commit
a8b7aaa
·
verified ·
1 Parent(s): 932e610

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -82
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("🧠 Settings")
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,
35
- device=0 if torch.cuda.is_available() else -1)
 
 
 
 
 
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
- st.title("🚨 Emoji Offensive Text Detector & Analysis")
69
- st.markdown("---")
70
 
71
- # 输入与分析
72
- st.header("✍️ Input & Moderation")
73
- def text_moderation_section():
74
- st.markdown("Enter text with emojis or upload an image with text.")
75
- text = st.text_area("Sentence (or OCR text will appear here):", height=120)
 
76
 
77
- uploaded_file = st.file_uploader("Or upload an image for OCR:", type=["jpg", "jpeg", "png"])
78
- if uploaded_file:
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.subheader("🔄 Translated Text")
91
- st.code(translated)
92
- st.subheader(f"🎯 Prediction: {label}")
93
- st.write(f"Confidence: {score:.2%}")
94
- st.subheader("🧠 Explanation")
 
95
  st.info(reason)
 
96
  except Exception as e:
97
- st.error(f"Error during processing: {e}")
98
-
99
- # 分析仪表板
100
- st.markdown("---")
101
- st.header("📊 Violation Analysis")
102
- def analysis_dashboard():
103
- if not st.session_state.history:
104
- st.info("No data to display. Please analyze some text first.")
105
- return
106
-
107
- df = pd.DataFrame(st.session_state.history)
108
-
109
- # 建议列表
110
- st.subheader("📝 Offensive Terms & Suggestions")
111
- for item in st.session_state.history:
112
- st.markdown(f"- **Input:** {item['text']}")
113
- st.markdown(f" - Translated: {item['translated']}")
114
- st.markdown(f" - Label: {item['label']} ({item['score']:.2%})")
115
- st.markdown(f" - Suggestion: {item['reason']}")
116
-
117
- # 雷达图
118
- radar_df = pd.DataFrame({
119
- "Category": ["Insult", "Abuse", "Discrimination", "Hate Speech", "Vulgarity"],
120
- "Score": [0.7, 0.4, 0.3, 0.5, 0.6]
121
- })
122
- radar_fig = px.line_polar(
123
- radar_df,
124
- r='Score',
125
- theta='Category',
126
- line_close=True,
127
- title="⚠️ Risk Radar by Category"
128
- )
129
- radar_fig.update_traces(line_color='black')
130
- st.plotly_chart(radar_fig)
131
-
132
- # 渲染各部分
133
- text_moderation_section()
134
- analysis_dashboard()
 
 
 
 
 
 
 
 
 
 
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.")