Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: gemma
|
3 |
+
language:
|
4 |
+
- th
|
5 |
+
- en
|
6 |
+
base_model:
|
7 |
+
- google/gemma-3-27b-pt
|
8 |
+
pipeline_tag: text-generation
|
9 |
+
---
|
10 |
+
|
11 |
+
**Authors**: NECTEC
|
12 |
+
|
13 |
+
### Description
|
14 |
+
|
15 |
+
This model, **nectec/thai-research-gemma-3-27b-it**, also known as **Pathumma LLM AI**, is a fine-tuned version of Google's Gemma 3 27B model, specialized for the Thai language. It was developed by the National Electronics and Computer Technology Center (NECTEC) of Thailand.
|
16 |
+
|
17 |
+
Starting with the `google/gemma-3-27b-pt` checkpoint, the model underwent continued pre-training on a diverse corpus of approximately 8 billion Thai tokens. Following this, it was instruction fine-tuned on 3,052,736 high-quality Thai question-answer pairs to enhance its ability to follow instructions and engage in conversation.
|
18 |
+
|
19 |
+
### Inputs and outputs
|
20 |
+
|
21 |
+
- **Input:**
|
22 |
+
- Text string, such as a question, a prompt, or a document to be summarized, primarily in Thai.
|
23 |
+
- Total input context of 128K tokens
|
24 |
+
|
25 |
+
- **Output:**
|
26 |
+
- Generated text in response to the input, such as an answer to a
|
27 |
+
question, analysis of image content, or a summary of a document, primarily in Thai.
|
28 |
+
- Total output context of 8192 tokens
|
29 |
+
|
30 |
+
### Usage
|
31 |
+
|
32 |
+
Below there are some code snippets on how to get quickly started with running the model. First, install the necessary libraries.
|
33 |
+
|
34 |
+
#### Running with `transformers` on a GPU
|
35 |
+
|
36 |
+
```sh
|
37 |
+
$ pip install -U transformers accelerate torch
|
38 |
+
```
|
39 |
+
|
40 |
+
You can use the model with the `transformers` library as follows. The `EOSLogitsBiasProcessor` can be helpful if the model has trouble generating the end-of-sequence token.
|
41 |
+
|
42 |
+
```python
|
43 |
+
from transformers import AutoTokenizer, LogitsProcessor, LogitsProcessorList, AutoModelForImageTextToText
|
44 |
+
import torch
|
45 |
+
|
46 |
+
model_id = "nectec/thai-research-gemma-3-27b-it"
|
47 |
+
|
48 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
49 |
+
model_id, device_map="auto", torch_dtype=torch.bfloat16
|
50 |
+
).eval()
|
51 |
+
|
52 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
53 |
+
|
54 |
+
class EOSLogitsBiasProcessor(LogitsProcessor):
|
55 |
+
def __init__(self,tokenizer, eos_token_id, bias_value=5.0, space_bias = -0.3):
|
56 |
+
self.eos_token_id = eos_token_id
|
57 |
+
self.bias_value = bias_value
|
58 |
+
self.space_bias = space_bias
|
59 |
+
def __call__(self, input_ids, scores):
|
60 |
+
scores[:, self.eos_token_id] += self.bias_value
|
61 |
+
scores[:,107] += 0.3
|
62 |
+
return scores
|
63 |
+
|
64 |
+
logits_processor = LogitsProcessorList([
|
65 |
+
EOSLogitsBiasProcessor(tokenizer,eos_token_id=tokenizer.eos_token_id, bias_value=10.0)
|
66 |
+
])
|
67 |
+
|
68 |
+
messages = [
|
69 |
+
{
|
70 |
+
"role": "system",
|
71 |
+
"content": [{"type": "text", "text": "You are a helpful Thai assistant. You are Pathumma LLM AI that build by National Electronics and Computer Technology Center (NECTEC)."}]
|
72 |
+
},
|
73 |
+
{
|
74 |
+
"role": "user",
|
75 |
+
"content": [
|
76 |
+
{"type": "text", "text": "ขอสูตรส้มตำหน่อย"}
|
77 |
+
]
|
78 |
+
},
|
79 |
+
]
|
80 |
+
|
81 |
+
inputs = tokenizer.apply_chat_template(
|
82 |
+
messages, add_generation_prompt=True, tokenize=True,
|
83 |
+
return_dict=True, return_tensors="pt"
|
84 |
+
).to(model.device)
|
85 |
+
|
86 |
+
input_len = inputs["input_ids"].shape[-1]
|
87 |
+
|
88 |
+
with torch.inference_mode():
|
89 |
+
generation = model.generate(
|
90 |
+
**inputs,
|
91 |
+
max_new_tokens=512,
|
92 |
+
do_sample=True,
|
93 |
+
num_beams=2,
|
94 |
+
repetition_penalty=1.1,
|
95 |
+
temperature=0.4,
|
96 |
+
logits_processor=logits_processor, # add if it have a problem not generate eos token
|
97 |
+
)
|
98 |
+
|
99 |
+
generated_text = tokenizer.decode(generation[0][input_len:], skip_special_tokens=True)
|
100 |
+
print(generated_text)
|
101 |
+
```
|
102 |
+
|
103 |
+
#### Running with `llama.cpp`
|
104 |
+
|
105 |
+
You can also run a quantized version of the model using `llama-cpp-python` for efficient inference on CPU or GPU.
|
106 |
+
|
107 |
+
```sh
|
108 |
+
%pip install llama-cpp-python transformers
|
109 |
+
```
|
110 |
+
```python
|
111 |
+
import transformers
|
112 |
+
from llama_cpp import Llama
|
113 |
+
|
114 |
+
# Download the GGUF model file
|
115 |
+
!wget -O './thai-research-gemma-3-27b-it-Q4_K_M.gguf' "https://huggingface.co/nectec/thai-research-gemma-3-27b-it/resolve/main/thai-research-gemma-3-27b-it-Q4_K_M.gguf?download=true"
|
116 |
+
|
117 |
+
PROMPT = "You are a helpful Thai assistant. You are Pathumma LLM AI that build by National Electronics and Computer Technology Center (NECTEC)."
|
118 |
+
|
119 |
+
llm = Llama(model_path='thai-research-gemma-3-27b-it-Q4_K_M.gguf', n_gpu_layers=-1, n_ctx=4096,verbose=False)
|
120 |
+
tokenizer = transformers.AutoTokenizer.from_pretrained("nectec/thai-research-gemma-3-27b-it")
|
121 |
+
|
122 |
+
memory = [{'content': PROMPT, 'role': 'system'},]
|
123 |
+
def generate(instuction,memory=memory):
|
124 |
+
memory.append({'content': instuction, 'role': 'user'})
|
125 |
+
p = tokenizer.apply_chat_template(
|
126 |
+
memory,
|
127 |
+
tokenize=False,
|
128 |
+
add_generation_prompt=True
|
129 |
+
)
|
130 |
+
response = llm(
|
131 |
+
p,
|
132 |
+
max_tokens=4096,
|
133 |
+
temperature=0.4, # ความคิดสร้างสรรค์ถ้าใส่น้อยจะตอบตรงคำถาม ถ้าใส่เยอะสร้างสรรค์แต่อาจจะผิดหรือไม่ตรงประเด็น
|
134 |
+
repeat_penalty=1.1, # ป้องกันการตอบคำซ้ำ
|
135 |
+
stop=["<end_of_turn>"]
|
136 |
+
)
|
137 |
+
output = response['choices'][0]['text']
|
138 |
+
memory.append({'content': output, 'role': 'assistant'})
|
139 |
+
return output
|
140 |
+
|
141 |
+
print(generate("ขอสูตรทำส้มตำ",memory=memory))
|
142 |
+
```
|
143 |
+
### Citation
|
144 |
+
|
145 |
+
If you use this model, please cite the base Gemma model and acknowledge NECTEC's contribution.
|
146 |
+
```none
|
147 |
+
@article{gemma_2025,
|
148 |
+
title={Gemma 3},
|
149 |
+
url={https://goo.gle/Gemma3Report},
|
150 |
+
publisher={Kaggle},
|
151 |
+
author={Gemma Team},
|
152 |
+
year={2025}
|
153 |
+
}
|
154 |
+
```
|