Spaces:
Sleeping
Sleeping
File size: 2,395 Bytes
a90ada2 259ce62 3a75e4a a90ada2 3a75e4a 259ce62 3a75e4a 2be3559 259ce62 a90ada2 ebc1811 a90ada2 259ce62 8bdbf2c 259ce62 a90ada2 259ce62 a90ada2 259ce62 |
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
from transformers import AutoTokenizer, T5ForConditionalGeneration
import torch
import gradio as gr
import json
import re
tokenizer = AutoTokenizer.from_pretrained("cointegrated/rut5-base-multitask")
model = T5ForConditionalGeneration.from_pretrained("cointegrated/rut5-base-multitask")
def generate_meta_tags(description):
prompt = f"""
Сгенерируй SEO-метатеги для карточки товара на основе описания ниже.
Описание товара:
{description.strip()}
Требования:
- title: до 60 символов, цепляющий заголовок с ключевыми словами
- description: до 160 символов, краткое описание преимуществ
Формат вывода (строго JSON):
{{"title": "...", "description": "..."}}
"""
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=200,
do_sample=True,
temperature=0.5,
top_k=50,
num_beams=3,
eos_token_id=tokenizer.eos_token_id
)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Чистка и парсинг результата
try:
json_str = re.search(r'\{.*\}', generated_text, re.DOTALL).group()
result = json.loads(json_str)
return json.dumps(result, ensure_ascii=False, indent=2)
except:
# Фоллбэк, если модель ошибается
title = re.sub(r'[^\w\s]', '', description)[:60]
desc = re.sub(r'[^\w\s]', '', description)[:160]
return json.dumps({"title": title, "description": desc}, ensure_ascii=False)
demo = gr.Interface(
fn=generate_meta_tags,
inputs=gr.Textbox(label="Описание товара", lines=5),
outputs=gr.JSON(label="Мета-теги"),
examples=[
["Смартфон Samsung Galaxy S23 с экраном 6.1\", 8 ГБ ОЗУ, 128 ГБ памяти, камерой 50 МП"],
["Электрическая зубная щётка Oral-B Pro 3 3000 с датчиком давления и 3 насадками"]
],
title="Генератор SEO-метатегов для маркетплейсов"
)
demo.launch() |