Spaces:
Sleeping
Sleeping
Update predictor.py
Browse files- predictor.py +158 -155
predictor.py
CHANGED
|
@@ -1,155 +1,158 @@
|
|
| 1 |
-
# predictor.py
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
-
import re
|
| 5 |
-
import os
|
| 6 |
-
import json
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
from
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
'
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
sentence = re.sub(
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
if
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
overall_stats
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# predictor.py
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import re
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import onnxruntime as ort
|
| 9 |
+
from collections import defaultdict, Counter
|
| 10 |
+
from difflib import SequenceMatcher
|
| 11 |
+
from transformers import BertTokenizerFast
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class DialogueEvaluator:
|
| 15 |
+
def __init__(self, model_path, keywords_path):
|
| 16 |
+
print("Initializing DialogueEvaluator...")
|
| 17 |
+
|
| 18 |
+
# 加载模型和tokenizer
|
| 19 |
+
self.tokenizer, self.model, self.id2label = self._load_model(model_path)
|
| 20 |
+
print("✅ Model and Tokenizer loaded.")
|
| 21 |
+
|
| 22 |
+
# 加载关键词体系
|
| 23 |
+
with open(keywords_path, 'r', encoding='utf-8') as f:
|
| 24 |
+
self.academic_keywords = json.load(f)
|
| 25 |
+
print("✅ Keywords loaded.")
|
| 26 |
+
|
| 27 |
+
# 构建关键词正则
|
| 28 |
+
self.keyword_patterns = self._build_keyword_patterns()
|
| 29 |
+
print("✅ Keyword patterns built.")
|
| 30 |
+
|
| 31 |
+
# 场景化停用词
|
| 32 |
+
self.scene_stopwords = r'^(嗯|啊|哦|呃|呐|哟)'
|
| 33 |
+
print("DialogueEvaluator initialized successfully.")
|
| 34 |
+
|
| 35 |
+
def _load_model(self, model_path):
|
| 36 |
+
model_path = Path(model_path)
|
| 37 |
+
model_dir = model_path.parent
|
| 38 |
+
|
| 39 |
+
tokenizer = BertTokenizerFast.from_pretrained(model_dir)
|
| 40 |
+
|
| 41 |
+
sess_options = ort.SessionOptions()
|
| 42 |
+
providers = ['CPUExecutionProvider'] # Hugging Face Spaces 通常无 GPU
|
| 43 |
+
model = ort.InferenceSession(str(model_path), sess_options, providers=providers)
|
| 44 |
+
print(f"ℹ️ ONNX Runtime using: {model.get_providers()[0]}")
|
| 45 |
+
|
| 46 |
+
label_map_path = model_dir / "label_map.json"
|
| 47 |
+
if not label_map_path.exists():
|
| 48 |
+
raise FileNotFoundError(f"Missing label_map.json at: {label_map_path}")
|
| 49 |
+
|
| 50 |
+
with open(label_map_path, 'r', encoding='utf-8') as f:
|
| 51 |
+
label_map = json.load(f)
|
| 52 |
+
id2label = {int(k): v for k, v in label_map['id2label'].items()}
|
| 53 |
+
return tokenizer, model, id2label
|
| 54 |
+
|
| 55 |
+
def _build_keyword_patterns(self):
|
| 56 |
+
patterns = {}
|
| 57 |
+
for scene, sentiment_dict in self.academic_keywords.items():
|
| 58 |
+
for sentiment, keywords in sentiment_dict.items():
|
| 59 |
+
for keyword in keywords:
|
| 60 |
+
pattern = self._create_fuzzy_pattern(keyword)
|
| 61 |
+
patterns[keyword] = {
|
| 62 |
+
'pattern': pattern,
|
| 63 |
+
'scene': scene,
|
| 64 |
+
'sentiment': sentiment
|
| 65 |
+
}
|
| 66 |
+
return patterns
|
| 67 |
+
|
| 68 |
+
def _create_fuzzy_pattern(self, keyword):
|
| 69 |
+
if len(keyword) <= 2:
|
| 70 |
+
return re.compile(re.escape(keyword))
|
| 71 |
+
pattern_str = re.escape(keyword[0]) + ''.join([f"{re.escape(c)}.?" for c in keyword[1:]])
|
| 72 |
+
return re.compile(pattern_str)
|
| 73 |
+
|
| 74 |
+
def _fuzzy_match_keywords(self, sentence):
|
| 75 |
+
matched_info = []
|
| 76 |
+
for keyword, info in self.keyword_patterns.items():
|
| 77 |
+
if info['pattern'].search(sentence):
|
| 78 |
+
sentiment = info['sentiment']
|
| 79 |
+
if re.search(fr'(不|没有|无|否|缺乏|不足|不够){keyword}', sentence):
|
| 80 |
+
sentiment = 'negative'
|
| 81 |
+
|
| 82 |
+
matched_info.append({
|
| 83 |
+
'keyword': keyword,
|
| 84 |
+
'scene': info['scene'],
|
| 85 |
+
'sentiment': sentiment
|
| 86 |
+
})
|
| 87 |
+
return matched_info
|
| 88 |
+
|
| 89 |
+
def _clean_sentence(self, sentence):
|
| 90 |
+
sentence = re.sub(r'[^\w\s\u4e00-\u9fff,。;:、]', '', sentence)
|
| 91 |
+
sentence = re.sub(r'\s+', ' ', sentence).strip()
|
| 92 |
+
sentence = re.sub(self.scene_stopwords, '', sentence)
|
| 93 |
+
return sentence
|
| 94 |
+
|
| 95 |
+
def _extract_key_sentences(self, text):
|
| 96 |
+
sentences = re.split(r'[。!?;\n]', text)
|
| 97 |
+
key_sentences = []
|
| 98 |
+
for sent in sentences:
|
| 99 |
+
if len(sent) < 5:
|
| 100 |
+
continue
|
| 101 |
+
clean_sent = self._clean_sentence(sent)
|
| 102 |
+
if not clean_sent:
|
| 103 |
+
continue
|
| 104 |
+
matched_info = self._fuzzy_match_keywords(clean_sent)
|
| 105 |
+
if matched_info:
|
| 106 |
+
key_sentences.append({
|
| 107 |
+
'sentence': clean_sent,
|
| 108 |
+
'matched_info': matched_info,
|
| 109 |
+
})
|
| 110 |
+
return key_sentences
|
| 111 |
+
|
| 112 |
+
def _predict_sentence(self, sentence):
|
| 113 |
+
inputs = self.tokenizer(
|
| 114 |
+
sentence, truncation=True, padding='max_length', max_length=128, return_tensors="np"
|
| 115 |
+
)
|
| 116 |
+
ort_inputs = {'input_ids': inputs['input_ids'], 'attention_mask': inputs['attention_mask']}
|
| 117 |
+
try:
|
| 118 |
+
outputs = self.model.run(None, ort_inputs)
|
| 119 |
+
logits = outputs[0]
|
| 120 |
+
probs = torch.softmax(torch.tensor(logits), dim=1)
|
| 121 |
+
pred_id = torch.argmax(probs).item()
|
| 122 |
+
return {
|
| 123 |
+
'label': self.id2label[pred_id],
|
| 124 |
+
'confidence': round(torch.max(probs).item(), 4)
|
| 125 |
+
}
|
| 126 |
+
except Exception as e:
|
| 127 |
+
print(f"❌ Inference failed for sentence: '{sentence}'. Error: {str(e)}")
|
| 128 |
+
return {'label': 'ERROR', 'confidence': 0.0}
|
| 129 |
+
|
| 130 |
+
def evaluate_full_text(self, text):
|
| 131 |
+
key_sentences_info = self._extract_key_sentences(text)
|
| 132 |
+
if not key_sentences_info:
|
| 133 |
+
return {'status': 'no_key_sentences', 'message': '未检测到包含评价关键词的有效句子。'}
|
| 134 |
+
|
| 135 |
+
processed_sentences = []
|
| 136 |
+
for sent_info in key_sentences_info:
|
| 137 |
+
prediction = self._predict_sentence(sent_info['sentence'])
|
| 138 |
+
sent_info.update(prediction)
|
| 139 |
+
processed_sentences.append(sent_info)
|
| 140 |
+
|
| 141 |
+
overall_stats = defaultdict(lambda: defaultdict(int))
|
| 142 |
+
all_labels = [sent['label'] for sent in processed_sentences]
|
| 143 |
+
overall_stats['total_sentences'] = len(processed_sentences)
|
| 144 |
+
overall_stats['label_distribution'] = dict(Counter(all_labels))
|
| 145 |
+
overall_stats['avg_confidence'] = round(
|
| 146 |
+
sum(s['confidence'] for s in processed_sentences) / len(processed_sentences),
|
| 147 |
+
4) if processed_sentences else 0
|
| 148 |
+
|
| 149 |
+
for sent in processed_sentences:
|
| 150 |
+
for info in sent['matched_info']:
|
| 151 |
+
overall_stats['scene_distribution'][info['scene']] += 1
|
| 152 |
+
overall_stats['sentiment_distribution'][info['sentiment']] += 1
|
| 153 |
+
|
| 154 |
+
return {
|
| 155 |
+
'status': 'success',
|
| 156 |
+
'overall_stats': dict(overall_stats),
|
| 157 |
+
'key_sentences': processed_sentences
|
| 158 |
+
}
|