pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
Loading the Model
Method 1: Basic Loading (CPU)
from transformers import AutoModelForCausalLM, AutoTokenizer
Load model and tokenizer
model_name = "ChamaraVishwajithRajapaksha/sinhala-gpt-v.1.0.0"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
print("✓ Model loaded successfully!")
Method 2: GPU Loading (CUDA)
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
Check if CUDA is available
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
Load model on GPU
model_name = "ChamaraVishwajithRajapaksha/sinhala-gpt-v.1.0.0"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16, # Use half precision for faster inference
device_map="auto" # Automatically use GPU if available
)
print(f"✓ Model loaded on {device}!")
Method 3: Low Memory Loading (8-bit Quantization)
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "ChamaraVishwajithRajapaksha/sinhala-gpt-v.1.0.0"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
load_in_8bit=True, # Load in 8-bit precision
device_map="auto"
)
print("✓ Model loaded in 8-bit mode (uses ~4x less memory)!")
Text Generation
Basic Generation
from transformers import AutoModelForCausalLM, AutoTokenizer
Load model
model_name = "ChamaraVishwajithRajapaksha/sinhala-gpt-v.1.0.0"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
for i, output in enumerate(outputs):
text = tokenizer.decode(output, skip_special_tokens=True)
print(f"\nPrompt {i+1}: {prompts[i]}")
print(f"Generated: {text}")
print("-" * 80)
Interactive Generation
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "ChamaraVishwajithRajapaksha/sinhala-gpt-v.1.0.0"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
print("Sinhala GPT Interactive Mode")
print("Type your prompt and press Enter. Type 'quit' to exit.")
print("-" * 80)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
4. Save and Load Model Locally
from transformers import AutoModelForCausalLM, AutoTokenizer
print("Downloading model...")
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
Save to disk
local_path = "./sinhala_gpt_local"
tokenizer.save_pretrained(local_path)
model.save_pretrained(local_path)
print(f"✓ Model saved to {local_path}")
Load from local path (much faster next time)
print("\nLoading from local path...")
tokenizer = AutoTokenizer.from_pretrained(local_path)
model = AutoModelForCausalLM.from_pretrained(local_path)
print("✓ Model loaded from local path!")
5. Pipeline Interface (Simplest Method)
from transformers import pipeline
Create text generation pipeline
generator = pipeline(
"text-generation",
model="ChamaraVishwajithRajapaksha/sinhala-gpt-v.1.0.0",
device=0 # Use GPU (device=0), or -1 for CPU
)
Token indices sequence length is longer than the specified maximum sequence length
Solution:
Truncate long inputs
inputs = tokenizer(
prompt,
return_tensors="pt",
max_length=512,
truncation=True
)
Issue 5: Model Not Found
Error:
OSError: ChamaraVishwajithRajapaksha/sinhala-gpt-v.1.0.0 does not appear to be a valid model identifier
Solutions:
Solution A: Check internet connection
Solution B: Verify model name is correct
Solution C: Try with use_auth_token if model is private
model = AutoModelForCausalLM.from_pretrained(
model_name,
use_auth_token="hf_your_token_here"
)
Solution D: Load from local path if already downloaded
model = AutoModelForCausalLM.from_pretrained("./sinhala_gpt_local")
API Reference
Generation Parameters
Parameter Type Default Description
max_length int 20 Maximum length of generated text
min_length int 0 Minimum length of generated text
temperature float 1.0 Sampling temperature (0.1-2.0). Lower = more focused
top_k int 50 Consider only top-k tokens
top_p float 1.0 Nucleus sampling threshold
repetition_penalty float 1.0 Penalty for repeating tokens (>1.0 reduces repetition)
no_repeat_ngram_size int 0 Prevent repeating n-grams
do_sample bool False Enable sampling (True = random, False = greedy)
num_return_sequences int 1 Number of sequences to generate
early_stopping bool False Stop when EOS token is generated
pad_token_id int None Token ID for padding
eos_token_id int None Token ID for end of sequence
Temperature Guide
Temperature Behavior Use Case
0.1 - 0.3 Very focused, deterministic Factual text, code
0.5 - 0.7 Balanced General purpose
0.8 - 1.0 Creative Stories, poetry
1.1 - 1.5 Very creative, random Brainstorming
Model Information
from transformers import AutoConfig