alwayse commited on
Commit
ea2d4f7
1 Parent(s): d0e1f8b

Upload api_init.py

Browse files
Files changed (1) hide show
  1. api_init.py +63 -0
api_init.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ import random
4
+ from meta_train import mmdPreModel
5
+ from collections import namedtuple
6
+ import joblib
7
+ from transformers import RobertaTokenizer, RobertaModel
8
+
9
+
10
+ def api_init():
11
+
12
+ random.seed(0)
13
+ np.random.seed(0)
14
+ torch.manual_seed(0)
15
+ torch.cuda.manual_seed(0)
16
+ torch.cuda.manual_seed_all(0)
17
+ torch.backends.cudnn.benchmark = False
18
+ torch.backends.cudnn.deterministic = True
19
+
20
+ model_name = 'roberta-base-openai-detector'
21
+ model_path_api = f'.'
22
+ token_num, hidden_size = 100, 768
23
+
24
+ Config = namedtuple('Config', ['in_dim', 'hid_dim', 'dropout', 'out_dim', 'token_num'])
25
+ config = Config(
26
+ in_dim=hidden_size,
27
+ token_num=token_num,
28
+ hid_dim=512,
29
+ dropout=0.2,
30
+ out_dim=300,)
31
+
32
+ net = mmdPreModel(config=config, num_mlp=0, transformer_flag=True, num_hidden_layers=1)
33
+
34
+ # load the features and models
35
+ feature_ref_for_test_filename = f'{model_path_api}/feature_ref_for_test.pt'
36
+ model_filename = f'{model_path_api}/logistic_regression_model.pkl'
37
+ net_filename = f'{model_path_api}/net.pt'
38
+
39
+ load_ref_data = torch.load(feature_ref_for_test_filename,map_location=torch.device('cpu')) # cpu
40
+ loaded_model = joblib.load(model_filename) # cpu
41
+ checkpoint = torch.load(net_filename,map_location=torch.device('cpu'))
42
+ net.load_state_dict(checkpoint['net'])
43
+ sigma, sigma0_u, ep = checkpoint['sigma'], checkpoint['sigma0_u'], checkpoint['ep']
44
+
45
+ # generic generative model
46
+ cache_dir = ".cache"
47
+ base_tokenizer = RobertaTokenizer.from_pretrained(model_name, cache_dir=cache_dir)
48
+ base_model = RobertaModel.from_pretrained(model_name, output_hidden_states=True, cache_dir=cache_dir)
49
+
50
+ # whether load the model to gpu
51
+ gpu_using = False
52
+
53
+ DEVICE = torch.device("cpu")
54
+ if gpu_using:
55
+ DEVICE = torch.device("cuda:0")
56
+ net = net.to(DEVICE)
57
+ sigma, sigma0_u, ep = sigma.to(DEVICE), sigma0_u.to(DEVICE), ep.to(DEVICE)
58
+ load_ref_data = load_ref_data.to(DEVICE)
59
+ base_model = base_model.to(DEVICE)
60
+ num_ref = 5000
61
+ feature_ref = load_ref_data[np.random.permutation(load_ref_data.shape[0])][:num_ref].to(DEVICE)
62
+
63
+ return base_model, base_tokenizer, net, feature_ref, sigma, sigma0_u, ep, loaded_model, DEVICE