|
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('saved_model.pth', map_location=cfg.device)) |
|
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) |
|
|
|
|
|
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() |
|
|
|
|