Views
No views yet
google/gemma-3-270m-it, tuned to follow instruction + optional input (context) and produce concise, helpful answers in Dhivehi.google/gemma-3-270m-itinstruction: the user request/questioninput (optional): supporting context the model should rely on1from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
2
3model_path = "alakxender/gemma-3-270m-dhivehi-ctx"
4
5model = AutoModelForCausalLM.from_pretrained(
6 model_path,
7 torch_dtype="auto",
8 device_map="auto",
9 attn_implementation="eager"
10)
11tok = AutoTokenizer.from_pretrained(model_path)
12pipe = pipeline("text-generation", model=model, tokenizer=tok)
13
14system_prompt = "You are a Dhivehi assistant. You are helpful and friendly."
15instruction = "ދިވެހިރާއްޖޭގެ ޖޯގްރަފީ އާއި ބެހޭ ގޮތުން ލިޔެފައިވާ މުހިންމު ނުކުތާތަކަކީ ކޮބާ؟"
16input_text = """ދިވެހިރާއްޖެއަކީ އިންޑިޔާ ކަނޑުގައި އޮންނަ ޖަޒީރާ ޤައުމެކެވެ. ދިވެހިރާއްޖެ އޮންނަނީ ސްރިލަންކާގެ ހުޅުނަގުގައި ދެކުނު އޭޝިޔާ ބައްރުގައެވެ. ދިވެހިރާއްޖެއަކީ މިނިވަންކަމާއި އިސްތިޤުލާލު ދިފާޢުކޮށް ރިއްކާތެރިކުރަމުން އަންނަ، ދިވެހީންގެ މިލްކުވެރި ސަރަހައްދެވެ."""
17
18messages = [
19 {"role": "system", "content": system_prompt},
20 {"role": "user", "content": f"instruction: {instruction}\n\ninput: {input_text}"},
21]
22
23do_sample = False
24if do_sample == True:
25 print("Using sampling parameters")
26 # Use sampling parameters only when do_sample is True
27 gen_kwargs = {
28 "max_new_tokens": 256,
29 "temperature": 0.2,
30 "top_p": 0.2,
31 "top_k": 5,
32 "do_sample": True,
33 "disable_compile": True
34 }
35else:
36 print("Using greedy decoding")
37 # Use only valid parameters for greedy decoding
38 gen_kwargs = {
39 "max_new_tokens": 256,
40 "disable_compile": True
41 }
42out = pipe(messages, **gen_kwargs)
43assistant = next((m["content"] for m in out[0]["generated_text"] if m["role"] == "assistant"), "")
44print(assistant)
45
46#Response: ދިވެހިރާއްޖެ އަކީ އިންޑިޔާ ކަނޑުގައި އޮންނަ ޖަޒީރާއެއް ކަމަށާއި، ދިވެހިރާއްޖެ އަކީ މުޅި ދިވެހިރާއްޖެއަށް ބޮޑެތި ތަރައްޤީތަކެއް ލިބިފައިވާ ޤައުމެއް ކަމަށެވެ.1from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
2
3model_path = "alakxender/gemma-3-270m-dhivehi-ctx"
4
5model = AutoModelForCausalLM.from_pretrained(
6 model_path, torch_dtype="auto", device_map="auto", attn_implementation="eager"
7)
8tok = AutoTokenizer.from_pretrained(model_path)
9gen = pipeline("text-generation", model=model, tokenizer=tok)
10
11instruction = """ދިވެހިންގެ މައިގަނޑު ސިފަތަކަކީ ކޮބައިހެއްޔެވެ؟"""
12input_text = """ދިވެހިންނަކީ ދިވެހިރާއްޖޭގެ އަހުލުވެރިންނެވެ. ކޮންމެ ދިވެއްސަކީ މުސްލިމެކެވެ. ދިވެހީންގެ ނަސްލަކީ އެތައް ސަތޭކަ ޤަރުނެއް ކަނޑައްތުކޮށް ދިވެހި ކާބާފައިންގެ ފަރާތުން ވާރުތަވެފައިވާ މުސްތަޤިއްލު ނަސްލެކެވެ. ދިވެހިންނަކީ އަމިއްލަ މިނިވަން ބަހަކުން ވާހަކަދައްކަމުންދާ، ޞައްޙަ އިސްލާމީ ޢަޤީދާގެ މަތީގައި ތިބި، އަމިއްލަ ބަހެއް ލިބިފައިވާ ބައެކެވެ."""
13
14user_only = [{"role": "user", "content": f"{instruction}\n\n{input_text}"}]
15prompt = tok.apply_chat_template(user_only, tokenize=False, add_generation_prompt=True)
16
17do_sample = False
18if do_sample == True:
19 # Use sampling parameters only when do_sample is True
20 gen_kwargs = {
21 "max_new_tokens": 256,
22 "temperature": 0.2,
23 "top_p": 0.2,
24 "top_k": 5,
25 "do_sample": True,
26 "disable_compile": True
27 }
28else:
29 # Use only valid parameters for greedy decoding
30 gen_kwargs = {
31 "max_new_tokens": 256,
32 "disable_compile": True
33 }
34
35resp = gen(prompt, **gen_kwargs)
36answer = resp[0]["generated_text"][len(prompt):].strip()
37print(answer)
38
39#Response: ދިވެހިންނަކީ ދިވެހިރާއްޖޭގެ އަހުލުވެރިންނެވެ. ދިވެހީންގެ ނަސްލަކީ އެތައް ސަތޭކަ ޤަރުނެއް ކަނޑައްތުކޮށް ދިވެހި ކާބާފައިންގެ ފަރާތުން ވާރުތަވެފައިވާ މުސްތަޤިއްލު ނަސްލެކެވެ. ދިވެހިންނަކީ އަމިއްލަ މިނިވަން ބަހަކުން ވާހަކަދައްކަމުންދާ، ޞައްޙަ އިސްލާމީ ޢަޤީދާގެ މަތީގައި ތިބި ބައެކެވެ.do_sample=False for faithful, deterministic answers to provided context.temperature≈0.2–0.7, top_p≈0.2–0.9.max_new_tokens modest (e.g., 128–384) for focused outputs.