samwell commited on
Commit
af39780
·
verified ·
1 Parent(s): ff38c17

Create app.py

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