Views
No views yet
1import torch
2
3# Define your custom model class
4class BigramLanguageModel(nn.Module):
5 # Include the complete definition of your BigramLanguageModel here
6
7 # Example method definitions:
8 def __init__(self):
9 super().__init__()
10 # Define your model layers here as per the training setup
11 # Example:
12 # self.token_embedding_table = nn.Embedding(vocab_size, n_embd)
13 # self.position_embedding_table = nn.Embedding(block_size, n_embd)
14 # self.blocks = nn.Sequential(*[Block(n_embd, n_head=n_head) for _ in range(n_layer)])
15 # self.ln_f = nn.LayerNorm(n_embd)
16 # self.lm_head = nn.Linear(n_embd, vocab_size)
17
18 def forward(self, idx, targets=None):
19 # Define the forward pass as per your model
20 pass
21
22 def generate(self, idx, max_new_tokens):
23 # Implement the generate method for text generation
24 pass
25
26# Load the model weights from Hugging Face
27model = BigramLanguageModel()
28model_url = "https://huggingface.co/yoonusajwardapiit/triptuner/resolve/main/pytorch_model.bin"
29model_weights = torch.hub.load_state_dict_from_url(model_url, map_location=torch.device('cpu'), weights_only=True)
30model.load_state_dict(model_weights)
31model.eval()
32
33# Define your character mappings
34chars = sorted(list(set("your_training_text_here"))) # Replace with the actual character set used in training
35stoi = {ch: i for i, ch in enumerate(chars)}
36itos = {i: ch for i, ch in enumerate(chars)}
37encode = lambda s: [stoi[c] for c in s]
38decode = lambda l: ''.join([itos[i] for i in l])
39
40# Test the model with a sample prompt
41prompt = "Hanthana" # Replace with any relevant location or prompt
42context = torch.tensor([encode(prompt)], dtype=torch.long)
43
44# Generate text using the model
45with torch.no_grad():
46 generated = model.generate(context, max_new_tokens=250) # Adjust the number of new tokens as needed
47
48# Decode and print the generated text
49generated_text = decode(generated[0].tolist())
50print(generated_text)
51
52
53## Training Data
54
55The model was trained on a dataset containing information about various locations in Sri Lanka's Central Province.
56
57## Model Architecture
58
59- Number of Layers: 4
60- Embedding Size: 64
61- Number of Heads: 4
62- Context Length: 32 tokens
63
64## License
65
66MIT License