cbspace commited on
Commit ·
a60243d
1
Parent(s): 7f908de
Rearrange code
Browse files
app.py
CHANGED
|
@@ -8,11 +8,6 @@ import spaces
|
|
| 8 |
|
| 9 |
@spaces.GPU
|
| 10 |
def main():
|
| 11 |
-
if torch.cuda.is_available():
|
| 12 |
-
device = torch.device('cuda')
|
| 13 |
-
else:
|
| 14 |
-
device = torch.device('cpu')
|
| 15 |
-
|
| 16 |
n_layers = 12
|
| 17 |
n_heads = 12
|
| 18 |
embed_dim = 768
|
|
@@ -23,12 +18,15 @@ def main():
|
|
| 23 |
|
| 24 |
tokenizer = tiktoken.encoding_for_model('gpt2')
|
| 25 |
model_path = hf_hub_download('cbspace/gpt', 'model.safetensors')
|
| 26 |
-
state_dict = load_file(model_path)
|
| 27 |
-
model = GPTModel(device, n_layers, n_heads, embed_dim, ffn_dim, n_vocab, max_seq_len, dropout)
|
| 28 |
-
model.load_state_dict(state_dict, strict=False)
|
| 29 |
-
model.eval()
|
| 30 |
|
| 31 |
def generate(prompt,out_tokens):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
inputs = tokenizer.encode(prompt)
|
| 33 |
outputs = model.generate(inputs, int(out_tokens), greedy=False)
|
| 34 |
return tokenizer.decode(outputs)
|
|
@@ -37,9 +35,9 @@ def main():
|
|
| 37 |
generate,
|
| 38 |
[
|
| 39 |
gr.Textbox(label='Prompt', lines=3),
|
| 40 |
-
gr.Number(label='Output Tokens', value=
|
| 41 |
],
|
| 42 |
-
gr.Textbox(label='Output', lines=
|
| 43 |
)
|
| 44 |
app.launch()
|
| 45 |
|
|
|
|
| 8 |
|
| 9 |
@spaces.GPU
|
| 10 |
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
n_layers = 12
|
| 12 |
n_heads = 12
|
| 13 |
embed_dim = 768
|
|
|
|
| 18 |
|
| 19 |
tokenizer = tiktoken.encoding_for_model('gpt2')
|
| 20 |
model_path = hf_hub_download('cbspace/gpt', 'model.safetensors')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def generate(prompt,out_tokens):
|
| 23 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 24 |
+
state_dict = load_file(model_path)
|
| 25 |
+
|
| 26 |
+
model = GPTModel(device, n_layers, n_heads, embed_dim, ffn_dim, n_vocab, max_seq_len, dropout)
|
| 27 |
+
model.load_state_dict(state_dict, strict=False)
|
| 28 |
+
model.eval()
|
| 29 |
+
|
| 30 |
inputs = tokenizer.encode(prompt)
|
| 31 |
outputs = model.generate(inputs, int(out_tokens), greedy=False)
|
| 32 |
return tokenizer.decode(outputs)
|
|
|
|
| 35 |
generate,
|
| 36 |
[
|
| 37 |
gr.Textbox(label='Prompt', lines=3),
|
| 38 |
+
gr.Number(label='Output Tokens', value=100)
|
| 39 |
],
|
| 40 |
+
gr.Textbox(label='Output', lines=6)
|
| 41 |
)
|
| 42 |
app.launch()
|
| 43 |
|