Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ 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__()
|
@@ -15,14 +16,25 @@ class GPTLanguageModel(torch.nn.Module):
|
|
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
|
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 |
|
|
|
2 |
import torch
|
3 |
from transformers import GPT2Tokenizer
|
4 |
|
5 |
+
# Define your model
|
6 |
class GPTLanguageModel(torch.nn.Module):
|
7 |
def __init__(self, vocab_size, hidden_size):
|
8 |
super(GPTLanguageModel, self).__init__()
|
|
|
16 |
# Custom generation logic here
|
17 |
return input_ids # Placeholder
|
18 |
|
19 |
+
# Define the Custom Text Generation Pipeline
|
20 |
+
class CustomTextGenerationPipeline:
|
21 |
+
def __init__(self, model, tokenizer):
|
22 |
+
self.model = model
|
23 |
+
self.tokenizer = tokenizer
|
24 |
+
|
25 |
+
def __call__(self, prompt, max_length=100):
|
26 |
+
input_ids = self.tokenizer.encode(prompt, return_tensors='pt')
|
27 |
+
generated_ids = self.model.generate(input_ids, max_length=max_length)
|
28 |
+
return self.tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
29 |
+
|
30 |
# Load tokenizer and model
|
31 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2") # Or your custom tokenizer
|
32 |
vocab_size = tokenizer.vocab_size
|
33 |
+
model = GPTLanguageModel(vocab_size=vocab_size, hidden_size=768) # Set sizes appropriately
|
34 |
|
35 |
# Load model weights
|
36 |
try:
|
37 |
+
model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu'), weights_only=True), strict=False)
|
38 |
except RuntimeError as e:
|
39 |
print(f"Error loading model weights: {e}")
|
40 |
|