Spaces:
Sleeping
Sleeping
| 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() | |