Views
No views yet
pip install torch sentencepiece huggingface-hub1import torch
2import sentencepiece as sp
3from huggingface_hub import hf_hub_download
4import numpy as np
5
6# Step 1: Download and load the tokenizer
7tokenizer_path = hf_hub_download(
8 repo_id="rupakrpk93/odia_tokenizers_test",
9 filename="odia_tokenizer.model"
10)
11
12tokenizer = sp.SentencePieceProcessor()
13tokenizer.load(tokenizer_path)
14
15# Step 2: Download model files
16model_path = hf_hub_download(
17 repo_id="rupakrpk93/odia_tokenizers_test",
18 filename="pytorch_model.bin"
19)
20
21config_path = hf_hub_download(
22 repo_id="rupakrpk93/odia_tokenizers_test",
23 filename="config.json"
24)
25
26# Step 3: Load the model architecture and weights
27# First, download the model architecture file
28architecture_path = hf_hub_download(
29 repo_id="rupakrpk93/odia_tokenizers_test",
30 filename="model_architecture.py"
31)
32
33# Import the model classes
34import sys
35import importlib.util
36spec = importlib.util.spec_from_file_location("model_architecture", architecture_path)
37model_module = importlib.util.module_from_spec(spec)
38sys.modules["model_architecture"] = model_module
39spec.loader.exec_module(model_module)
40
41# Import the classes we need
42GPTConfig = model_module.GPTConfig
43GPT = model_module.GPT
44
45# Create model configuration
46config = GPTConfig()
47
48# Initialize and load the model
49device = "cuda" if torch.cuda.is_available() else "cpu"
50model = GPT(config)
51
52# Load the pretrained weights
53checkpoint = torch.load(model_path, map_location=device)
54
55# Check if the state_dict is nested and extract it if necessary
56if isinstance(checkpoint, dict) and 'model' in checkpoint:
57 state_dict = checkpoint['model']
58else:
59 state_dict = checkpoint
60
61# Remove the 'model.' prefix from keys if present
62from collections import OrderedDict
63new_state_dict = OrderedDict()
64for k, v in state_dict.items():
65 if k.startswith('model.'):
66 new_state_dict[k[6:]] = v # Remove 'model.' prefix
67 else:
68 new_state_dict[k] = v
69
70model.load_state_dict(new_state_dict)
71
72model = model.to(device)
73model.eval()
74print(f"Model loaded successfully on {device}")
75
76# Step 4: Generate text function
77def generate_odia_text(prompt, max_length=100, temperature=0.8):
78 # Encode the prompt
79 input_ids = tokenizer.encode_as_ids(prompt)
80 input_tensor = torch.tensor(input_ids).unsqueeze(0).to(device)
81
82 # Generate
83 with torch.no_grad():
84 output = model.generate(input_tensor, max_length, temperature=temperature)
85
86 # Decode the output
87 generated_text = tokenizer.decode(output.squeeze().tolist())
88 return generated_text1# Example 1: Simple text generation
2prompt = "ସେ କାଲି ସ୍କୁଲକୁ"
3generated_text = generate_odia_text(prompt, max_length=200)
4print(f"Prompt: {prompt}")
5print(f"Generated: {generated_text}")
6
7# Example 2: Encode and decode text
8text = "ଓଡିଆ ଭାଷା ଏକ ସୁନ୍ଦର ଭାଷା"
9encoded = tokenizer.encode_as_ids(text)
10print(f"Original: {text}")
11print(f"Encoded: {encoded}")
12
13decoded = tokenizer.decode(encoded)
14print(f"Decoded: {decoded}")1@misc{odia_gpt_2024,
2 title={Odia GPT Language Model},
3 author={Your Name},
4 year={2024},
5 publisher={HuggingFace}
6}