predict_universal.py
14.7 KB
1
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
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
91
92
93
94
95
96
97
98
99
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Qwen3微博情感分析统一预测接口
支持0.6B、4B、8B三种规格的Embedding和LoRA模型
"""
import os
import sys
import argparse
import torch
from typing import List, Dict, Tuple, Any
# 添加当前目录到路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from models_config import MODEL_CONFIGS, MODEL_PATHS
from qwen3_embedding_universal import Qwen3EmbeddingUniversal
from qwen3_lora_universal import Qwen3LoRAUniversal
class Qwen3UniversalPredictor:
"""Qwen3统一预测器"""
def __init__(self):
self.models = {} # 存储已加载的模型 {model_key: {model: obj, display_name: str}}
def _get_model_key(self, model_type: str, model_size: str) -> str:
"""生成模型键值"""
return f"{model_type}_{model_size}"
def load_model(self, model_type: str, model_size: str) -> None:
"""加载指定的模型"""
if model_type not in ['embedding', 'lora']:
raise ValueError(f"不支持的模型类型: {model_type}")
if model_size not in ['0.6B', '4B', '8B']:
raise ValueError(f"不支持的模型大小: {model_size}")
model_path = MODEL_PATHS[model_type][model_size]
model_key = self._get_model_key(model_type, model_size)
# 检查训练好的模型文件是否存在
if not os.path.exists(model_path):
print(f"训练好的模型文件不存在: {model_path}")
print(f"请先训练 {model_type.upper()}-{model_size} 模型,或检查模型路径配置")
return
print(f"加载 {model_type.upper()}-{model_size} 模型...")
try:
if model_type == 'embedding':
model = Qwen3EmbeddingUniversal(model_size)
model.load_model(model_path)
else: # lora
model = Qwen3LoRAUniversal(model_size)
model.load_model(model_path)
self.models[model_key] = {
'model': model,
'display_name': f"Qwen3-{model_type.title()}-{model_size}"
}
print(f"{model_type.upper()}-{model_size} 模型加载成功")
except Exception as e:
print(f"加载 {model_type.upper()}-{model_size} 模型失败: {e}")
print(f"这可能是因为基础模型下载失败或训练好的模型文件损坏")
def load_all_models(self, model_dir: str = './models') -> None:
"""加载所有可用的模型"""
print("开始加载所有可用的Qwen3模型...")
loaded_count = 0
for model_type in ['embedding', 'lora']:
for model_size in ['0.6B', '4B', '8B']:
try:
self.load_model(model_type, model_size)
loaded_count += 1
except Exception as e:
print(f"跳过 {model_type}-{model_size}: {e}")
print(f"\n已加载 {loaded_count} 个模型")
self._print_loaded_models()
def load_specific_models(self, model_configs: List[Tuple[str, str]]) -> None:
"""加载指定的模型配置
Args:
model_configs: [(model_type, model_size), ...] 的列表
"""
print("加载指定的Qwen3模型...")
for model_type, model_size in model_configs:
try:
self.load_model(model_type, model_size)
except Exception as e:
print(f"跳过 {model_type}-{model_size}: {e}")
print(f"\n已加载 {len(self.models)} 个模型")
self._print_loaded_models()
def _print_loaded_models(self):
"""打印已加载的模型列表"""
if self.models:
print("已加载模型:")
for model_info in self.models.values():
print(f" - {model_info['display_name']}")
else:
print("没有成功加载任何模型")
def predict_single(self, text: str, model_key: str = None) -> Dict[str, Tuple[int, float]]:
"""单文本预测
Args:
text: 要预测的文本
model_key: 指定模型键值,None表示使用所有模型
Returns:
{model_name: (prediction, confidence), ...}
"""
results = {}
if model_key and model_key in self.models:
# 使用指定模型
model_info = self.models[model_key]
try:
prediction, confidence = model_info['model'].predict_single(text)
results[model_info['display_name']] = (prediction, confidence)
except Exception as e:
print(f"模型 {model_info['display_name']} 预测失败: {e}")
results[model_info['display_name']] = (0, 0.0)
else:
# 使用所有模型
for model_info in self.models.values():
try:
prediction, confidence = model_info['model'].predict_single(text)
results[model_info['display_name']] = (prediction, confidence)
except Exception as e:
print(f"模型 {model_info['display_name']} 预测失败: {e}")
results[model_info['display_name']] = (0, 0.0)
return results
def predict_batch(self, texts: List[str]) -> Dict[str, List[int]]:
"""批量预测"""
results = {}
for model_info in self.models.values():
try:
predictions = model_info['model'].predict(texts)
results[model_info['display_name']] = predictions
except Exception as e:
print(f"模型 {model_info['display_name']} 预测失败: {e}")
results[model_info['display_name']] = [0] * len(texts)
return results
def ensemble_predict(self, text: str) -> Tuple[int, float]:
"""集成预测"""
if len(self.models) < 2:
raise ValueError("集成预测需要至少2个模型")
results = self.predict_single(text)
# 加权平均(这里使用简单平均,可以根据模型性能调整权重)
total_weight = 0
weighted_prob = 0
for model_name, (pred, conf) in results.items():
if conf > 0: # 只考虑有效预测
prob = conf if pred == 1 else 1 - conf
weighted_prob += prob
total_weight += 1
if total_weight == 0:
return 0, 0.5
final_prob = weighted_prob / total_weight
final_pred = int(final_prob > 0.5)
final_conf = final_prob if final_pred == 1 else 1 - final_prob
return final_pred, final_conf
def _select_and_load_model(self):
"""让用户选择并加载模型"""
print("Qwen3微博情感分析预测系统")
print("="*40)
print("请选择要使用的模型:")
print("\n方法选择:")
print(" 1. Embedding + 分类头 (推理快速,显存占用少)")
print(" 2. LoRA微调 (效果更好,显存占用较多)")
method_choice = None
while method_choice not in ['1', '2']:
method_choice = input("\n请选择方法 (1/2): ").strip()
if method_choice not in ['1', '2']:
print("无效选择,请输入 1 或 2")
method_type = "embedding" if method_choice == '1' else "lora"
method_name = "Embedding + 分类头" if method_choice == '1' else "LoRA微调"
print(f"\n已选择: {method_name}")
print("\n模型大小选择:")
print(" 1. 0.6B - 轻量级,推理快速")
print(" 2. 4B - 中等规模,性能均衡")
print(" 3. 8B - 大规模,性能最佳")
size_choice = None
while size_choice not in ['1', '2', '3']:
size_choice = input("\n请选择模型大小 (1/2/3): ").strip()
if size_choice not in ['1', '2', '3']:
print("无效选择,请输入 1、2 或 3")
size_map = {'1': '0.6B', '2': '4B', '3': '8B'}
model_size = size_map[size_choice]
print(f"已选择: Qwen3-{method_name}-{model_size}")
print("正在加载模型...")
try:
self.load_model(method_type, model_size)
print(f"模型加载成功!")
except Exception as e:
print(f"模型加载失败: {e}")
print("请检查模型文件是否存在,或先进行训练")
def interactive_predict(self):
"""交互式预测模式"""
if len(self.models) == 0:
# 让用户选择要加载的模型
self._select_and_load_model()
if len(self.models) == 0:
print("没有加载任何模型,退出预测")
return
print("\n" + "="*60)
print("Qwen3微博情感分析预测系统")
print("="*60)
print("已加载模型:")
for model_info in self.models.values():
print(f" - {model_info['display_name']}")
print("\n命令提示:")
print(" 输入 'q' 退出程序")
print(" 输入 'switch' 切换模型")
print(" 输入 'models' 查看已加载模型")
print(" 输入 'compare' 比较所有模型性能")
print("-"*60)
while True:
try:
text = input("\n请输入要分析的微博内容: ").strip()
if text.lower() == 'q':
print("感谢使用,再见!")
break
if text.lower() == 'models':
print("已加载模型:")
for model_info in self.models.values():
print(f" - {model_info['display_name']}")
continue
if text.lower() == 'switch':
print("切换模型...")
self.models.clear() # 清空当前模型
self._select_and_load_model()
if len(self.models) > 0:
print("模型切换成功!")
for model_info in self.models.values():
print(f" 当前模型: {model_info['display_name']}")
continue
if text.lower() == 'compare':
test_text = input("请输入要比较的文本: ")
self._compare_models(test_text)
continue
if not text:
print("请输入有效内容")
continue
# 预测
results = self.predict_single(text)
print(f"\n原文: {text}")
print("预测结果:")
# 按模型类型和大小排序显示
sorted_results = sorted(results.items())
for model_name, (pred, conf) in sorted_results:
sentiment = "正面" if pred == 1 else "负面"
print(f" {model_name:20}: {sentiment} (置信度: {conf:.4f})")
# 只显示单个模型的预测结果(不进行集成)
except KeyboardInterrupt:
print("\n\n程序被中断,再见!")
break
except Exception as e:
print(f"预测过程中出现错误: {e}")
def _compare_models(self, text: str):
"""比较不同模型的性能"""
print(f"\n模型性能比较 - 文本: {text}")
print("-" * 60)
results = self.predict_single(text)
embedding_models = []
lora_models = []
for model_name, (pred, conf) in results.items():
sentiment = "正面" if pred == 1 else "负面"
if "Embedding" in model_name:
embedding_models.append((model_name, sentiment, conf))
elif "Lora" in model_name:
lora_models.append((model_name, sentiment, conf))
if embedding_models:
print("Embedding + 分类头方法:")
for name, sentiment, conf in embedding_models:
print(f" {name}: {sentiment} ({conf:.4f})")
if lora_models:
print("LoRA微调方法:")
for name, sentiment, conf in lora_models:
print(f" {name}: {sentiment} ({conf:.4f})")
def main():
"""主函数"""
parser = argparse.ArgumentParser(description='Qwen3微博情感分析统一预测接口')
parser.add_argument('--model_dir', type=str, default='./models',
help='模型文件目录')
parser.add_argument('--model_type', type=str, choices=['embedding', 'lora'],
help='指定模型类型')
parser.add_argument('--model_size', type=str, choices=['0.6B', '4B', '8B'],
help='指定模型大小')
parser.add_argument('--text', type=str,
help='直接预测指定文本')
parser.add_argument('--interactive', action='store_true', default=True,
help='交互式预测模式(默认)')
parser.add_argument('--ensemble', action='store_true',
help='使用集成预测')
parser.add_argument('--load_all', action='store_true',
help='加载所有可用模型')
args = parser.parse_args()
# 创建预测器
predictor = Qwen3UniversalPredictor()
# 加载模型
if args.load_all:
# 加载所有模型
predictor.load_all_models(args.model_dir)
elif args.model_type and args.model_size:
# 加载指定模型
predictor.load_model(args.model_type, args.model_size)
# 如果没有指定模型,交互式模式会让用户选择
# 如果指定了文本,直接预测
if args.text:
if args.ensemble and len(predictor.models) > 1:
pred, conf = predictor.ensemble_predict(args.text)
sentiment = "正面" if pred == 1 else "负面"
print(f"文本: {args.text}")
print(f"集成预测: {sentiment} (置信度: {conf:.4f})")
else:
results = predictor.predict_single(args.text)
print(f"文本: {args.text}")
for model_name, (pred, conf) in results.items():
sentiment = "正面" if pred == 1 else "负面"
print(f"{model_name}: {sentiment} (置信度: {conf:.4f})")
else:
# 进入交互式模式
predictor.interactive_predict()
if __name__ == "__main__":
main()