File size: 1,143 Bytes
f7c6887 4791fc3 f7c6887 139373c f7c6887 |
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 |
import gradio as gr
from gpt import GPTLanguageModel
import torch
import config as cfg
torch.manual_seed(1337)
with open('input.txt', 'r', encoding='utf-8') as f:
text = f.read()
chars = sorted(list(set(text)))
vocab_size = len(chars)
stoi = { ch:i for i,ch in enumerate(chars) }
itos = { i:ch for i,ch in enumerate(chars) }
encode = lambda s: [stoi[c] for c in s]
decode = lambda l: ''.join([itos[i] for i in l])
model = GPTLanguageModel(vocab_size)
model.load_state_dict(torch.load('model.pth', map_location=cfg.device))
m = model.to(cfg.device)
def inference(input_text, count):
encoded_text = [encode(input_text)]
count = int(count)
context = torch.tensor(encoded_text, dtype=torch.long, device=cfg.device)
out_text = decode(m.generate(context, max_new_tokens=count)[0].tolist())
return out_text
title = "ERAV1 Session 21"
demo = gr.Interface(
inference,
inputs = [gr.Textbox(label="Text", placeholder="Enter text"), gr.Textbox(label="Tokens", placeholder="Enter number of tokens to be generated")],
outputs = [gr.Textbox(label="Generated text")],
title = title
)
demo.launch() |