import gradio as gr from gpt import GPTLanguageModel import torch import config as cfg torch.manual_seed(1337) # wget https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt with open('input.txt', 'r', encoding='utf-8') as f: text = f.read() # here are all the unique characters that occur in this text chars = sorted(list(set(text))) vocab_size = len(chars) # create a mapping from characters to integers 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] # encoder: take a string, output a list of integers decode = lambda l: ''.join([itos[i] for i in l]) # decoder: take a list of integers, output a string model = GPTLanguageModel(vocab_size) model.load_state_dict(torch.load('saved_model.pth')) m = model.to(cfg.device) def inference(input_context, count): encoded_text = [encode(input_context)] count = int(count) context = torch.tensor(encoded_text, dtype=torch.long, device=cfg.device) print('--------------------context = ',context) out_text = decode(m.generate(context, max_new_tokens=count)[0].tolist()) return out_text title = "TSAI S21 Assignment: GPT training on mini shakespeare dataset" description = "A simple Gradio interface that accepts a context and generates shakespere like text " demo = gr.Interface( inference, inputs = [gr.Textbox(placeholder="Enter starting characters"), gr.Textbox(placeholder="Enter number of characters you want to generate")], outputs = [gr.Textbox(label="Shakespeare like generated text")], title = title, description = description ) demo.launch()