Spaces:
Runtime error
Runtime error
Commit
·
fa6bd11
1
Parent(s):
0e09d65
Update pages/gpt_v1.py
Browse files- pages/gpt_v1.py +30 -14
pages/gpt_v1.py
CHANGED
@@ -4,7 +4,8 @@ import torch
|
|
4 |
import textwrap
|
5 |
import plotly.express as px
|
6 |
|
7 |
-
|
|
|
8 |
|
9 |
tokenizer = GPT2Tokenizer.from_pretrained('sberbank-ai/rugpt3small_based_on_gpt2')
|
10 |
model = GPT2LMHeadModel.from_pretrained(
|
@@ -12,26 +13,33 @@ model = GPT2LMHeadModel.from_pretrained(
|
|
12 |
output_attentions = False,
|
13 |
output_hidden_states = False,
|
14 |
)
|
15 |
-
|
16 |
-
model.load_state_dict(torch.load('model.pt', map_location=torch.device('cpu')))
|
17 |
|
18 |
|
19 |
-
length = st.sidebar.slider('
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
|
26 |
-
prompt = st.text_input('
|
27 |
-
if st.button('
|
28 |
-
|
|
|
29 |
with torch.inference_mode():
|
30 |
prompt = tokenizer.encode(prompt, return_tensors='pt')
|
31 |
out = model.generate(
|
32 |
input_ids=prompt,
|
33 |
max_length=length,
|
34 |
-
num_beams=
|
35 |
do_sample=True,
|
36 |
temperature=temperature,
|
37 |
top_k=top_k,
|
@@ -39,9 +47,17 @@ if st.button('**Сгенерировать текст**'):
|
|
39 |
no_repeat_ngram_size=3,
|
40 |
num_return_sequences=num_samples,
|
41 |
).cpu().numpy()
|
|
|
|
|
42 |
for i, out_ in enumerate(out):
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
|
47 |
|
|
|
4 |
import textwrap
|
5 |
import plotly.express as px
|
6 |
|
7 |
+
|
8 |
+
st.header(':green[Text generation by GPT2 model]')
|
9 |
|
10 |
tokenizer = GPT2Tokenizer.from_pretrained('sberbank-ai/rugpt3small_based_on_gpt2')
|
11 |
model = GPT2LMHeadModel.from_pretrained(
|
|
|
13 |
output_attentions = False,
|
14 |
output_hidden_states = False,
|
15 |
)
|
16 |
+
|
17 |
+
model.load_state_dict(torch.load('models/model.pt', map_location=torch.device('cpu')))
|
18 |
|
19 |
|
20 |
+
length = st.sidebar.slider('**Generated sequence length:**', 8, 256, 15)
|
21 |
+
if length > 100:
|
22 |
+
st.warning("This is very hard for me, please have pity on me. Could you lower the value?", icon="🤖")
|
23 |
+
num_samples = st.sidebar.slider('**Number of generations:**', 1, 10, 1)
|
24 |
+
if num_samples > 4:
|
25 |
+
st.warning("OH MY ..., I have to work late again!!! Could you lower the value?", icon="🤖")
|
26 |
+
temperature = st.sidebar.slider('**Temperature:**', 0.1, 10.0, 3.0)
|
27 |
+
if temperature > 6.0:
|
28 |
+
st.info('What? You want to get some kind of bullshit as a result? Turn down the temperature', icon="🤖")
|
29 |
+
top_k = st.sidebar.slider('**Number of most likely generation words:**', 10, 200, 50)
|
30 |
+
top_p = st.sidebar.slider('**Minimum total probability of top words:**', 0.4, 1.0, 0.9)
|
31 |
|
32 |
|
33 |
+
prompt = st.text_input('**Enter text 👇:**')
|
34 |
+
if st.button('**Generate text**'):
|
35 |
+
image_container = st.empty()
|
36 |
+
image_container.image("pict/wait.jpeg", caption="that's so long!!!", use_column_width=True)
|
37 |
with torch.inference_mode():
|
38 |
prompt = tokenizer.encode(prompt, return_tensors='pt')
|
39 |
out = model.generate(
|
40 |
input_ids=prompt,
|
41 |
max_length=length,
|
42 |
+
num_beams=8,
|
43 |
do_sample=True,
|
44 |
temperature=temperature,
|
45 |
top_k=top_k,
|
|
|
47 |
no_repeat_ngram_size=3,
|
48 |
num_return_sequences=num_samples,
|
49 |
).cpu().numpy()
|
50 |
+
image_container.empty()
|
51 |
+
st.write('**_Результат_** 👇')
|
52 |
for i, out_ in enumerate(out):
|
53 |
+
# audio_file = open('pict/pole-chudes-priz.mp3', 'rb')
|
54 |
+
# audio_bytes = audio_file.read()
|
55 |
+
# st.audio(audio_bytes, format='audio/mp3')
|
56 |
+
|
57 |
+
with st.expander(f'Текст {i+1}:'):
|
58 |
+
st.write(textwrap.fill(tokenizer.decode(out_), 100))
|
59 |
+
st.image("pict/wow.png")
|
60 |
+
|
61 |
|
62 |
|
63 |
|