Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
model_name = "
|
| 6 |
-
tokenizer =
|
| 7 |
-
model =
|
| 8 |
|
| 9 |
def generate_meta_description(product_description):
|
| 10 |
-
prompt = f"Сгенерируй meta description (до 160 символов) по следующему описанию товара: {product_description}"
|
| 11 |
-
|
| 12 |
-
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
| 13 |
summary_ids = model.generate(
|
| 14 |
-
|
| 15 |
-
max_length=60,
|
| 16 |
-
num_beams=
|
| 17 |
-
no_repeat_ngram_size=
|
|
|
|
| 18 |
early_stopping=True
|
| 19 |
)
|
| 20 |
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 21 |
-
|
| 22 |
-
# обрезаем аккуратно, чтобы не обрывать слова
|
| 23 |
if len(summary) > 160:
|
| 24 |
truncated = summary[:160]
|
| 25 |
last_space = truncated.rfind(' ')
|
| 26 |
summary = truncated[:last_space]
|
| 27 |
-
|
| 28 |
return summary.strip()
|
| 29 |
|
| 30 |
iface = gr.Interface(
|
| 31 |
fn=generate_meta_description,
|
| 32 |
inputs=gr.Textbox(label="Описание товара", lines=5, placeholder="Например: Красивое мужское пальто из шерсти..."),
|
| 33 |
outputs=gr.Textbox(label="Meta Description (до 160 символов)"),
|
| 34 |
-
title="Meta Description
|
| 35 |
-
description="
|
| 36 |
)
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import GPT2Tokenizer, T5ForConditionalGeneration
|
| 3 |
|
| 4 |
+
# Загрузка модели и токенизатора
|
| 5 |
+
model_name = "RussianNLP/FRED-T5-Summarizer"
|
| 6 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name, eos_token='</s>')
|
| 7 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
|
| 9 |
def generate_meta_description(product_description):
|
| 10 |
+
prompt = f"<LM> Сгенерируй meta description (до 160 символов) по следующему описанию товара: {product_description}"
|
| 11 |
+
input_ids = tokenizer.encode(prompt, return_tensors='pt', max_length=512, truncation=True)
|
|
|
|
| 12 |
summary_ids = model.generate(
|
| 13 |
+
input_ids,
|
| 14 |
+
max_length=60,
|
| 15 |
+
num_beams=5,
|
| 16 |
+
no_repeat_ngram_size=4,
|
| 17 |
+
top_p=0.9,
|
| 18 |
early_stopping=True
|
| 19 |
)
|
| 20 |
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 21 |
+
# Обрезаем до 160 символов, не обрывая слова
|
|
|
|
| 22 |
if len(summary) > 160:
|
| 23 |
truncated = summary[:160]
|
| 24 |
last_space = truncated.rfind(' ')
|
| 25 |
summary = truncated[:last_space]
|
|
|
|
| 26 |
return summary.strip()
|
| 27 |
|
| 28 |
iface = gr.Interface(
|
| 29 |
fn=generate_meta_description,
|
| 30 |
inputs=gr.Textbox(label="Описание товара", lines=5, placeholder="Например: Красивое мужское пальто из шерсти..."),
|
| 31 |
outputs=gr.Textbox(label="Meta Description (до 160 символов)"),
|
| 32 |
+
title="Генератор Meta Description",
|
| 33 |
+
description="Вставьте описание товара, и получите краткое и логичное описание для тега <meta name=\"description\"> (до 160 символов)."
|
| 34 |
)
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|