GPT2/chat.py

30 lines
592 B
Python
Raw Permalink Normal View History

2024-07-18 13:38:25 +00:00
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.")