Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
from model import Transformer
|
5 |
+
from transformers import AutoTokenizer # pip install transformers
|
6 |
+
from utils import (
|
7 |
+
BLOCK_SIZE,
|
8 |
+
DEVICE,
|
9 |
+
DROPOUT,
|
10 |
+
NUM_EMBED,
|
11 |
+
NUM_HEAD,
|
12 |
+
NUM_LAYER,
|
13 |
+
encode,
|
14 |
+
decode
|
15 |
+
)
|
16 |
+
|
17 |
+
#tokenizer = AutoTokenizer.from_pretrained("DeepPavlov/rubert-base-cased")
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
19 |
+
vocab_size = tokenizer.vocab_size
|
20 |
+
|
21 |
+
# train a new model
|
22 |
+
model = Transformer(
|
23 |
+
vocab_size=vocab_size,
|
24 |
+
num_embed=NUM_EMBED,
|
25 |
+
block_size=BLOCK_SIZE,
|
26 |
+
num_heads=NUM_HEAD,
|
27 |
+
num_layers=NUM_LAYER,
|
28 |
+
dropout=DROPOUT
|
29 |
+
)
|
30 |
+
# load model to GPU if available
|
31 |
+
m = model.to(DEVICE)
|
32 |
+
# print the number of parameters in the model
|
33 |
+
|
34 |
+
m = torch.load("base_model.pth", map_location=torch.device(DEVICE))
|
35 |
+
m.eval()
|
36 |
+
|
37 |
+
#print(
|
38 |
+
# "Model with {:.2f}M parameters".format(sum(p.numel() for p in m.parameters()) / 1e6)
|
39 |
+
#)
|
40 |
+
def model_generate(text):
|
41 |
+
# generate some output based on the context
|
42 |
+
#context = torch.tensor(np.array(encode("Hello! My name is ", tokenizer)))
|
43 |
+
#context = torch.zeros((1, 1), dtype=torch.long, device=DEVICE)
|
44 |
+
text_input = str(input())
|
45 |
+
context_np = np.array(encode(text_input, tokenizer))
|
46 |
+
context_np = np.array([context_np])
|
47 |
+
context = torch.from_numpy(context_np)
|
48 |
+
#print(context)
|
49 |
+
|
50 |
+
return decode(enc_sec=m.generate(idx=context, max_new_tokens=100, block_size=BLOCK_SIZE)[0], tokenizer=tokenizer)
|
51 |
+
|
52 |
+
iface = gr.Interface(fn=model_generate, inputs="text", outputs="text")
|
53 |
+
iface.launch()
|