Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,31 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
""
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|