|
import gradio as gr |
|
import torch |
|
import os |
|
|
|
|
|
from model_gpt import GPT, GPTConfig |
|
|
|
|
|
device = 'cuda' if torch.cuda.is_available() else 'cpu' |
|
|
|
|
|
|
|
checkpoint = torch.load('ckpt.pt', 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) |
|
|
|
|
|
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]) |
|
|
|
|
|
data = torch.tensor(encode(text), dtype=torch.long) |
|
|
|
|
|
def generate_output(length): |
|
context = torch.zeros((1, 1), dtype=torch.long, device=device) |
|
output_sequence = decode(model.generate(context, max_new_tokens=length)[0].tolist()) |
|
return output_sequence |
|
|
|
|
|
title = "Shakespeare Text Generation" |
|
description = "Model that generates text in the style of William Shakespeare." |
|
|
|
demo = gr.Interface( |
|
fn = generate_output, |
|
inputs = [gr.Number(value = 50,label = "Sequence Length",info = "Length of the sample sequence you wish to generate.")], |
|
outputs = [gr.TextArea(lines = 5,label="Sequence Output")], |
|
title = title, |
|
description = description |
|
) |
|
|
|
|
|
demo.launch() |