samwell commited on
Commit
50859d6
1 Parent(s): 71fd06e

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +36 -13
app.py CHANGED
@@ -1,26 +1,49 @@
 
1
  import gradio as gr
2
  import torch
3
- from transformers import GPT2LMHeadModel, GPT2Tokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Load the model and tokenizer
6
- model = GPT2LMHeadModel.from_pretrained("samwell/SamGPT")
7
- tokenizer = GPT2Tokenizer.from_pretrained("gpt2") # Assuming you used the GPT-2 tokenizer
8
 
9
- def generate_text(prompt, max_length):
10
- input_ids = tokenizer.encode(prompt, return_tensors="pt")
11
- output = model.generate(input_ids, max_length=max_length, num_return_sequences=1, no_repeat_ngram_size=2)
12
- generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
13
- return generated_text
 
 
 
 
 
14
 
15
  iface = gr.Interface(
16
- fn=generate_text,
17
  inputs=[
18
  gr.Textbox(label="Prompt"),
19
- gr.Slider(minimum=10, maximum=200, value=50, step=1, label="Max Length")
20
  ],
21
  outputs=gr.Textbox(label="Generated Text"),
22
  title="SamGPT Text Generation",
23
- description="Enter a prompt to generate text with SamGPT."
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()