JenniferHJF commited on
Commit
8c213e8
·
verified ·
1 Parent(s): 4eeb268

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -48
app.py CHANGED
@@ -1,48 +1,31 @@
1
- import streamlit as st
2
- from transformers import pipeline
3
-
4
- # Page setup
5
- st.set_page_config(page_title="Hate Speech Detector", page_icon="🚨", layout="centered")
6
- st.title("🚨 Hate Speech Classification Demo")
7
- st.markdown("""
8
- This app uses the fine-tuned model `JenniferHJF/qwen1.5-emoji-finetuned` to predict whether the given input text contains **offensive or hateful content**.
9
-
10
- The model returns:
11
- - `1` if the content is offensive
12
- - `0` if it is not offensive
13
-
14
- ⚠️ Note: This is a demo and the model may not be perfect in detecting nuanced or implicit hate speech.
15
- """)
16
-
17
- # Example inputs
18
- examples = [
19
- "You're a disgrace to this country.",
20
- "Hope you have a great day!",
21
- "Why are you even alive?",
22
- "That was really rude and uncalled for.",
23
- "You are amazing and smart!",
24
- "Get lost, nobody wants you here."
25
- ]
26
-
27
- selected_example = st.selectbox("📘 Choose an example sentence:", options=examples, index=0)
28
- text = st.text_area("📝 Or enter your own text below:", value=selected_example, height=150)
29
-
30
- if st.button("🚀 Analyze"):
31
- with st.spinner("Running model inference..."):
32
- classifier = pipeline("text-generation", model="JenniferHJF/qwen1.5-emoji-finetuned", max_new_tokens=20)
33
- output = classifier(f"""Please determine whether the following text is offensive.
34
- Reply with '1' for offensive, '0' for non-offensive.
35
- Text: {text}
36
- """)[0]["generated_text"]
37
-
38
- # Extract the last '0' or '1' from output
39
- prediction = "Unknown"
40
- if "1" in output.strip().splitlines()[-1]:
41
- prediction = "Offensive (1)"
42
- elif "0" in output.strip().splitlines()[-1]:
43
- prediction = "Non-Offensive (0)"
44
-
45
- st.markdown(f"### ✅ Prediction: `{prediction}`")
46
- st.code(output.strip(), language="text")
47
- else:
48
- st.info("👈 Enter text and click 'Analyze' to begin.")
 
1
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
2
+
3
+ # Load Qwen 微调模型用于 emoji 转换
4
+ emoji_translator = pipeline(
5
+ "text-generation",
6
+ model="JenniferHJF/qwen1.5-emoji-finetuned",
7
+ tokenizer="JenniferHJF/qwen1.5-emoji-finetuned",
8
+ max_new_tokens=20,
9
+ trust_remote_code=True
10
+ )
11
+
12
+ # Load zero-shot/offensive-classification model(可替换为 ChatGLM3、DeepSeek 等)
13
+ offensive_classifier = pipeline(
14
+ "text-classification",
15
+ model="s-nlp/roberta-offensive-language-detection" # 示例模型,可换大模型
16
+ )
17
+
18
+ # Unified prediction function
19
+ def classify_text_with_emoji(raw_text):
20
+ # Step 1: Convert emojis ➝ Chinese
21
+ prompt = f"输入:{raw_text}\n输出:"
22
+ converted = emoji_translator(prompt)[0]['generated_text']
23
+ # 拿最后一行当输出结果(避免生成前缀)
24
+ translated_text = converted.strip().splitlines()[-1]
25
+
26
+ # Step 2: Run classification
27
+ result = offensive_classifier(translated_text)[0]
28
+ label = result['label']
29
+ score = result['score']
30
+
31
+ return translated_text, label, score