Spaces:
Runtime error
Runtime error
File size: 4,207 Bytes
8969f81 |
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 |
from threading import Thread
import falcon
from falcon.http_status import HTTPStatus
import json
import requests
import time
from Model import generate_completion
import sys
class AutoComplete(object):
def on_post(self, req, resp, single_endpoint=True, x=None, y=None):
json_data = json.loads(req.bounded_stream.read())
resp.status = falcon.HTTP_200
start = time.time()
try:
context = json_data["context"].rstrip()
except KeyError:
resp.body = "The context field is required"
resp.status = falcon.HTTP_422
return
try:
n_samples = json_data['samples']
except KeyError:
n_samples = 3
try:
length = json_data['gen_length']
except KeyError:
length = 20
try:
max_time = json_data['max_time']
except KeyError:
max_time = -1
try:
model_name = json_data['model_size']
except KeyError:
model_name = "small"
try:
temperature = json_data['temperature']
except KeyError:
temperature = 0.7
try:
max_tokens = json_data['max_tokens']
except KeyError:
max_tokens = 256
try:
top_p = json_data['top_p']
except KeyError:
top_p = 0.95
try:
top_k = json_data['top_k']
except KeyError:
top_k = 40
# CTRL
try:
repetition_penalty = json_data['repetition_penalty']
except KeyError:
repetition_penalty = 0.02
# PPLM
try:
stepsize = json_data['step_size']
except KeyError:
stepsize = 0.02
try:
gm_scale = json_data['gm_scale']
except KeyError:
gm_scale = None
try:
kl_scale = json_data['kl_scale']
except KeyError:
kl_scale = None
try:
num_iterations = json_data['num_iterations']
except KeyError:
num_iterations = None
try:
use_sampling = json_data['use_sampling']
except KeyError:
use_sampling = None
try:
bag_of_words_or_discrim = json_data['bow_or_discrim']
except KeyError:
bag_of_words_or_discrim = "kitchen"
print(json_data)
sentences = generate_completion(
context,
length=length,
max_time=max_time,
model_name=model_name,
temperature=temperature,
max_tokens=max_tokens,
top_p=top_p,
top_k=top_k,
# CTRL
repetition_penalty=repetition_penalty,
# PPLM
stepsize=stepsize,
bag_of_words_or_discrim=bag_of_words_or_discrim,
gm_scale=gm_scale,
kl_scale=kl_scale,
num_iterations=num_iterations,
use_sampling=use_sampling
)
resp.body = json.dumps({"sentences": sentences, 'time': time.time() - start})
resp.status = falcon.HTTP_200
sys.stdout.flush()
class Request(Thread):
def __init__(self, end_point, data):
Thread.__init__(self)
self.end_point = end_point
self.data = data
self.ret = None
def run(self):
print("Requesting with url", self.end_point)
self.ret = requests.post(url=self.end_point, json=self.data)
def join(self):
Thread.join(self)
return self.ret.text
class HandleCORS(object):
def process_request(self, req, resp):
resp.set_header('Access-Control-Allow-Origin', '*')
resp.set_header('Access-Control-Allow-Methods', '*')
resp.set_header('Access-Control-Allow-Headers', '*')
if req.method == 'OPTIONS':
raise HTTPStatus(falcon.HTTP_200, body='\n')
autocomplete = AutoComplete()
app = falcon.API(middleware=[HandleCORS()])
app.add_route('/autocomplete', autocomplete)
app.add_route('/autocomplete/{x}', autocomplete)
app.add_route('/autocomplete/{x}/{y}', autocomplete)
application = app
|