|
import os |
|
import pickle |
|
from contextlib import nullcontext |
|
import torch |
|
import tiktoken |
|
from model import GPTConfig, GPT |
|
import gradio as gr |
|
|
|
def nanogpt(start:str , max_new_tokens = 500, num_samples =2): |
|
|
|
|
|
|
|
init_from = 'resume' |
|
out_dir = 'out-shakespeare-char' |
|
|
|
|
|
|
|
temperature = 0.8 |
|
top_k = 200 |
|
seed = 1337 |
|
device = 'cpu' |
|
dtype = 'bfloat16' if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else 'float16' |
|
compile = False |
|
|
|
|
|
|
|
torch.manual_seed(seed) |
|
torch.cuda.manual_seed(seed) |
|
torch.backends.cuda.matmul.allow_tf32 = True |
|
torch.backends.cudnn.allow_tf32 = True |
|
device_type = 'cuda' if 'cuda' in device else 'cpu' |
|
ptdtype = {'float32': torch.float32, 'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype] |
|
ctx = nullcontext() if device_type == 'cpu' else torch.amp.autocast(device_type=device_type, dtype=ptdtype) |
|
|
|
|
|
if init_from == 'resume': |
|
|
|
ckpt_path = 'ckpt.pt' |
|
checkpoint = torch.load(ckpt_path, map_location=device) |
|
gptconf = GPTConfig(**checkpoint['model_args']) |
|
model = GPT(gptconf) |
|
state_dict = checkpoint['model'] |
|
unwanted_prefix = '_orig_mod.' |
|
for k,v in list(state_dict.items()): |
|
if k.startswith(unwanted_prefix): |
|
state_dict[k[len(unwanted_prefix):]] = state_dict.pop(k) |
|
model.load_state_dict(state_dict) |
|
|
|
|
|
model.eval() |
|
model.to(device) |
|
if compile: |
|
model = torch.compile(model) |
|
|
|
|
|
load_meta = False |
|
if init_from == 'resume' and 'config' in checkpoint and 'dataset' in checkpoint['config']: |
|
meta_path = os.path.join('data', checkpoint['config']['dataset'], 'meta.pkl') |
|
load_meta = os.path.exists(meta_path) |
|
if load_meta: |
|
print(f"Loading meta from {meta_path}...") |
|
with open(meta_path, 'rb') as f: |
|
meta = pickle.load(f) |
|
|
|
stoi, itos = meta['stoi'], meta['itos'] |
|
encode = lambda s: [stoi[c] for c in s] |
|
decode = lambda l: ''.join([itos[i] for i in l]) |
|
else: |
|
|
|
print("No meta.pkl found, assuming GPT-2 encodings...") |
|
enc = tiktoken.get_encoding("gpt2") |
|
encode = lambda s: enc.encode(s, allowed_special={"<|endoftext|>"}) |
|
decode = lambda l: enc.decode(l) |
|
|
|
|
|
|
|
|
|
|
|
start_ids = encode(start) |
|
x = (torch.tensor(start_ids, dtype=torch.long, device=device)[None, ...]) |
|
|
|
|
|
with torch.no_grad(): |
|
with ctx: |
|
|
|
y = model.generate(x, max_new_tokens, temperature=temperature, top_k=top_k) |
|
|
|
output = decode(y[0].tolist()) |
|
return output |
|
|
|
INTERFACE = gr.Interface(fn=nanogpt, inputs=[gr.Textbox(label= "Prompt"),gr.Slider(300,500, "number", label= "Maximum number of tokens to be geenrated")] , outputs=gr.Text(label= "Generated Text"), title="NanoGPT", |
|
description="NanoGPT is a large transformer-based language model with 10.65 million parameters, trained on a small dataset of Shakespeare work (size: 1MB only). It is trained with character level tokeniation with a simple objective: predict the next char, given all of the previous chars within some text.", |
|
examples = [['We as the new generation AI enginners.',300,1], |
|
['A forgotten era of humility and happiness',300,2], |
|
|
|
]).launch(debug=True) |