Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +24 -20
- config.json +9 -0
app.py
CHANGED
@@ -1,28 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from transformers import
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Load tokenizer and model
|
11 |
-
tokenizer = GPT2Tokenizer.from_pretrained("gpt2") #
|
12 |
-
|
13 |
-
model
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
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 |
+
}
|