File size: 1,744 Bytes
a90ada2
a968f2c
a90ada2
a968f2c
 
 
 
fff4dbb
05c8c2e
a968f2c
 
05c8c2e
a968f2c
 
 
 
05c8c2e
 
 
a968f2c
05c8c2e
 
 
 
 
 
 
 
 
 
a968f2c
 
05c8c2e
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import gradio as gr
from transformers import GPT2Tokenizer, T5ForConditionalGeneration

# Загрузка модели и токенизатора
model_name = "RussianNLP/FRED-T5-Summarizer"
tokenizer = GPT2Tokenizer.from_pretrained(model_name, eos_token='</s>')
model = T5ForConditionalGeneration.from_pretrained(model_name)

def generate_meta_description(product_description):
    prompt = f"<LM> Сгенерируй meta description (до 160 символов) по следующему описанию товара: {product_description}"
    input_ids = tokenizer.encode(prompt, return_tensors='pt', max_length=512, truncation=True)
    summary_ids = model.generate(
        input_ids,
        max_length=60,
        num_beams=5,
        no_repeat_ngram_size=4,
        early_stopping=True
    )
    summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
    # Обрезаем до 160 символов, не обрывая слова
    if len(summary) > 160:
        truncated = summary[:160]
        last_space = truncated.rfind(' ')
        summary = truncated[:last_space]
    return summary.strip()

iface = gr.Interface(
    fn=generate_meta_description,
    inputs=gr.Textbox(label="Описание товара", lines=5, placeholder="Например: Красивое мужское пальто из шерсти..."),
    outputs=gr.Textbox(label="Meta Description (до 160 символов)"),
    title="Генератор Meta Description",
    description="Вставьте описание товара, и получите краткое и логичное описание для тега <meta name=\"description\"> (до 160 символов)."
)

if __name__ == "__main__":
    iface.launch()