Spaces:
Running
Running
admin commited on
Commit ·
832cc9b
1
Parent(s): 9cb4476
add exception catch
Browse files
app.py
CHANGED
|
@@ -27,31 +27,35 @@ def predict(
|
|
| 27 |
instruction += f"<|im_start|>user\n{user}\n<|im_end|>\n<|im_start|>assistant\n{assistant}\n<|im_end|>\n"
|
| 28 |
|
| 29 |
instruction += f"<|im_start|>user\n{message}\n<|im_end|>\n<|im_start|>assistant\n"
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
generate_kwargs = dict(
|
| 43 |
-
input_ids=input_ids.to(device),
|
| 44 |
-
attention_mask=attention_mask.to(device),
|
| 45 |
-
streamer=streamer,
|
| 46 |
-
do_sample=True,
|
| 47 |
-
temperature=temperature,
|
| 48 |
-
max_new_tokens=max_new_tokens,
|
| 49 |
-
top_k=top_k,
|
| 50 |
-
repetition_penalty=repetition_penalty,
|
| 51 |
-
top_p=top_p,
|
| 52 |
-
)
|
| 53 |
-
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 54 |
-
t.start()
|
| 55 |
outputs = []
|
| 56 |
for new_token in streamer:
|
| 57 |
outputs.append(new_token)
|
|
|
|
| 27 |
instruction += f"<|im_start|>user\n{user}\n<|im_end|>\n<|im_start|>assistant\n{assistant}\n<|im_end|>\n"
|
| 28 |
|
| 29 |
instruction += f"<|im_start|>user\n{message}\n<|im_end|>\n<|im_start|>assistant\n"
|
| 30 |
+
try:
|
| 31 |
+
streamer = TextIteratorStreamer(
|
| 32 |
+
tokenizer,
|
| 33 |
+
skip_prompt=True,
|
| 34 |
+
skip_special_tokens=True,
|
| 35 |
+
)
|
| 36 |
+
enc = tokenizer(instruction, return_tensors="pt", padding=True, truncation=True)
|
| 37 |
+
input_ids, attention_mask = enc.input_ids, enc.attention_mask
|
| 38 |
+
if input_ids.shape[1] > CONTEXT_LENGTH:
|
| 39 |
+
input_ids = input_ids[:, -CONTEXT_LENGTH:]
|
| 40 |
+
attention_mask = attention_mask[:, -CONTEXT_LENGTH:]
|
| 41 |
+
|
| 42 |
+
generate_kwargs = dict(
|
| 43 |
+
input_ids=input_ids.to(device),
|
| 44 |
+
attention_mask=attention_mask.to(device),
|
| 45 |
+
streamer=streamer,
|
| 46 |
+
do_sample=True,
|
| 47 |
+
temperature=temperature,
|
| 48 |
+
max_new_tokens=max_new_tokens,
|
| 49 |
+
top_k=top_k,
|
| 50 |
+
repetition_penalty=repetition_penalty,
|
| 51 |
+
top_p=top_p,
|
| 52 |
+
)
|
| 53 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 54 |
+
t.start()
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
streamer = f"{e}"
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
outputs = []
|
| 60 |
for new_token in streamer:
|
| 61 |
outputs.append(new_token)
|