Spaces:
Sleeping
Sleeping
Kartheekb7
commited on
Commit
•
d208796
1
Parent(s):
b89f4f5
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import tiktoken
|
3 |
+
from model import *
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
enc = tiktoken.get_encoding('gpt2')
|
7 |
+
model = torch.load('model.pt',map_location='cpu')
|
8 |
+
|
9 |
+
|
10 |
+
def response(message = "Hello, I'm a language model", num_return_sequences = 5,max_length = 30,top_k = 50):
|
11 |
+
tokens = enc.encode(message)
|
12 |
+
tokens = torch.tensor(tokens, dtype= torch.long) # (8,) #check tiktoken app
|
13 |
+
tokens = tokens.unsqueeze(0).repeat(num_return_sequences, 1) # (5, 8)
|
14 |
+
x = tokens.to('cpu')
|
15 |
+
torch.manual_seed(42)
|
16 |
+
torch.cuda.manual_seed(42)
|
17 |
+
while x.size(1) < max_length:
|
18 |
+
# forward the model to get the logits
|
19 |
+
with torch.no_grad():
|
20 |
+
logits = model(x)[0] # (B, T, vocab_size)
|
21 |
+
# take the logits at the last position
|
22 |
+
logits = logits[:, -1, :] # (B, vocab_size)
|
23 |
+
# get the probabilities
|
24 |
+
probs = F.softmax(logits, dim=-1)
|
25 |
+
# do top-k sampling of 50 (huggingface pipeline default)
|
26 |
+
# topk_probs here becomes (5, 50), topk_indices is (5, 50)
|
27 |
+
topk_probs, topk_indices = torch.topk(probs, top_k, dim=-1)
|
28 |
+
# select a token from the top-k probabilities
|
29 |
+
# note: multinomial does not demand the input to sum to 1
|
30 |
+
ix = torch.multinomial(topk_probs, 1) # (B, 1)
|
31 |
+
# gather the corresponding indices
|
32 |
+
xcol = torch.gather(topk_indices, -1, ix) # (B, 1)
|
33 |
+
# append to the sequence
|
34 |
+
x = torch.cat((x, xcol), dim=1)
|
35 |
+
|
36 |
+
# print the generated text
|
37 |
+
return_text = ""
|
38 |
+
for i in range(num_return_sequences):
|
39 |
+
tokens = x[i, :max_length].tolist()
|
40 |
+
decoded = enc.decode(tokens)
|
41 |
+
return_text = return_text + ">"+ decoded +"\n"
|
42 |
+
return return_text
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
# Function to generate text
|
47 |
+
def generate_text(prompt, top_k, max_return_sequences, max_tokens):
|
48 |
+
ans = response(message = prompt, num_return_sequences = max_return_sequences,max_length = max_tokens,top_k = top_k)
|
49 |
+
|
50 |
+
|
51 |
+
return ans
|
52 |
+
|
53 |
+
# Create Gradio interface
|
54 |
+
iface = gr.Interface(
|
55 |
+
fn=generate_text,
|
56 |
+
inputs=[
|
57 |
+
gr.Textbox(lines=5, label="Input Text"),
|
58 |
+
gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Top-k"),
|
59 |
+
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Return Sequences"),
|
60 |
+
gr.Slider(minimum=10, maximum=150, value=20, step=5, label="Max Tokens")
|
61 |
+
],
|
62 |
+
outputs=gr.Textbox(label="Generated Text"),
|
63 |
+
title="GPT-2 Text Generator",
|
64 |
+
description="Generate text using GPT-2 model with adjustable parameters.",
|
65 |
+
examples=[["Hello, I'm a language model"]],
|
66 |
+
)
|
67 |
+
|
68 |
+
# Launch the interface
|
69 |
+
iface.launch()
|