Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,76 @@
|
|
1 |
-
import torch
|
2 |
-
import tiktoken
|
3 |
-
from model import GPT, GPTConfig
|
4 |
-
import gradio as gr
|
5 |
-
from torch.nn import functional as F
|
6 |
-
|
7 |
-
device = "cpu"
|
8 |
-
if torch.cuda.is_available():
|
9 |
-
device = "cuda"
|
10 |
-
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
|
11 |
-
device = "mps"
|
12 |
-
|
13 |
-
# STOP
|
14 |
-
num_return_sequences = 1
|
15 |
-
# max_length = 100
|
16 |
-
model = GPT(GPTConfig())
|
17 |
-
model.to(device)
|
18 |
-
model.load_state_dict(torch.load('./checkpoints/final_model.pth', map_location=device))
|
19 |
-
|
20 |
-
# Set the model to evaluation mode
|
21 |
-
model.eval()
|
22 |
-
|
23 |
-
|
24 |
-
def generate(text, max_length):
|
25 |
-
enc = tiktoken.get_encoding("gpt2")
|
26 |
-
tokens = enc.encode(text)
|
27 |
-
tokens = torch.tensor(tokens, dtype= torch.long) # (len,) #check tiktoken app
|
28 |
-
tokens = tokens.unsqueeze(0).repeat(num_return_sequences, 1) # (1, len)
|
29 |
-
x = tokens.to(device)
|
30 |
-
|
31 |
-
while x.size(1) < max_length:
|
32 |
-
# forward the model to get the logits
|
33 |
-
with torch.no_grad():
|
34 |
-
logits = model(x)[0] # (B, T, vocab_size)
|
35 |
-
# take the logits at the last position
|
36 |
-
logits = logits[:, -1, :] # (B, vocab_size)
|
37 |
-
# get the probabilities
|
38 |
-
probs = F.softmax(logits, dim=-1)
|
39 |
-
# do top-k sampling of 50 (huggingface pipeline default)
|
40 |
-
# topk_probs here becomes (5, 50), topk_indices is (5, 50)
|
41 |
-
topk_probs, topk_indices = torch.topk(probs, 50, dim=-1)
|
42 |
-
# select a token from the top-k probabilities
|
43 |
-
# note: multinomial does not demand the input to sum to 1
|
44 |
-
ix = torch.multinomial(topk_probs, 1) # (B, 1)
|
45 |
-
# gather the corresponding indices
|
46 |
-
xcol = torch.gather(topk_indices, -1, ix) # (B, 1)
|
47 |
-
# append to the sequence
|
48 |
-
x = torch.cat((x, xcol), dim=1)
|
49 |
-
|
50 |
-
# print the generated text
|
51 |
-
for i in range(num_return_sequences):
|
52 |
-
tokens = x[i, :max_length].tolist()
|
53 |
-
decoded = enc.decode(tokens)
|
54 |
-
return decoded
|
55 |
-
|
56 |
-
title = "Shakespeare Poem generation using GPT - 121M Model."
|
57 |
-
description = "A simple Gradio interface to demo genaration of shakespeare poem."
|
58 |
-
examples = [["Let us kill him, and we'll have corn at our own price."],
|
59 |
-
["Would you proceed especially against Caius Marcius?"],
|
60 |
-
["Nay, but speak not maliciously."]]
|
61 |
-
demo = gr.Interface(
|
62 |
-
generate,
|
63 |
-
inputs=[
|
64 |
-
gr.TextArea(label="Enter text"),
|
65 |
-
gr.Slider(10, 100, value = 10, step=1, label="Token Length"),
|
66 |
-
],
|
67 |
-
outputs=[
|
68 |
-
gr.TextArea(label="Generated Text")
|
69 |
-
],
|
70 |
-
title=title,
|
71 |
-
description=description,
|
72 |
-
examples=examples,
|
73 |
-
cache_examples=False,
|
74 |
-
live=True
|
75 |
-
)
|
76 |
demo.launch()
|
|
|
1 |
+
import torch
|
2 |
+
import tiktoken
|
3 |
+
from model import GPT, GPTConfig
|
4 |
+
import gradio as gr
|
5 |
+
from torch.nn import functional as F
|
6 |
+
|
7 |
+
device = "cpu"
|
8 |
+
if torch.cuda.is_available():
|
9 |
+
device = "cuda"
|
10 |
+
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
|
11 |
+
device = "mps"
|
12 |
+
|
13 |
+
# STOP
|
14 |
+
num_return_sequences = 1
|
15 |
+
# max_length = 100
|
16 |
+
model = GPT(GPTConfig())
|
17 |
+
model.to(device)
|
18 |
+
model.load_state_dict(torch.load('./checkpoints/final_model.pth', map_location=device))
|
19 |
+
|
20 |
+
# Set the model to evaluation mode
|
21 |
+
model.eval()
|
22 |
+
|
23 |
+
|
24 |
+
def generate(text, max_length):
|
25 |
+
enc = tiktoken.get_encoding("gpt2")
|
26 |
+
tokens = enc.encode(text)
|
27 |
+
tokens = torch.tensor(tokens, dtype= torch.long) # (len,) #check tiktoken app
|
28 |
+
tokens = tokens.unsqueeze(0).repeat(num_return_sequences, 1) # (1, len)
|
29 |
+
x = tokens.to(device)
|
30 |
+
|
31 |
+
while x.size(1) < max_length:
|
32 |
+
# forward the model to get the logits
|
33 |
+
with torch.no_grad():
|
34 |
+
logits = model(x)[0] # (B, T, vocab_size)
|
35 |
+
# take the logits at the last position
|
36 |
+
logits = logits[:, -1, :] # (B, vocab_size)
|
37 |
+
# get the probabilities
|
38 |
+
probs = F.softmax(logits, dim=-1)
|
39 |
+
# do top-k sampling of 50 (huggingface pipeline default)
|
40 |
+
# topk_probs here becomes (5, 50), topk_indices is (5, 50)
|
41 |
+
topk_probs, topk_indices = torch.topk(probs, 50, dim=-1)
|
42 |
+
# select a token from the top-k probabilities
|
43 |
+
# note: multinomial does not demand the input to sum to 1
|
44 |
+
ix = torch.multinomial(topk_probs, 1) # (B, 1)
|
45 |
+
# gather the corresponding indices
|
46 |
+
xcol = torch.gather(topk_indices, -1, ix) # (B, 1)
|
47 |
+
# append to the sequence
|
48 |
+
x = torch.cat((x, xcol), dim=1)
|
49 |
+
|
50 |
+
# print the generated text
|
51 |
+
for i in range(num_return_sequences):
|
52 |
+
tokens = x[i, :max_length].tolist()
|
53 |
+
decoded = enc.decode(tokens)
|
54 |
+
return decoded
|
55 |
+
|
56 |
+
title = "Shakespeare Poem generation using GPT - 121M Model."
|
57 |
+
description = "A simple Gradio interface to demo genaration of shakespeare poem."
|
58 |
+
examples = [["Let us kill him, and we'll have corn at our own price."],
|
59 |
+
["Would you proceed especially against Caius Marcius?"],
|
60 |
+
["Nay, but speak not maliciously."]]
|
61 |
+
demo = gr.Interface(
|
62 |
+
generate,
|
63 |
+
inputs=[
|
64 |
+
gr.TextArea(label="Enter text"),
|
65 |
+
gr.Slider(10, 100, value = 10, step=1, label="Token Length"),
|
66 |
+
],
|
67 |
+
outputs=[
|
68 |
+
gr.TextArea(label="Generated Text")
|
69 |
+
],
|
70 |
+
title=title,
|
71 |
+
description=description,
|
72 |
+
examples=examples,
|
73 |
+
cache_examples=False,
|
74 |
+
live=True
|
75 |
+
)
|
76 |
demo.launch()
|