Views
No views yet
google/gemma-3-270m, designed to generate creative and coherent Dhivehi content based on prompts, titles, or instructions.google/gemma-3-270m-it1from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
2import torch
3
4# Load model
5from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
6import torch
7
8# Load model
9model_path = "alakxender/gemma-3-270m-dhivehi-text-gen"
10model = AutoModelForCausalLM.from_pretrained(
11 model_path,
12 torch_dtype="auto",
13 device_map="auto",
14 attn_implementation="eager"
15)
16tokenizer = AutoTokenizer.from_pretrained(model_path)
17pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
18
19# Generate content
20title_or_prompt = "ދިވެހިރާއްޖެއަކީ އިންޑިޔާ ކަނޑުގައި އޮންނަ ޖަޒީރާ ޤައުމެކެވެ"
21
22# Create the prompt format used during training
23prompt = f"Create a dhivehi article for the following topic: {title_or_prompt}"
24
25# Create chat format message for content generation (matching training format)
26messages = [
27 {"role": "system", "content": "You are a helpful assistant that can generate dhivehi articles based on a given topic."},
28 {"role": "user", "content": prompt}
29]
30
31# Apply chat template
32formatted_prompt = pipe.tokenizer.apply_chat_template(
33 messages,
34 tokenize=False,
35 add_generation_prompt=True
36)
37
38# Generation parameters
39gen_kwargs = {
40 "max_new_tokens": 256,
41 "temperature": 0.7,
42 "top_p": 0.9,
43 "top_k": 50,
44 "do_sample": True,
45 "disable_compile": True,
46 "pad_token_id": tokenizer.eos_token_id
47}
48
49# Generate content
50outputs = pipe(formatted_prompt, **gen_kwargs)
51
52# Extract generated content (remove the prompt)
53generated_content = outputs[0]['generated_text'][len(formatted_prompt):].strip()
54
55print(f"Generated content: {generated_content}")max_new_tokens: Controls the length of generated text (64-512 recommended)temperature: Controls randomness (0.1-1.0, higher = more creative)do_sample: Boolean flag to enable/disable sampling