Ilvir commited on
Commit
70ce59b
1 Parent(s): a0ba832

Update pages/gpt.py

Browse files
Files changed (1) hide show
  1. pages/gpt.py +23 -12
pages/gpt.py CHANGED
@@ -5,7 +5,7 @@ import textwrap
5
  import plotly.express as px
6
 
7
 
8
-
9
 
10
  tokenizer = GPT2Tokenizer.from_pretrained('sberbank-ai/rugpt3small_based_on_gpt2')
11
  model = GPT2LMHeadModel.from_pretrained(
@@ -13,20 +13,27 @@ model = GPT2LMHeadModel.from_pretrained(
13
  output_attentions = False,
14
  output_hidden_states = False,
15
  )
16
- # Вешаем сохраненные веса на нашу модель
17
  model.load_state_dict(torch.load('models/modelgpt.pt', map_location=torch.device('cpu')))
18
 
19
 
20
- length = st.sidebar.slider('**Длина генерируемой последовательности:**', 8, 256, 15)
21
- num_samples = st.sidebar.slider('**Число генераций:**', 1, 10, 1)
22
- temperature = st.sidebar.slider('**Температура:**', 1.0, 10.0, 2.0)
23
- top_k = st.sidebar.slider('**Количество наиболее вероятных слов генерации:**', 10, 200, 50)
24
- top_p = st.sidebar.slider('**Минимальная суммарная вероятность топовых слов:**', 0.4, 1.0, 0.9)
 
 
 
 
 
 
25
 
26
 
27
- prompt = st.text_input('**Введите текст 👇:**')
28
- if st.button('**Сгенерировать текст**'):
29
-
 
30
  with torch.inference_mode():
31
  prompt = tokenizer.encode(prompt, return_tensors='pt')
32
  out = model.generate(
@@ -40,11 +47,15 @@ if st.button('**Сгенерировать текст**'):
40
  no_repeat_ngram_size=3,
41
  num_return_sequences=num_samples,
42
  ).cpu().numpy()
 
43
  st.write('**_Результат_** 👇')
44
  for i, out_ in enumerate(out):
45
-
 
 
 
46
  with st.expander(f'Текст {i+1}:'):
47
  st.write(textwrap.fill(tokenizer.decode(out_), 100))
48
  st.image("pict/wow.png")
49
 
50
-
 
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/modelgpt.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")
60
 
61
+