JenniferHJF commited on
Commit
f1ce293
·
verified ·
1 Parent(s): c69f58d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -11
app.py CHANGED
@@ -19,31 +19,55 @@ model_options = {
19
  "BERT Emotion": "bhadresh-savani/bert-base-go-emotion"
20
  }
21
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Streamlit 侧边栏模型选择
23
  selected_model = st.sidebar.selectbox("Choose classification model", list(model_options.keys()))
24
  selected_model_id = model_options[selected_model]
25
-
26
  classifier = pipeline("text-classification", model=selected_model_id, device=0 if torch.cuda.is_available() else -1)
27
 
 
 
 
 
 
 
28
  def classify_emoji_text(text: str):
29
- """
30
- Step 1: 翻译文本中的 emoji
31
- Step 2: 使用分类器判断是否冒犯
32
- """
33
  prompt = f"输入:{text}\n输出:"
34
  input_ids = emoji_tokenizer(prompt, return_tensors="pt").to(emoji_model.device)
35
  with torch.no_grad():
36
  output_ids = emoji_model.generate(**input_ids, max_new_tokens=64, do_sample=False)
37
  decoded = emoji_tokenizer.decode(output_ids[0], skip_special_tokens=True)
38
-
39
- # 保留真正输出部分(移除 prompt)
40
- if "输出:" in decoded:
41
- translated_text = decoded.split("输出:")[-1].strip()
42
- else:
43
- translated_text = decoded.strip()
44
 
45
  result = classifier(translated_text)[0]
46
  label = result["label"]
47
  score = result["score"]
48
 
49
  return translated_text, label, score
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  "BERT Emotion": "bhadresh-savani/bert-base-go-emotion"
20
  }
21
 
22
+ # ✅ 页面配置
23
+ st.set_page_config(page_title="Emoji Offensive Text Detector", page_icon="🚨", layout="wide")
24
+
25
+ # ✅ 页面标题
26
+ st.title("🧠 Emoji-based Offensive Language Classifier")
27
+
28
+ st.markdown("""
29
+ This application translates emojis in a sentence and classifies whether the final sentence is offensive or not using two AI models.
30
+ - The **first model** translates emoji or symbolic phrases into standard Chinese text.
31
+ - The **second model** performs offensive language detection.
32
+ """)
33
+
34
  # Streamlit 侧边栏模型选择
35
  selected_model = st.sidebar.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
+ # ✅ 输入区域
40
+ st.markdown("### ✍️ Input your sentence:")
41
+ default_text = "你是🐷"
42
+ text = st.text_area("Enter sentence with emojis:", value=default_text, height=150)
43
+
44
+ # ✅ 主逻辑封装函数
45
  def classify_emoji_text(text: str):
 
 
 
 
46
  prompt = f"输入:{text}\n输出:"
47
  input_ids = emoji_tokenizer(prompt, return_tensors="pt").to(emoji_model.device)
48
  with torch.no_grad():
49
  output_ids = emoji_model.generate(**input_ids, max_new_tokens=64, do_sample=False)
50
  decoded = emoji_tokenizer.decode(output_ids[0], skip_special_tokens=True)
51
+ translated_text = decoded.split("输出:")[-1].strip() if "输出:" in decoded else decoded.strip()
 
 
 
 
 
52
 
53
  result = classifier(translated_text)[0]
54
  label = result["label"]
55
  score = result["score"]
56
 
57
  return translated_text, label, score
58
+
59
+ # ✅ 触发按钮
60
+ if st.button("🚦 Analyze"):
61
+ with st.spinner("🔍 Processing..."):
62
+ try:
63
+ translated, label, score = classify_emoji_text(text)
64
+ st.markdown("### 🔄 Translated sentence:")
65
+ st.code(translated, language="text")
66
+
67
+ st.markdown(f"### 🎯 Prediction: `{label}`")
68
+ st.markdown(f"### 📊 Confidence Score: `{score:.2%}`")
69
+
70
+ except Exception as e:
71
+ st.error(f"❌ An error occurred during processing:\n\n{e}")
72
+ else:
73
+ st.info("👈 Please input text and click the button to classify.")