aeresd commited on
Commit
e375d90
·
verified ·
1 Parent(s): 9284803

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -49
app.py CHANGED
@@ -38,6 +38,7 @@ if "history" not in st.session_state:
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,18 +50,9 @@ def classify_emoji_text(text: str):
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
  # 主页面:输入与分析共存
@@ -86,7 +78,7 @@ if st.button("🚦 Analyze Text"):
86
 
87
  # 图片上传与 OCR
88
  st.markdown("---")
89
- st.subheader("2. 图片 OCR & 分类")
90
  uploaded_file = st.file_uploader("Upload an image (JPG/PNG)", type=["jpg","jpeg","png"])
91
  if uploaded_file:
92
  image = Image.open(uploaded_file)
@@ -127,42 +119,5 @@ if st.session_state.history:
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.")
 
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
  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
  # 主页面:输入与分析共存
 
78
 
79
  # 图片上传与 OCR
80
  st.markdown("---")
81
+ st.subheader("2. Image 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)
 
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.")