praty7717 commited on
Commit
ccbb326
·
verified ·
1 Parent(s): dc6bd3e

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +24 -20
  2. config.json +9 -0
app.py CHANGED
@@ -1,28 +1,32 @@
1
  import gradio as gr
2
  import torch
3
- from transformers import GPT2LMHeadModel, GPT2Tokenizer
4
 
5
- # Define the model class
6
- class GPTLanguageModel(GPT2LMHeadModel):
7
- def __init__(self, config):
8
- super().__init__(config)
 
 
 
 
 
 
 
 
9
 
10
  # Load tokenizer and model
11
- tokenizer = GPT2Tokenizer.from_pretrained("gpt2") # Use your tokenizer path
12
- model = GPTLanguageModel.from_pretrained("gpt2") # Load the architecture
13
- model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu'))) # Load the weights
14
- model.eval() # Set to evaluation mode
15
-
16
- # Define a custom text generation pipeline
17
- class CustomTextGenerationPipeline:
18
- def __init__(self, model, tokenizer):
19
- self.model = model
20
- self.tokenizer = tokenizer
21
-
22
- def __call__(self, prompt, max_length=100):
23
- input_ids = self.tokenizer.encode(prompt, return_tensors='pt')
24
- generated_ids = self.model.generate(input_ids, max_length=max_length)
25
- return self.tokenizer.decode(generated_ids[0], skip_special_tokens=True)
26
 
27
  # Create the pipeline
28
  pipeline = CustomTextGenerationPipeline(model, tokenizer)
 
1
  import gradio as gr
2
  import torch
3
+ from transformers import GPT2Tokenizer
4
 
5
+ class GPTLanguageModel(torch.nn.Module):
6
+ def __init__(self, vocab_size, hidden_size):
7
+ super(GPTLanguageModel, self).__init__()
8
+ self.embedding = torch.nn.Embedding(vocab_size, hidden_size)
9
+ # Add other layers as needed
10
+
11
+ def forward(self, input_ids):
12
+ return self.embedding(input_ids) # Placeholder for the forward pass
13
+
14
+ def generate(self, input_ids, max_length=100):
15
+ # Custom generation logic here
16
+ return input_ids # Placeholder
17
 
18
  # Load tokenizer and model
19
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2") # Or your custom tokenizer
20
+ vocab_size = tokenizer.vocab_size
21
+ model = GPTLanguageModel(vocab_size=vocab_size, hidden_size=768) # Set the sizes appropriately
22
+
23
+ # Load model weights
24
+ try:
25
+ model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu')), strict=False)
26
+ except RuntimeError as e:
27
+ print(f"Error loading model weights: {e}")
28
+
29
+ model.eval()
 
 
 
 
30
 
31
  # Create the pipeline
32
  pipeline = CustomTextGenerationPipeline(model, tokenizer)
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "block_size": 256,
3
+ "model_type": "gpt",
4
+ "n_embd": 384,
5
+ "n_head": 6,
6
+ "n_layer": 6,
7
+ "transformers_version": "4.44.2",
8
+ "vocab_size": 95
9
+ }