-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify.py
More file actions
688 lines (552 loc) · 22 KB
/
classify.py
File metadata and controls
688 lines (552 loc) · 22 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
import os
import argparse
import torch
from torch.optim import AdamW
from torch.utils.data import Dataset, DataLoader
from transformers import (
get_linear_schedule_with_warmup,
BertTokenizer,
BertForSequenceClassification,
RobertaTokenizer,
RobertaForSequenceClassification,
AutoTokenizer,
AutoModelForSequenceClassification,
)
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score
import numpy as np
from flask import Flask, request, jsonify
import requests
import pickle
import threading
from queue import Queue
CACHE_FILE = "samples.pkl"
DEVICE = torch.device(
"cuda"
if torch.cuda.is_available()
else "mps" if torch.backends.mps.is_available() else "cpu"
)
HEAD_MAX_LEN = 512
BODY_MAX_LEN = 512
BATCH_SIZE = 16
EPOCHS = 4
LEARNING_RATE = 2e-5
SEED = 42
BERT_DIR = "./bert/" # bert-base-multilingual-cased
ROBERTA_DIR = "./roberta/" # roberta-large
XLM_ROBERTA = "./xlm_roberta" # xlm-roberta-large
# 固定随机种子
torch.manual_seed(SEED)
np.random.seed(SEED)
model_classes = {
"bert": (BertTokenizer, BertForSequenceClassification, BERT_DIR),
"roberta": (RobertaTokenizer, RobertaForSequenceClassification, ROBERTA_DIR),
"xlm": (AutoTokenizer, AutoModelForSequenceClassification, XLM_ROBERTA),
}
def extract_body_text_by_file(file_path, max_len=512):
file_size = os.path.getsize(file_path)
if file_size <= max_len:
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
return f.read().strip()
chunk_size = max_len // 3 # 三等分
positions = [0, file_size // 2, file_size - chunk_size] # 头、中、尾
extracted_text = []
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
for pos in positions:
f.seek(pos) # 定位到相应位置
text_chunk = f.read(chunk_size)
extracted_text.append(text_chunk.strip())
return "".join(extracted_text)
def extract_body_text_by_string(input_string, max_len=512):
string_length = len(input_string)
if string_length <= max_len:
return input_string.strip()
chunk_size = max_len // 3 # 三等分
positions = [0, string_length // 2, string_length - chunk_size] # 头、中、尾
extracted_text = []
for pos in positions:
text_chunk = input_string[pos : pos + chunk_size]
extracted_text.append(text_chunk.strip())
return "".join(extracted_text)
def collect_samples(ok_dir, ban_dir):
"""从目录中收集数据,优先从缓存文件 samples.pkl 读取"""
# 1. 先尝试加载缓存数据
if os.path.exists(CACHE_FILE):
try:
with open(CACHE_FILE, "rb") as f:
print("Loading samples from cache...")
return pickle.load(f)
except Exception as e:
print(f"Failed to load cache: {e}")
# 2. 重新收集数据
print("Collecting samples from directories...")
samples = []
def process_directory(directory, label):
if not os.path.exists(directory):
print(f"Warning: Directory {directory} not found!")
return
for domain in os.listdir(directory):
domain_path = os.path.join(directory, domain)
head_path = os.path.join(domain_path, "head.txt")
body_path = os.path.join(domain_path, "body.txt")
if os.path.exists(head_path) and os.path.exists(body_path):
try:
with open(head_path, "r", encoding="utf-8", errors="ignore") as f:
head_text = f.read().strip()
body_text = extract_body_text_by_file(body_path, BODY_MAX_LEN)
samples.append(
{"head": head_text, "body": body_text, "label": label}
)
except Exception as e:
print(f"Error reading files for domain {domain}: {e}")
process_directory(ok_dir, label=0)
process_directory(ban_dir, label=1)
# 3. 序列化 samples 并缓存
try:
with open(CACHE_FILE, "wb") as f:
pickle.dump(samples, f)
print("Samples cached successfully.")
except Exception as e:
print(f"Failed to save cache: {e}")
return samples
# 自定义Dataset类
class TextDataset(Dataset):
def __init__(self, samples, tokenizer, max_len, mode="head"):
self.samples = samples
self.tokenizer = tokenizer
self.max_len = max_len
self.mode = mode # 'head' or 'body'
def __len__(self):
return len(self.samples)
def __getitem__(self, idx):
sample = self.samples[idx]
# 读取对应模式的文本内容
text = sample[self.mode]
# Tokenize文本
encoding = self.tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=self.max_len,
padding="max_length",
truncation=True,
return_attention_mask=True,
return_tensors="pt",
)
return {
"input_ids": encoding["input_ids"].flatten(),
"attention_mask": encoding["attention_mask"].flatten(),
"label": torch.tensor(sample["label"], dtype=torch.long),
}
# 训练函数
def train_model(model, train_loader, val_loader, model_save_path):
print("train_model")
optimizer = AdamW(model.parameters(), lr=LEARNING_RATE)
total_steps = len(train_loader) * EPOCHS
scheduler = get_linear_schedule_with_warmup(
optimizer, num_warmup_steps=0, num_training_steps=total_steps
)
best_f1 = 0.0
for epoch in range(EPOCHS):
model.train()
total_loss = 0
for batch in train_loader:
optimizer.zero_grad()
input_ids = batch["input_ids"].to(DEVICE)
attention_mask = batch["attention_mask"].to(DEVICE)
labels = batch["label"].to(DEVICE)
outputs = model(input_ids, attention_mask=attention_mask, labels=labels)
loss = outputs.loss
total_loss += loss.item()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
scheduler.step()
avg_train_loss = total_loss / len(train_loader)
val_acc, val_f1 = evaluate(model, val_loader)
print(f"Epoch {epoch+1}/{EPOCHS}")
print(f"Train loss: {avg_train_loss:.4f}")
print(f"Val acc: {val_acc:.4f}, Val F1: {val_f1:.4f}")
if val_f1 > best_f1:
torch.save(model.state_dict(), model_save_path)
best_f1 = val_f1
print(f"New best model saved with F1: {best_f1:.4f}")
# 评估函数
def evaluate(model, data_loader):
model.eval()
predictions = []
true_labels = []
with torch.no_grad():
for batch in data_loader:
input_ids = batch["input_ids"].to(DEVICE)
attention_mask = batch["attention_mask"].to(DEVICE)
labels = batch["label"].cpu().numpy()
outputs = model(input_ids, attention_mask=attention_mask)
logits = outputs.logits.detach().cpu().numpy()
preds = np.argmax(logits, axis=1)
predictions.extend(preds)
true_labels.extend(labels)
acc = accuracy_score(true_labels, predictions)
f1 = f1_score(true_labels, predictions)
return acc, f1
# 预测函数(统一用于 API 和命令行模式)
def predict_text(text, model, tokenizer, max_len, device=DEVICE):
encoding = tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=max_len,
padding="max_length",
truncation=True,
return_attention_mask=True,
return_tensors="pt",
)
input_ids = encoding["input_ids"].to(device)
attention_mask = encoding["attention_mask"].to(device)
with torch.no_grad():
outputs = model(input_ids, attention_mask=attention_mask)
# print("outputs", outputs)
logits = outputs.logits.cpu().numpy()
pred = np.argmax(logits, axis=1)[0]
return "ok" if pred == 0 else "ban"
def train_model_pipeline(ok_dir, ban_dir, model_type, max_len, model_name):
if model_type not in model_classes:
raise ValueError("Invalid model_type. Use 'bert', 'roberta', or 'xlm'.")
samples = collect_samples(ok_dir, ban_dir)
train_samples, val_samples = train_test_split(
samples,
test_size=0.2,
stratify=[s["label"] for s in samples],
random_state=SEED,
)
tokenizer_cls, model_cls, model_dir = model_classes[model_type]
tokenizer = tokenizer_cls.from_pretrained(model_dir)
model = model_cls.from_pretrained(model_dir, num_labels=2).to(DEVICE)
train_dataset = TextDataset(train_samples, tokenizer, max_len, model_name)
val_dataset = TextDataset(val_samples, tokenizer, max_len, model_name)
train_loader = DataLoader(
train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4
)
val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE)
train_model(model, train_loader, val_loader, f"best_{model_name}_model.bin")
model_save_path = (
f"{model_type}_geosite_by_{model_name}" # 使用命令行参数中的 model_type
)
model.save_pretrained(model_save_path)
tokenizer.save_pretrained(model_save_path)
def train_head_model(ok_dir, ban_dir, model_type):
train_model_pipeline(ok_dir, ban_dir, model_type, HEAD_MAX_LEN, "head")
def train_body_model(ok_dir, ban_dir, model_type):
train_model_pipeline(ok_dir, ban_dir, model_type, BODY_MAX_LEN, "body")
# it's in standard library
import sqlite3
# 初始化 SQLite 数据库
def init_db():
conn = sqlite3.connect("cache.db")
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS domain_predictions (
domain TEXT PRIMARY KEY,
head_prediction TEXT,
body_prediction TEXT
)
"""
)
conn.commit()
conn.close()
def get_cache(domain):
conn = sqlite3.connect("cache.db")
cursor = conn.cursor()
cursor.execute(
"SELECT head_prediction, body_prediction FROM domain_predictions WHERE domain=?",
(domain,),
)
row = cursor.fetchone()
conn.close()
if row:
return {"head_prediction": row[0], "body_prediction": row[1]}
return None
def set_cache(domain, head_prediction, body_prediction):
conn = sqlite3.connect("cache.db")
cursor = conn.cursor()
cursor.execute(
"""
INSERT INTO domain_predictions (domain, head_prediction, body_prediction)
VALUES (?, ?, ?)
ON CONFLICT(domain) DO UPDATE SET
head_prediction=excluded.head_prediction,
body_prediction=excluded.body_prediction
""",
(domain, head_prediction, body_prediction),
)
conn.commit()
conn.close()
def start_api_service(ip, port, model_type, load_method):
app = Flask(__name__)
if model_type not in model_classes:
raise ValueError("Invalid model_type. Use 'bert', 'roberta', or 'xlm'.")
tokenizer_cls, model_cls, model_dir = model_classes[model_type]
if load_method == "state_dict":
head_tokenizer = tokenizer_cls.from_pretrained(model_dir)
body_tokenizer = tokenizer_cls.from_pretrained(model_dir)
head_model = model_cls.from_pretrained(model_dir, num_labels=2).to(DEVICE)
body_model = model_cls.from_pretrained(model_dir, num_labels=2).to(DEVICE)
model_paths = {"head": "best_head_model.bin", "body": "best_body_model.bin"}
head_model.load_state_dict(torch.load(model_paths["head"]))
body_model.load_state_dict(torch.load(model_paths["body"]))
elif load_method == "pretrained":
head_tokenizer = tokenizer_cls.from_pretrained(f"{model_type}_geosite_by_head")
body_tokenizer = tokenizer_cls.from_pretrained(f"{model_type}_geosite_by_body")
head_model = model_cls.from_pretrained(f"{model_type}_geosite_by_head").to(
DEVICE
)
body_model = model_cls.from_pretrained(f"{model_type}_geosite_by_body").to(
DEVICE
)
else:
raise ValueError(
f"Invalid load_method: {load_method}. Use 'state_dict' or 'pretrained'."
)
head_model.eval()
body_model.eval()
init_db() # 确保数据库表存在
@app.route("/predict", methods=["POST"])
def predict():
data = request.get_json()
text = data.get("text", "")
model_name = data.get("model_name", "head")
if model_name not in ["head", "body"]:
return jsonify({"error": 'Invalid model_type. Use "head" or "body".'}), 400
if model_name == "body":
text = extract_body_text_by_string(text)
model = head_model if model_name == "head" else body_model
tokenizer = head_tokenizer if model_name == "head" else body_tokenizer
max_len = HEAD_MAX_LEN if model_name == "head" else BODY_MAX_LEN
result = predict_text(text, model, tokenizer, max_len)
return jsonify({"result": result})
@app.route("/check", methods=["POST"])
def check():
data = request.json
print("got check request", data)
domain = data.get("domain")
socks5_proxy = data.get("socks5_proxy")
only_proxy = data.get("only_proxy", False)
if not domain:
return jsonify({"error": "Domain is required"}), 400
# 1. 查询缓存
cached_result = get_cache(domain)
if cached_result:
return jsonify(
{
"domain": domain,
"head_prediction": cached_result["head_prediction"],
"body_prediction": cached_result["body_prediction"],
"cached": True,
}
)
headers = {
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0.2 Mobile/15E148 Safari/604.1",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
}
def fetch(proxies=None):
# print("fetching")
# Create a queue to store the first successful response
result_queue = Queue()
def make_request(url, proxies=None):
print("making request", url, proxies)
try:
response = requests.get(
url, headers=headers, proxies=proxies, timeout=4
)
if response.text:
result_queue.put(("success", response))
except requests.exceptions.RequestException as e:
result_queue.put(("error", str(e)))
# Start both requests in parallel
https_thread = threading.Thread(
target=make_request, args=(f"https://{domain}", proxies)
)
http_thread = threading.Thread(
target=make_request, args=(f"http://{domain}", proxies)
)
https_thread.start()
http_thread.start()
# print("Wait for the first response")
status, result = result_queue.get()
# print("Cancel the other thread if it's still running")
https_thread.join(timeout=0.1)
http_thread.join(timeout=0.1)
print("status", status, result, domain, proxies)
if status == "success":
return result
else:
return f"fetch err: {result}"
# end of fetch
# 2. 进行请求
result = None
if only_proxy:
if not socks5_proxy:
return (
jsonify({"error": "socks5_proxy is required when only_proxy=True"}),
400,
)
proxies = {
"http": f"socks5://{socks5_proxy}",
"https": f"socks5://{socks5_proxy}",
}
result = fetch(proxies)
else:
# 使用多线程并行执行直接请求和代理请求
result_queue = Queue()
def make_request2(proxies=None):
try:
response = fetch(proxies)
if not isinstance(response, str): # 如果不是错误消息
result_queue.put(("success", response))
else:
result_queue.put(("error", response))
except Exception as e:
result_queue.put(("error", str(e)))
# 启动直接请求线程
direct_thread = threading.Thread(target=make_request2)
# 如果有代理,启动代理请求线程
proxy_thread = None
if socks5_proxy:
proxies = {
"http": f"socks5://{socks5_proxy}",
"https": f"socks5://{socks5_proxy}",
}
proxy_thread = threading.Thread(target=make_request2, args=(proxies,))
direct_thread.start()
if proxy_thread:
proxy_thread.start()
# 等待第一个响应
status, result = result_queue.get()
# 取消其他线程
direct_thread.join(timeout=0.1)
if proxy_thread:
proxy_thread.join(timeout=0.1)
# 如果直接请求成功,优先使用它
if status == "success":
pass # 使用已获取的结果
else:
# 如果直接请求失败,使用代理请求结果(如果有)
if proxy_thread and result_queue.qsize() > 0:
status, result = result_queue.get()
else:
return jsonify({"error": f"Request failed: {result}"}), 500
if isinstance(result, str):
return jsonify({"error": f"Request failed: {result}"}), 500
response = result
h_text = [f"{key}: {value}" for key, value in response.headers.items()]
head_text = "\n".join(h_text)
body_text = extract_body_text_by_string(response.text)
# 3. 进行预测
head_result = predict_text(head_text, head_model, head_tokenizer, HEAD_MAX_LEN)
body_result = predict_text(body_text, body_model, body_tokenizer, BODY_MAX_LEN)
# 4. 缓存结果
set_cache(domain, head_result, body_result)
return jsonify(
{
"domain": domain,
"head_prediction": head_result,
"body_prediction": body_result,
"cached": False,
}
)
print(f"Starting API service on port {port}...")
app.run(host=ip, port=port)
# 命令行调用
if __name__ == "__main__":
print("device is ", DEVICE)
parser = argparse.ArgumentParser(
description="Train, predict, or serve API using head/body models."
)
parser.add_argument(
"--mode",
type=str,
required=True,
choices=[
"train_head",
"train_body",
"predict_head",
"predict_body",
"serve_api",
],
help="Mode: train_head, train_body, predict_head, predict_body, serve_api",
)
parser.add_argument(
"--ok_dir",
type=str,
default="china-list_out",
help="Directory containing ok samples",
)
parser.add_argument(
"--ban_dir",
type=str,
default="proxy-list_out",
help="Directory containing ban samples",
)
parser.add_argument(
"--text", type=str, help="Text to predict (only for predict mode)"
)
parser.add_argument(
"--ip",
type=str,
default="0.0.0.0",
help="ip to serve API (only for serve_api mode)",
)
parser.add_argument(
"--port",
type=int,
default=5000,
help="Port to serve API (only for serve_api mode)",
)
parser.add_argument(
"--model_type",
type=str,
default="bert",
choices=["bert", "roberta", "xlm"],
help="Model type: bert, roberta-large, xlm-roberta-large",
)
parser.add_argument(
"--load_method",
type=str,
default="pretrained",
choices=["state_dict", "pretrained"],
help="Method to load model for predict: state_dict or pretrained",
)
args = parser.parse_args()
if args.model_type not in model_classes:
raise ValueError("Invalid model_type. Use 'bert', 'roberta', or 'xlm'.")
if args.mode == "train_head":
train_head_model(args.ok_dir, args.ban_dir, args.model_type)
elif args.mode == "train_body":
train_body_model(args.ok_dir, args.ban_dir, args.model_type)
elif args.mode == "serve_api":
start_api_service(args.ip, args.port, args.model_type, args.load_method)
elif args.mode in ["predict_head", "predict_body"]:
model_name = "head" if args.mode == "predict_head" else "body"
max_len = HEAD_MAX_LEN if model_name == "head" else BODY_MAX_LEN
tokenizer_cls, model_cls, model_dir = model_classes[args.model_type]
if args.load_method == "state_dict":
tokenizer = tokenizer_cls.from_pretrained(model_dir)
model = model_cls.from_pretrained(model_dir, num_labels=2).to(DEVICE)
model.load_state_dict(torch.load(f"best_{model_name}_model.bin"))
elif args.load_method == "pretrained":
model_path = f"{args.model_type}_geosite_by_{model_name}"
tokenizer = tokenizer_cls.from_pretrained(model_path)
model = model_cls.from_pretrained(model_path).to(DEVICE)
else:
raise ValueError(
f"Invalid load_method: {args.load_method}. Use 'state_dict' or 'pretrained'."
)
model.eval()
text = args.text
if model_name == "body":
text = extract_body_text_by_string(text)
result = predict_text(text, model, tokenizer, max_len)
print(f"Prediction: {result}")