File size: 6,761 Bytes
b84d85a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python3
"""
Example: Loading Codsworth from Hugging Face Hub

After uploading to Hugging Face, users can load the model using this script.
"""

# ========================
# QUICK START (After Upload)
# ========================

"""
# Option 1: Using Hugging Face Transformers (if converted)

from transformers import AutoModel, AutoTokenizer

model_name = "your-username/codsworth"  # Change to your username
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)

# Generate
inputs = tokenizer("Hello world", return_tensors="pt")
outputs = model(**inputs)

# Option 2: Using Codsworth directly (recommended for this implementation)

import torch
import json

# Download from HF
from huggingface_hub import hf_hub_download

config_path = hf_hub_download(repo_id="your-username/codsworth", filename="config.json")
model_path = hf_hub_download(repo_id="your-username/codsworth", filename="codsworth_model.pt")
tokenizer_path = hf_hub_download(repo_id="your-username/codsworth", filename="tokenizer.json")

# Load locally
import sys
sys.path.insert(0, '/path/to/codsworth')

from codsworth.config import CodsworthConfig
from codsworth.model import CodsworthTransformer

with open(config_path) as f:
    cfg = json.load(f)["model"]

config = CodsworthConfig(**cfg)
model = CodsworthTransformer(config)
model.load_state_dict(torch.load(model_path, map_location="cpu"))
model.eval()

with open(tokenizer_path) as f:
    vocab = json.load(f)
id_to_word = {v: k for k, v in vocab.items()}

# Generate
def generate(prompt):
    words = prompt.lower().split()
    ids = [vocab.get(w, vocab["<unk>"]) for w in words]
    
    for _ in range(50):
        inp = ids[-128:] + [0] * max(0, 128-len(ids))
        with torch.no_grad():
            logits = model(torch.tensor([inp]))["logits"]
            next_tok = torch.multinomial(torch.softmax(logits[0,-1], -1), 1).item()
        ids.append(next_tok)
    
    return " ".join(id_to_word.get(t, "<unk>") for t in ids)

print(generate("hello"))
"""


# ========================
# FULL EXAMPLE SCRIPT
# ========================

import argparse
import json
import os
import sys

import torch

# Check for transformers
try:
    from transformers import AutoTokenizer
    HAS_TRANSFORMERS = True
except ImportError:
    HAS_TRANSFORMERS = False

try:
    from huggingface_hub import hf_hub_download
    HAS_HF_HUB = True
except ImportError:
    HAS_HF_HUB = False


def load_from_huggingface(
    repo_id: str = "your-username/codsworth",
    device: str = "cpu",
):
    """
    Load Codsworth model from Hugging Face Hub.
    
    Args:
        repo_id: Your HF repo ID (e.g., "jaqrshanahan/codsworth")
        device: "cpu" or "cuda"
        
    Returns:
        model, vocab, id_to_word, config
    """
    
    if not HAS_HF_HUB:
        print("Installing huggingface_hub...")
        os.system("pip install huggingface_hub")
        from huggingface_hub import hf_hub_download
    
    print(f"Downloading from https://huggingface.co/{repo_id}")
    
    # Download files
    config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
    model_path = hf_hub_download(repo_id=repo_id, filename="codsworth_model.pt")
    tokenizer_path = hf_hub_download(repo_id=repo_id, filename="tokenizer.json")
    
    print("Files downloaded!")
    
    # Load config
    with open(config_path) as f:
        config_data = json.load(f)
    
    # Add to path (adjust path to your local codsworth)
    sys.path.insert(0, ".")
    
    from codsworth.config import CodsworthConfig
    from codsworth.model import CodsworthTransformer
    
    # Create config
    model_cfg = config_data["model"]
    config = CodsworthConfig(
        vocab_size=model_cfg["vocab_size"],
        context_length=model_cfg["context_length"],
        embedding_dim=model_cfg["embedding_dim"],
        num_layers=model_cfg["num_layers"],
        num_heads=model_cfg["num_heads"],
        ffn_hidden_dim=model_cfg["ffn_hidden_dim"],
        use_rope=model_cfg["use_rope"],
        rope_theta=model_cfg["rope_theta"],
    )
    
    # Load model
    model = CodsworthTransformer(config)
    model.load_state_dict(torch.load(model_path, map_location=device))
    model.to(device)
    model.eval()
    
    # Load tokenizer
    with open(tokenizer_path) as f:
        vocab = json.load(f)
    id_to_word = {v: k for k, v in vocab.items()}
    
    print(f"Loaded! Parameters: {model.get_num_params():,}")
    
    return model, vocab, id_to_word, config


def generate_text(
    model,
    vocab,
    id_to_word,
    prompt: str,
    max_tokens: int = 50,
    temperature: float = 1.0,
    device: str = "cpu",
) -> str:
    """Generate text from prompt."""
    
    words = prompt.lower().split()
    ids = [vocab.get(w, vocab["<unk>"]) for w in words]
    
    for _ in range(max_tokens):
        input_seq = ids[-model.config.context_length:]
        if len(input_seq) < model.config.context_length:
            input_seq = [vocab["<pad>"]] * (model.config.context_length - len(input_seq)) + input_seq
        
        with torch.no_grad():
            inp = torch.tensor([input_seq], dtype=torch.long).to(device)
            logits = model(inp)["logits"]
            next_logits = logits[0, -1, :] / temperature
            probs = torch.softmax(next_logits, dim=-1)
            next_token = torch.multinomial(probs, 1).item()
        
        ids.append(next_token)
        
        if next_token == vocab.get("<eos>", 2):
            break
    
    return " ".join(id_to_word.get(t, "<unk>") for t in ids)


def main():
    parser = argparse.ArgumentParser(description="Load Codsworth from Hugging Face")
    parser.add_argument("--repo", type=str, default="your-username/codsworth",
                       help="Hugging Face repo ID")
    parser.add_argument("--prompt", type=str, default="hello world",
                       help="Prompt for generation")
    parser.add_argument("--tokens", type=int, default=50,
                       help="Max tokens to generate")
    parser.add_argument("--temp", type=float, default=1.0,
                       help="Temperature")
    parser.add_argument("--cuda", action="store_true",
                       help="Use GPU")
    
    args = parser.parse_args()
    
    device = "cuda" if args.cuda and torch.cuda.is_available() else "cpu"
    
    print("=" * 50)
    print("Loading Codsworth from Hugging Face Hub")
    print("=" * 50)
    
    model, vocab, id_to_word, config = load_from_huggingface(args.repo, device)
    
    print(f"\nGenerating from: '{args.prompt}'")
    result = generate_text(model, vocab, id_to_word, args.prompt, args.tokens, device)
    
    print(f"\nResult:\n{result}")


if __name__ == "__main__":
    main()