metaGenerator / app.py
Triok1's picture
Update app.py
4058b9f verified
raw
history blame
2.53 kB
from transformers import AutoTokenizer, T5ForConditionalGeneration
import torch
import gradio as gr
import re
tokenizer = AutoTokenizer.from_pretrained("cointegrated/rut5-base-multitask", legacy=False)
model = T5ForConditionalGeneration.from_pretrained("cointegrated/rut5-base-multitask")
def smart_truncate(text, max_len):
if len(text) <= max_len:
return text
return text[:text[:max_len+1].rfind(' ')].strip()
def generate_meta(description):
# Пример промта для модели
prompt = f"""
Create a title and description for product page.
Product name: Fenix ARB-L18-4000U
Description: {description.strip()}
Output format:
{"title": "SEO заголовок до 60 символов", "description": "SEO описание до 160 символов"}
"""
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=200,
num_beams=5,
early_stopping=True,
no_repeat_ngram_size=2
)
try:
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
json_match = re.search(r'\{.*\}', result, re.DOTALL)
if json_match:
json_data = json.loads(json_match.group())
title = smart_truncate(json_data.get("title", ""), 60)
desc = smart_truncate(json_data.get("description", ""), 160)
else:
clean_text = re.sub(r'\s+', ' ', description)
title = smart_truncate(f"Аккумулятор Fenix {clean_text}", 60)
desc = smart_truncate(clean_text, 160)
except Exception as e:
clean_text = re.sub(r'\s+', ' ', description)
title = smart_truncate(f"Аккумулятор Fenix {clean_text}", 60)
desc = smart_truncate(clean_text, 160)
return title, desc # ✅ Здесь важно — возвращать 2 отдельные строки
# Интерфейс
with gr.Blocks() as app:
gr.Markdown("## Генератор метатегов (контроль длины)")
inp = gr.Textbox(label="Описание товара", lines=7)
btn = gr.Button("Сгенерировать")
with gr.Row():
out_title = gr.Textbox(label="Title (до 60)", interactive=False)
out_desc = gr.Textbox(label="Description (до 160)", lines=3, interactive=False)
btn.click(fn=generate_meta, inputs=inp, outputs=[out_title, out_desc])
app.launch()