forked from qiuhuachuan/smile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeChat_local.py
70 lines (53 loc) · 2.18 KB
/
MeChat_local.py
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
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '6'
import torch
from transformers import AutoTokenizer, AutoModel
from peft import PeftModel
model = AutoModel.from_pretrained('THUDM/chatglm-6b',
revision='v0.1.0',
trust_remote_code=True)
LaRA_PATH = './src/research_smile_epoch2_rank8_full'
model = PeftModel.from_pretrained(model, LaRA_PATH)
model = model.float().to(device='cuda')
tokenizer = AutoTokenizer.from_pretrained('THUDM/chatglm-6b',
trust_remote_code=True)
def format_example(example: dict) -> dict:
context = f'''Input: {example['input']}\n'''
return {'context': context, 'target': ''}
def generate_response(data: dict):
with torch.no_grad():
feature = format_example(data)
input_text = feature['context']
ids = tokenizer.encode(input_text)
input_length = len(ids)
input_ids = torch.LongTensor([ids]).to(device='cuda')
out = model.generate(input_ids=input_ids,
max_length=2040,
do_sample=True,
temperature=0.9,
top_p=0.9)
true_out_text = tokenizer.decode(out[0][input_length:])
answer = true_out_text.replace('\nEND', '').strip()
return answer
data = []
while True:
seeker_msg = input('求助者:')
data.append({'owner': 'seeker', 'msg': seeker_msg})
input_str = ''
for item in data:
if item['owner'] == 'seeker':
input_str += '求助者:' + item['msg']
else:
input_str += '支持者:' + item['msg']
input_str += '支持者:'
while len(input_str) > 2000:
if input_str.index('求助者:') > input_str.index('支持者:'):
start_idx = input_str.index('求助者:')
else:
start_idx = input_str.index('支持者:')
input_str = input_str[start_idx:]
wrapped_data = {'input': input_str}
response = generate_response(data=wrapped_data)
print(f'支持者:{response}')
supporter_msg = {'owner': 'supporter', 'msg': response}
data.append(supporter_msg)