-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
153 lines (108 loc) · 3.57 KB
/
main.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
147
148
149
150
151
152
153
'''
Name: main.py
Writer: Hoseop Lee, Ainizer
Rule: Flask app server
update: 21.02.14
'''
from transformers import AutoModelForCausalLM, AutoTokenizer
from flask import Flask, request, jsonify, render_template
import torch
import os
from queue import Queue, Empty
from threading import Thread
import time
app = Flask(__name__)
print("model loading...")
print(os.system("ls"))
# Model & Tokenizer loading
tokenizer = AutoTokenizer.from_pretrained('./GPT2-large_Fairytale')
model = AutoModelForCausalLM.from_pretrained('./GPT2-large_Fairytale')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)
requests_queue = Queue() # request queue.
BATCH_SIZE = 1 # max request size.
CHECK_INTERVAL = 0.1
print("complete model loading")
##
# Request handler.
# GPU app can process only one request in one time.
def handle_requests_by_batch():
while True:
request_batch = []
while not (len(request_batch) >= BATCH_SIZE):
try:
request_batch.append(requests_queue.get(timeout=CHECK_INTERVAL))
except Empty:
continue
for requests in request_batch:
try:
requests["output"] = mk_fairytale(requests['input'][0], requests['input'][1])
except Exception as e:
requests["output"] = e
handler = Thread(target=handle_requests_by_batch).start()
##
# GPT-2 generator.
# Make Fairytale
def mk_fairytale(text, length):
try:
input_ids = tokenizer.encode(text, return_tensors='pt')
# input_ids also need to apply gpu device!
input_ids = input_ids.to(device)
min_length = len(input_ids.tolist()[0])
length = length if length > 0 else 1
length += min_length
# story model generating
outputs = model.generate(input_ids, pad_token_id=50256,
do_sample=True,
max_length=length,
min_length=min_length,
top_k=40,
num_return_sequences=1)
result = dict()
for idx, sample_output in enumerate(outputs):
result[0] = tokenizer.decode(sample_output.tolist(), skip_special_tokens=True)
return result
except Exception as e:
print('Error occur in script generating!', e)
return jsonify({'error': e}), 500
##
# Get post request page.
@app.route('/fairytale', methods=['POST'])
def generate():
# GPU app can process only one request in one time.
if requests_queue.qsize() > BATCH_SIZE:
return jsonify({'Error': 'Too Many Requests'}), 429
try:
args = []
text = request.form['text']
length = int(request.form['length'])
args.append(text)
args.append(length)
except Exception as e:
return jsonify({'message': 'Invalid request'}), 500
# input a request on queue
req = {'input': args}
requests_queue.put(req)
# wait
while 'output' not in req:
time.sleep(CHECK_INTERVAL)
return jsonify(req['output'])
##
# Queue deadlock error debug page.
@app.route('/queue_clear')
def queue_clear():
while not requests_queue.empty():
requests_queue.get()
return "Clear", 200
##
# Sever health checking page.
@app.route('/healthz', methods=["GET"])
def health_check():
return "Health", 200
##
# Main page.
@app.route('/')
def main():
return render_template('main.html'), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)