Spaces:
Sleeping
Sleeping
Update pages/gpt.py
Browse files- pages/gpt.py +24 -10
pages/gpt.py
CHANGED
|
@@ -4,26 +4,36 @@ import torch
|
|
| 4 |
import textwrap
|
| 5 |
import plotly.express as px
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
tokenizer = GPT2Tokenizer.from_pretrained('sberbank-ai/rugpt3small_based_on_gpt2')
|
| 8 |
model = GPT2LMHeadModel.from_pretrained(
|
| 9 |
'sberbank-ai/rugpt3small_based_on_gpt2',
|
| 10 |
output_attentions = False,
|
| 11 |
output_hidden_states = False,
|
| 12 |
)
|
| 13 |
-
|
| 14 |
model.load_state_dict(torch.load('models/model.pt', map_location=torch.device('cpu')))
|
| 15 |
|
| 16 |
|
| 17 |
-
length = st.sidebar.slider('
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
-
prompt = st.text_input('
|
| 25 |
-
if st.button('
|
| 26 |
-
|
|
|
|
| 27 |
with torch.inference_mode():
|
| 28 |
prompt = tokenizer.encode(prompt, return_tensors='pt')
|
| 29 |
out = model.generate(
|
|
@@ -37,9 +47,13 @@ if st.button('**Сгенерировать текст**'):
|
|
| 37 |
no_repeat_ngram_size=3,
|
| 38 |
num_return_sequences=num_samples,
|
| 39 |
).cpu().numpy()
|
|
|
|
| 40 |
st.write('**_Результат_** 👇')
|
| 41 |
for i, out_ in enumerate(out):
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
with st.expander(f'Текст {i+1}:'):
|
| 44 |
st.write(textwrap.fill(tokenizer.decode(out_), 100))
|
| 45 |
st.image("pict/wow.png")
|
|
|
|
| 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(
|
| 12 |
'sberbank-ai/rugpt3small_based_on_gpt2',
|
| 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(
|
|
|
|
| 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")
|