Views
No views yet
amanuelbyte/Amharic_dataset.trust_remote_code=True to load.HRMText1 class from the hrm_model.py file.1import torch
2from transformers import T5Tokenizer
3from huggingface_hub import hf_hub_download
4from hrm_model import HRMText1 # Import the custom class
5import json
6
7# Replace with your repo ID
8repo_id = "amanuelbyte/HRM-amharic"
9device = "cuda" if torch.cuda.is_available() else "cpu"
10
11# 1. Load the tokenizer
12tokenizer = T5Tokenizer.from_pretrained(repo_id)
13
14# 2. Load the model's configuration
15config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
16with open(config_path, 'r') as f:
17 config = json.load(f)
18
19# 3. Instantiate the model with the config
20# The trust_remote_code=True is not strictly needed here because we import manually,
21# but it's good practice for custom models.
22model = HRMText1(config)
23
24# 4. Load the model weights
25weights_path = hf_hub_download(repo_id=repo_id, filename="pytorch_model.bin")
26state_dict = torch.load(weights_path, map_location=device)
27model.load_state_dict(state_dict)
28model.to(device)
29model.eval()
30
31print("Model loaded successfully!")
32
33# Now you can use the model for generation...
34prompt = "የኢትዮጵያ ዋና ከተማ"
35input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
36
37with torch.inference_mode():
38 output_ids = model.generate(input_ids, max_new_tokens=50) # Assuming a generate method exists
39
40print(tokenizer.decode(output_ids, skip_special_tokens=True))