app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
2 |
+
import torch
|
3 |
+
|
4 |
+
model_id = "scb10x/llama-3-typhoon-v1.5-8b-instruct"
|
5 |
+
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
8 |
+
model_id,
|
9 |
+
torch_dtype=torch.bfloat16,
|
10 |
+
device_map="auto",
|
11 |
+
)
|
12 |
+
|
13 |
+
messages = [
|
14 |
+
{"role": "system", "content": "You are a helpful assistant who're always speak Thai."},
|
15 |
+
{"role": "user", "content": "ขอสูตรไก่ย่าง"},
|
16 |
+
]
|
17 |
+
|
18 |
+
input_ids = tokenizer.apply_chat_template(
|
19 |
+
messages,
|
20 |
+
add_generation_prompt=True,
|
21 |
+
return_tensors="pt"
|
22 |
+
).to(model.device)
|
23 |
+
|
24 |
+
terminators = [
|
25 |
+
tokenizer.eos_token_id,
|
26 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
27 |
+
]
|
28 |
+
|
29 |
+
outputs = model.generate(
|
30 |
+
input_ids,
|
31 |
+
max_new_tokens=512,
|
32 |
+
eos_token_id=terminators,
|
33 |
+
do_sample=True,
|
34 |
+
temperature=0.4,
|
35 |
+
top_p=0.9,
|
36 |
+
)
|
37 |
+
response = outputs[0][input_ids.shape[-1]:]
|
38 |
+
print(tokenizer.decode(response, skip_special_tokens=True))
|