Ilvir commited on
Commit
ed7e2ea
1 Parent(s): dd1464b

Upload 2 files

Browse files
Files changed (2) hide show
  1. gpt.py +47 -0
  2. modelgpt.pt +3 -0
gpt.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
2
+ import streamlit as st
3
+ import torch
4
+
5
+
6
+ tokenizer = GPT2Tokenizer.from_pretrained('sberbank-ai/rugpt3small_based_on_gpt2')
7
+ model = GPT2LMHeadModel.from_pretrained(
8
+ 'sberbank-ai/rugpt3small_based_on_gpt2',
9
+ output_attentions = False,
10
+ output_hidden_states = False,
11
+ )
12
+ # Вешаем сохраненные веса на нашу модель
13
+ model.load_state_dict(torch.load('modelgpt.pt', map_location=torch.device('cpu')))
14
+
15
+
16
+
17
+
18
+ prompt = st.text_input('Введите текст prompt:')
19
+ length = st.slider('Длина генерируемой последовательности:', 10, 1000, 50)
20
+ num_samples = st.slider('Число генераций:', 1, 10, 1)
21
+ temperature = st.slider('Температура:', 0.1, 1.0, 0.5)
22
+
23
+
24
+
25
+
26
+ def generate_text(model, tokenizer, prompt, length, num_samples, temperature):
27
+ input_ids = tokenizer.encode(prompt, return_tensors='pt')
28
+ output_sequences = model.generate(
29
+ input_ids=input_ids,
30
+ max_length=length,
31
+ num_return_sequences=num_samples,
32
+ temperature=temperature
33
+ )
34
+
35
+ generated_texts = []
36
+ for output_sequence in output_sequences:
37
+ generated_text = tokenizer.decode(output_sequence, clean_up_tokenization_spaces=True)
38
+ generated_texts.append(generated_text)
39
+
40
+ return generated_texts
41
+
42
+
43
+ if st.button('Сгенерировать текст'):
44
+ generated_texts = generate_text(model, tokenizer, prompt, length, num_samples, temperature)
45
+ for i, text in enumerate(generated_texts):
46
+ st.write(f'Текст {i+1}:')
47
+ st.write(text)
modelgpt.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2eaa05107b384cc31f73396db885069998dbeb1027e7934be657f9a1f7b88bb9
3
+ size 551320983