forked from OptimalScale/LMFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
146 lines (120 loc) · 4.69 KB
/
app.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
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
import json
import torch
import os
from flask import Flask, request, stream_with_context
from flask import render_template
from flask_cors import CORS
from accelerate import Accelerator
from dataclasses import dataclass, field
from transformers import HfArgumentParser
from typing import Optional
from lmflow.datasets.dataset import Dataset
from lmflow.pipeline.auto_pipeline import AutoPipeline
from lmflow.models.auto_model import AutoModel
from lmflow.args import ModelArguments, DatasetArguments, AutoArguments
WINDOW_LENGTH = 512
@dataclass
class AppArguments:
end_string: Optional[str] = field(
default="##",
metadata={
"help": "end string mark of the chatbot's output"
},
)
max_new_tokens: Optional[int] = field(
default=200,
metadata={
"help": "maximum number of generated tokens"
},
)
parser = HfArgumentParser((
ModelArguments,
AppArguments,
))
model_args, app_args = (
parser.parse_args_into_dataclasses()
)
app = Flask(__name__)
CORS(app)
ds_config_path = "./examples/ds_config.json"
with open (ds_config_path, "r") as f:
ds_config = json.load(f)
local_rank = int(os.getenv("LOCAL_RANK", "0"))
world_size = int(os.getenv("WORLD_SIZE", "1"))
torch.cuda.set_device(local_rank)
model = AutoModel.get_model(model_args, tune_strategy='none', ds_config=ds_config, use_accelerator=True)
accelerator = Accelerator()
def stream_generate(inputs,context_len = 1024, max_new_tokens=128, end_string="##"):
max_src_len = context_len - max_new_tokens - len(end_string)
input_ids = model.tokenizer(inputs).input_ids
input_echo_len = len(input_ids)
output_ids = list(input_ids)
input_ids = input_ids[-max_src_len:]
past_key_values = out = None
flag_stop = False
for i in range(0, max_new_tokens):
with accelerator.autocast():
if i == 0:
with torch.no_grad():
out = model.backend_model(torch.as_tensor([input_ids], device=local_rank), use_cache=True)
logits = out.logits
past_key_values = out.past_key_values
else:
with torch.no_grad():
out = model.backend_model(
input_ids=torch.as_tensor([[token]], device=local_rank),
use_cache=True,
past_key_values=past_key_values,
)
logits = out.logits
past_key_values = out.past_key_values
last_token_logits = logits[0, -1, :]
token = int(torch.argmax(last_token_logits))
output_ids.append(token)
tmp_output_ids = output_ids[input_echo_len:]
output = model.tokenizer.decode(
tmp_output_ids,
skip_special_tokens=True,
spaces_between_special_tokens=False,
)
if end_string in output:
index = output.index(end_string)
output = output[:index]
flag_stop = True
yield output.replace("\ufffd","")
if flag_stop == True:
break
@app.route('/predict',methods = ['POST'])
def predict():
if(request.method == "POST"):
try:
user_input = request.get_json()["Input"]
conversation = request.get_json()["History"]
history_input = ""
if(len(conversation) >= 2):
if(len(conversation) == 2):
history_input ="###Human: " + user_input +" "
else:
for i in range(0, len(conversation)-1):
if(i % 2 == 0):
history_input = history_input + "###Human: " + conversation[i+1]["content"] + " "
elif(i % 2 == 1):
history_input = history_input + "###Assistant:" + conversation[i+1]["content"]
history_input = history_input + "###Assistant:"
if len(model.encode(history_input))> WINDOW_LENGTH:
inputs = model.encode(history_input)
inputs = inputs[-WINDOW_LENGTH:]
history_input = model.decode(inputs)
return app.response_class(stream_with_context(stream_generate(history_input,
max_new_tokens=app_args.max_new_tokens,
end_string=app_args.end_string)))
except Exception as ex:
print(ex)
text_out = ex
else:
text_out = "Not POST Method"
return text_out
@app.route('/',methods = ['GET'])
def login():
return render_template('index.html')
app.run(port = 5000, debug = False)