Dhairya-365 commited on
Commit
41fdbec
·
verified ·
1 Parent(s): aad3536

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -11
app.py CHANGED
@@ -1,32 +1,29 @@
1
  import gradio as gr
2
  import torch
3
- import accelerate
4
- # Added GPT2Config and GPT2LMHeadModel to the imports
5
  from transformers import AutoTokenizer, GenerationConfig, GPT2Config, GPT2LMHeadModel
6
 
7
  # 1. Load the standard GPT-2 tokenizer
8
  tokenizer = AutoTokenizer.from_pretrained("gpt2")
9
  tokenizer.pad_token = tokenizer.eos_token
10
 
11
- # 2. Define the configuration and load the model
12
- # We use GPT2Config to ensure the 'model_type' is explicitly handled
13
- config = GPT2Config.from_pretrained("gpt2")
14
 
15
- # Load your local .safetensors weights into the GPT2 structure
16
  model = GPT2LMHeadModel.from_pretrained(
17
  ".",
18
  config=config,
19
  local_files_only=True,
20
  torch_dtype=torch.float32,
21
- device_map="auto"
 
22
  )
23
 
24
- # 3. Load your specific generation settings from your local file
25
- # Ensure 'generation_config.json' is actually in your Space root folder
26
  gen_config = GenerationConfig.from_pretrained(".", "generation_config.json")
27
 
28
  def generate_code(prompt):
29
- # Move inputs to the same device as the model (CPU or GPU)
30
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
31
 
32
  with torch.no_grad():
@@ -38,7 +35,7 @@ def generate_code(prompt):
38
 
39
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
40
 
41
- # 4. Gradio Interface
42
  demo = gr.Interface(
43
  fn=generate_code,
44
  inputs=gr.Textbox(placeholder="Write a function to...", label="Input Prompt"),
 
1
  import gradio as gr
2
  import torch
 
 
3
  from transformers import AutoTokenizer, GenerationConfig, GPT2Config, GPT2LMHeadModel
4
 
5
  # 1. Load the standard GPT-2 tokenizer
6
  tokenizer = AutoTokenizer.from_pretrained("gpt2")
7
  tokenizer.pad_token = tokenizer.eos_token
8
 
9
+ # 2. Define the configuration with YOUR specific vocab size
10
+ # Changed vocab_size to 50000 based on your error report
11
+ config = GPT2Config.from_pretrained("gpt2", vocab_size=50000)
12
 
13
+ # 3. Load the model and ignore the size mismatch warning
14
  model = GPT2LMHeadModel.from_pretrained(
15
  ".",
16
  config=config,
17
  local_files_only=True,
18
  torch_dtype=torch.float32,
19
+ device_map="auto",
20
+ ignore_mismatched_sizes=True # This is the key fix!
21
  )
22
 
23
+ # 4. Load your generation settings
 
24
  gen_config = GenerationConfig.from_pretrained(".", "generation_config.json")
25
 
26
  def generate_code(prompt):
 
27
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
28
 
29
  with torch.no_grad():
 
35
 
36
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
37
 
38
+ # 5. Gradio Interface
39
  demo = gr.Interface(
40
  fn=generate_code,
41
  inputs=gr.Textbox(placeholder="Write a function to...", label="Input Prompt"),