Spaces:
Sleeping
Sleeping
nb app
Browse files
app.py
CHANGED
|
@@ -1,7 +1,102 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import transformers
|
| 2 |
+
import torch
|
| 3 |
+
import tokenizers
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
+
from PIL import Image
|
|
|
|
| 8 |
|
| 9 |
+
|
| 10 |
+
@st.cache(hash_funcs={tokenizers.Tokenizer: lambda _: None, tokenizers.AddedToken: lambda _: None, re.Pattern: lambda _: None}, allow_output_mutation=True, suppress_st_warning=True)
|
| 11 |
+
def get_model(model_name, model_path):
|
| 12 |
+
tokenizer = transformers.GPT2Tokenizer.from_pretrained(model_name)
|
| 13 |
+
model = transformers.GPT2LMHeadModel.from_pretrained(model_name)
|
| 14 |
+
model.eval()
|
| 15 |
+
return model, tokenizer
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def predict(text, model, tokenizer, n_beams=5, temperature=2.5, top_p=0.8, length_of_generated=300):
|
| 19 |
+
# text += '\n'
|
| 20 |
+
input_ids = tokenizer.encode(text, return_tensors="pt")
|
| 21 |
+
length_of_prompt = len(input_ids[0])
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
out = model.generate(input_ids,
|
| 24 |
+
do_sample=True,
|
| 25 |
+
num_beams=n_beams,
|
| 26 |
+
temperature=temperature,
|
| 27 |
+
top_p=top_p,
|
| 28 |
+
max_length=length_of_prompt + length_of_generated,
|
| 29 |
+
eos_token_id=tokenizer.eos_token_id
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
generated = list(map(tokenizer.decode, out))[0]
|
| 33 |
+
return generated.replace('\n[EOS]\n', '')
|
| 34 |
+
|
| 35 |
+
def predict_gpt(text, model, tokenizer,):
|
| 36 |
+
input_ids = tokenizer.encode(text, return_tensors="pt").to(device)
|
| 37 |
+
with torch.no_grad():
|
| 38 |
+
out = model.generate(input_ids,
|
| 39 |
+
do_sample=True,
|
| 40 |
+
num_beams=3,
|
| 41 |
+
temperature=1.0,
|
| 42 |
+
top_p=0.75,
|
| 43 |
+
max_length=1024,
|
| 44 |
+
eos_token_id = tokenizer.eos_token_id,
|
| 45 |
+
pad_token_id = tokenizer.pad_token_id,
|
| 46 |
+
repetition_penalty = 2.5,
|
| 47 |
+
num_return_sequences = 1,
|
| 48 |
+
output_attentions = True,
|
| 49 |
+
return_dict_in_generate=True,
|
| 50 |
+
)
|
| 51 |
+
decode = lambda x : tokenizer.decode(x, skip_special_tokens=True)
|
| 52 |
+
generated_text = list(map(decode, out['sequences'])).split('Описание :')[1]
|
| 53 |
+
return generated_text
|
| 54 |
+
|
| 55 |
+
def predict_t5(text, model, tokenizer,):
|
| 56 |
+
input_ids = tokenizer.encode(text, return_tensors="pt").to(device)
|
| 57 |
+
with torch.no_grad():
|
| 58 |
+
out = model.generate(input_ids,
|
| 59 |
+
do_sample=True,
|
| 60 |
+
num_beams=4,
|
| 61 |
+
temperature=1.2,
|
| 62 |
+
top_p=0.35,
|
| 63 |
+
max_length=1024,
|
| 64 |
+
length_penalty = 5.5,
|
| 65 |
+
output_attentions = True,
|
| 66 |
+
return_dict_in_generate=True,
|
| 67 |
+
repetition_penalty = 2.5,
|
| 68 |
+
num_return_sequences = 1
|
| 69 |
+
)
|
| 70 |
+
decode = lambda x : tokenizer.decode(x, skip_special_tokens=True)
|
| 71 |
+
generated_text = list(map(decode, out['sequences']))[0]
|
| 72 |
+
return generated_text
|
| 73 |
+
|
| 74 |
+
gpt_model, gpt_tokenizer = get_model('mipatov/rugpt3_nb_descr', 'mipatov/rugpt3_nb_descr')
|
| 75 |
+
t5_model, t5_tokenizer = get_model('mipatov/rugpt3_nb_descr', 'mipatov/rugpt3_nb_descr')
|
| 76 |
+
|
| 77 |
+
# st.title("NeuroKorzh")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
option = st.selectbox('Выберите модель', ('GPT', 'T5'))
|
| 81 |
+
temperature = st.slider(label='Температура', min_value=0.1, max_value=10, value=1)
|
| 82 |
+
|
| 83 |
+
# st.markdown("\n")
|
| 84 |
+
example = ' Название : Super_NB 2001 Gaming;\n Диагональ экрана : 21 " ;\n Разрешение экрана : 1337x228 ;\n Поверхность экрана : матовая ;\n Тип матрицы : nfc ;\n Процессор : CMD processor 7 ядер 16.16 ГГц (46.0 ГГц, в режиме Turbo) ;\n Оперативная память : 28 Гб DDR5 ;\n Тип видеокарты : интегрированный ;\n Видеокарта : qwerty-grafics ;\n SSD : 720 Гб ;\n Wi-Fi : ДА, 802.11 a/b/g/n/ac ;\n Bluetooth : ДА, v5.0 ;\n Кабельная сеть : 10/100/1000 (Gigabit Ethernet) Мбит/с ;\n USB 2.0 : 13 ;\n USB 3.0 : 22 ;\n HDMI : 11 ;\n Операционная система : CMD-shell ;\n Веб-камера : встроенная ;\n Микрофон : есть ;\n Разъем наушники/микрофон : комбинированный разъем ;\n Акустическая система : стереодинамики ;\n Цвет клавиатуры : черный ;\n Цифровой блок клавиатуры : есть ;\n Подсветка клавиш клавиатуры : есть ;\n Тип батареи : Al-Ion ;\n Количество ячеек батареи : 36 cell ;\n Энергоемкость батареи : 176 Wh ;\n Цвет : черный ;\n Размеры : 1.23 х 456 х 78.9 мм ;\n Вес : 19 кг ;\n Гарантия : 322 мес. ;\n Материал корпуса : пластик ;\n Время работы от батареи : 82ч ;\n Кард-ридер : есть WA SD ;'
|
| 85 |
+
|
| 86 |
+
text = st.text_area(label='Характеристики ноутбука', value=example, height=200).replace('\n','')
|
| 87 |
+
button = st.button('Старт')
|
| 88 |
+
|
| 89 |
+
if button:
|
| 90 |
+
try:
|
| 91 |
+
with st.spinner("Пишем описание..."):
|
| 92 |
+
if option == 'GPT':
|
| 93 |
+
result = predict_gpt(text, gpt_model, gpt_tokenizer, temperature=temperature)
|
| 94 |
+
elif option == 'T5':
|
| 95 |
+
result = predict_t5(text, t5_model, t5_tokenizer, temperature=temperature)
|
| 96 |
+
else:
|
| 97 |
+
st.error('Error in selectbox')
|
| 98 |
+
|
| 99 |
+
st.text_area(label='', value=result, height=1000)
|
| 100 |
+
|
| 101 |
+
except Exception:
|
| 102 |
+
st.error("Ooooops, something went wrong. Please try again and report to me, tg: @vladyur")
|