import tiktoken import subprocess import time length = input("Completion length: ") length = str(int(length)) # ensure input a valid integer text = input("Text to complete: ") enc = tiktoken.get_encoding("gpt2") tokens = [ str(tok) for tok in enc.encode(text) ] start = time.time() proc = subprocess.Popen( ["./gpt", length, *tokens], stdout=subprocess.PIPE, text=True ) while (line := proc.stdout.readline()): token = int(line) print(enc.decode([token]), end='', flush=True) print() end = time.time() print(f"It took {end - start:.2f}s to complete the text.")