aeresd commited on
Commit
896a453
·
verified ·
1 Parent(s): 180b7cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -14
app.py CHANGED
@@ -38,7 +38,6 @@ 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输出:"
44
  input_ids = emoji_tokenizer(prompt, return_tensors="pt").to(emoji_model.device)
@@ -50,9 +49,18 @@ def classify_emoji_text(text: str):
50
  result = classifier(translated_text)[0]
51
  label = result["label"]
52
  score = result["score"]
53
- reasoning = f"The sentence was flagged as '{label}' due to potentially offensive phrases. Consider replacing emotionally charged, ambiguous, or abusive terms."
54
-
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
  # 主页面:输入与分析共存
@@ -119,38 +127,42 @@ if st.session_state.history:
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.")
124
- # 單詞冒犯性分析模塊
125
  st.markdown("### 🧬 Word-level Offensive Correlation")
126
 
 
127
  last_translated_text = st.session_state.history[-1]["translated"]
128
  words = last_translated_text.split()
129
 
 
130
  word_scores = []
131
  for word in words:
132
  try:
133
- result = classifier(word)[0]
134
  word_scores.append({
135
  "Word": word,
136
- "Label": result["label"],
137
- "Score": result["score"]
138
  })
139
- except:
140
  continue
141
 
142
  if word_scores:
143
  word_df = pd.DataFrame(word_scores)
144
  word_df = word_df.sort_values(by="Score", ascending=False).reset_index(drop=True)
145
 
146
- # 顯示前5個,隱藏表格邊框
147
  max_display = 5
148
- show_more = st.toggle("Show more words")
 
149
 
150
  display_df = word_df if show_more else word_df.head(max_display)
 
151
  st.markdown(
152
  display_df.to_html(index=False, border=0),
153
  unsafe_allow_html=True
154
  )
155
  else:
156
- st.info("❕ No word-level analysis available.")
 
 
 
38
  st.session_state.history = []
39
 
40
  # 分类函数
 
41
  def classify_emoji_text(text: str):
42
  prompt = f"输入:{text}\n输出:"
43
  input_ids = emoji_tokenizer(prompt, return_tensors="pt").to(emoji_model.device)
 
49
  result = classifier(translated_text)[0]
50
  label = result["label"]
51
  score = result["score"]
52
+ reasoning = (
53
+ f"The sentence was flagged as '{label}' due to potentially offensive phrases. "
54
+ "Consider replacing emotionally charged, ambiguous, or abusive terms."
55
+ )
56
+
57
+ st.session_state.history.append({
58
+ "text": text,
59
+ "translated": translated_text,
60
+ "label": label,
61
+ "score": score,
62
+ "reason": reasoning
63
+ })
64
  return translated_text, label, score, reasoning
65
 
66
  # 主页面:输入与分析共存
 
127
  radar_fig = px.line_polar(radar_df, r='Score', theta='Category', line_close=True, title="⚠️ Risk Radar by Category")
128
  radar_fig.update_traces(line_color='black')
129
  st.plotly_chart(radar_fig)
130
+
131
+ # —— 新增:单词级冒犯性相关性分析 —— #
 
132
  st.markdown("### 🧬 Word-level Offensive Correlation")
133
 
134
+ # 取最近一次翻译文本,按空格拆分单词
135
  last_translated_text = st.session_state.history[-1]["translated"]
136
  words = last_translated_text.split()
137
 
138
+ # 对每个单词进行分类并收集分数
139
  word_scores = []
140
  for word in words:
141
  try:
142
+ res = classifier(word)[0]
143
  word_scores.append({
144
  "Word": word,
145
+ "Label": res["label"],
146
+ "Score": res["score"]
147
  })
148
+ except Exception:
149
  continue
150
 
151
  if word_scores:
152
  word_df = pd.DataFrame(word_scores)
153
  word_df = word_df.sort_values(by="Score", ascending=False).reset_index(drop=True)
154
 
 
155
  max_display = 5
156
+ # Streamlit 1.22+ 支持 st.toggle,若版本不支持可改用 checkbox
157
+ show_more = st.toggle("Show more words", value=False)
158
 
159
  display_df = word_df if show_more else word_df.head(max_display)
160
+ # 隐藏边框并渲染 HTML 表格
161
  st.markdown(
162
  display_df.to_html(index=False, border=0),
163
  unsafe_allow_html=True
164
  )
165
  else:
166
+ st.info("❕ No word-level analysis available.")
167
+ else:
168
+ st.info("⚠️ No classification data available yet.")