Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -1,26 +1,49 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
model = GPT2LMHeadModel.from_pretrained("samwell/SamGPT")
|
7 |
-
tokenizer = GPT2Tokenizer.from_pretrained("gpt2") # Assuming you used the GPT-2 tokenizer
|
8 |
|
9 |
-
def
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
iface = gr.Interface(
|
16 |
-
fn=
|
17 |
inputs=[
|
18 |
gr.Textbox(label="Prompt"),
|
19 |
-
gr.Slider(minimum=
|
20 |
],
|
21 |
outputs=gr.Textbox(label="Generated Text"),
|
22 |
title="SamGPT Text Generation",
|
23 |
-
description="Enter a prompt to generate text with
|
24 |
)
|
25 |
|
26 |
-
iface.launch()
|
|
|
1 |
+
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
+
import tiktoken
|
5 |
+
from supplementary import GPTModel, generate_text_simple
|
6 |
+
|
7 |
+
# Load model configuration
|
8 |
+
GPT_CONFIG_124M = {
|
9 |
+
"vocab_size": 50257,
|
10 |
+
"context_length": 1024,
|
11 |
+
"emb_dim": 768,
|
12 |
+
"n_heads": 12,
|
13 |
+
"n_layers": 12,
|
14 |
+
"drop_rate": 0.1,
|
15 |
+
"qkv_bias": False
|
16 |
+
}
|
17 |
+
|
18 |
+
# Initialize model
|
19 |
+
model = GPTModel(GPT_CONFIG_124M)
|
20 |
+
|
21 |
+
# Load the trained weights
|
22 |
+
model.load_state_dict(torch.load("my_gpt_model.pth", map_location=torch.device('cpu')))
|
23 |
+
model.eval()
|
24 |
|
25 |
+
tokenizer = tiktoken.get_encoding("gpt2")
|
|
|
|
|
26 |
|
27 |
+
def generate(prompt, max_new_tokens):
|
28 |
+
token_ids = tokenizer.encode(prompt)
|
29 |
+
input_ids = torch.tensor(token_ids).unsqueeze(0)
|
30 |
+
output_ids = generate_text_simple(
|
31 |
+
model=model,
|
32 |
+
idx=input_ids,
|
33 |
+
max_new_tokens=max_new_tokens,
|
34 |
+
context_size=GPT_CONFIG_124M["context_length"]
|
35 |
+
)
|
36 |
+
return tokenizer.decode(output_ids.squeeze(0).tolist())
|
37 |
|
38 |
iface = gr.Interface(
|
39 |
+
fn=generate,
|
40 |
inputs=[
|
41 |
gr.Textbox(label="Prompt"),
|
42 |
+
gr.Slider(minimum=1, maximum=100, value=20, step=1, label="Max New Tokens")
|
43 |
],
|
44 |
outputs=gr.Textbox(label="Generated Text"),
|
45 |
title="SamGPT Text Generation",
|
46 |
+
description="Enter a prompt to generate text with the custom language model."
|
47 |
)
|
48 |
|
49 |
+
iface.launch()
|