Spaces:
Sleeping
Sleeping
vvv-knyazeva
commited on
Commit
•
beaec44
1
Parent(s):
befad0a
Upload gpt.py
Browse files- pages/gpt.py +39 -0
pages/gpt.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
2 |
+
|
3 |
+
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
|
4 |
+
model = GPT2LMHeadModel.from_pretrained('model.ph')
|
5 |
+
|
6 |
+
|
7 |
+
import streamlit as st
|
8 |
+
|
9 |
+
prompt = st.text_input('Введите текст prompt:')
|
10 |
+
length = st.slider('Длина генерируемой последовательности:', 10, 1000, 50)
|
11 |
+
num_samples = st.slider('Число генераций:', 1, 10, 1)
|
12 |
+
temperature = st.slider('Температура:', 0.1, 1.0, 0.5)
|
13 |
+
|
14 |
+
|
15 |
+
import torch
|
16 |
+
|
17 |
+
def generate_text(model, tokenizer, prompt, length, num_samples, temperature):
|
18 |
+
input_ids = tokenizer.encode(prompt, return_tensors='pt')
|
19 |
+
output_sequences = model.generate(
|
20 |
+
input_ids=input_ids,
|
21 |
+
max_length=length,
|
22 |
+
num_return_sequences=num_samples,
|
23 |
+
temperature=temperature
|
24 |
+
)
|
25 |
+
|
26 |
+
generated_texts = []
|
27 |
+
for output_sequence in output_sequences:
|
28 |
+
generated_text = tokenizer.decode(output_sequence, clean_up_tokenization_spaces=True)
|
29 |
+
generated_texts.append(generated_text)
|
30 |
+
|
31 |
+
return generated_texts
|
32 |
+
|
33 |
+
|
34 |
+
if st.button('Сгенерировать текст'):
|
35 |
+
generated_texts = generate_text(model, tokenizer, prompt, length, num_samples, temperature)
|
36 |
+
for i, text in enumerate(generated_texts):
|
37 |
+
st.write(f'Текст {i+1}:')
|
38 |
+
st.write(text)
|
39 |
+
|