aeresd commited on
Commit
5464ca6
·
verified ·
1 Parent(s): c60aa43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -24
app.py CHANGED
@@ -1,30 +1,39 @@
1
- import streamlit as st
2
- from transformers import pipeline
3
 
4
- # Load the text classification model pipeline
5
- classifier = pipeline("text-classification",model='isom5240ust/bert-base-uncased-emotion', return_all_scores=True)
 
 
 
 
 
 
 
6
 
7
- # Streamlit application title
8
- st.title("Text Classification for you")
9
- st.write("Classification for 6 emotions: sadness, joy, love, anger, fear, surprise")
10
 
11
- # Text input for user to enter the text to classify
12
- text = st.text_area("Enter the text to classify", "")
 
 
 
 
 
 
 
 
 
13
 
14
- # Perform text classification when the user clicks the "Classify" button
15
- if st.button("Classify"):
16
- # Perform text classification on the input text
17
- results = classifier(text)[0]
 
18
 
19
- # Display the classification result
20
- max_score = float('-inf')
21
- max_label = ''
22
 
23
- for result in results:
24
- if result['score'] > max_score:
25
- max_score = result['score']
26
- max_label = result['label']
27
-
28
- st.write("Text:", text)
29
- st.write("Label:", max_label)
30
- st.write("Score:", max_score)
 
1
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
2
+ import torch
3
 
4
+ # Step 1: Emoji 翻译模型(你自己训练的模型)
5
+ emoji_model_id = "JenniferHJF/qwen1.5-emoji-finetuned"
6
+ emoji_tokenizer = AutoTokenizer.from_pretrained(emoji_model_id, trust_remote_code=True)
7
+ emoji_model = AutoModelForCausalLM.from_pretrained(
8
+ emoji_model_id,
9
+ trust_remote_code=True,
10
+ torch_dtype=torch.float16
11
+ ).to("cuda" if torch.cuda.is_available() else "cpu")
12
+ emoji_model.eval()
13
 
14
+ # Step 2: 冒犯性文本识别模型
15
+ classifier = pipeline("text-classification", model="unitary/toxic-bert", device=0 if torch.cuda.is_available() else -1)
 
16
 
17
+ def classify_emoji_text(text: str):
18
+ """
19
+ Step 1: 翻译文本中的 emoji
20
+ Step 2: 使用分类器判断是否冒犯
21
+ """
22
+ prompt = f"""请判断下面的文本是否具有冒犯性。
23
+ 这里的“冒犯性”主要指包含人身攻击、侮辱、歧视、仇恨言论或极端粗俗的内容。
24
+ 如果文本具有冒犯性,请仅回复冒犯;如果不具有冒犯性,请仅回复不冒犯。
25
+ 文本如下:
26
+ {text}
27
+ """
28
 
29
+ input_ids = emoji_tokenizer(prompt, return_tensors="pt").to(emoji_model.device)
30
+ with torch.no_grad():
31
+ output_ids = emoji_model.generate(**input_ids, max_new_tokens=50, do_sample=False)
32
+ decoded = emoji_tokenizer.decode(output_ids[0], skip_special_tokens=True)
33
+ translated_text = decoded.strip().split("文本如下:")[-1].strip()
34
 
35
+ result = classifier(translated_text)[0]
36
+ label = result["label"]
37
+ score = result["score"]
38
 
39
+ return translated_text, label, score