Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ from transformers import AutoTokenizer, T5ForConditionalGeneration
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
import re
|
| 5 |
-
import json
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained("cointegrated/rut5-base-multitask", legacy=False)
|
| 8 |
model = T5ForConditionalGeneration.from_pretrained("cointegrated/rut5-base-multitask")
|
|
@@ -13,16 +12,17 @@ def smart_truncate(text, max_len):
|
|
| 13 |
return text[:text[:max_len+1].rfind(' ')].strip()
|
| 14 |
|
| 15 |
def generate_meta(description):
|
| 16 |
-
#
|
| 17 |
prompt = f"""
|
| 18 |
Create a title and description for product page.
|
| 19 |
Product name: Fenix ARB-L18-4000U
|
| 20 |
-
Description:
|
| 21 |
Output format:
|
| 22 |
-
{
|
| 23 |
"""
|
|
|
|
| 24 |
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
| 25 |
-
|
| 26 |
with torch.no_grad():
|
| 27 |
outputs = model.generate(
|
| 28 |
**inputs,
|
|
@@ -31,36 +31,39 @@ Output format:
|
|
| 31 |
early_stopping=True,
|
| 32 |
no_repeat_ngram_size=2
|
| 33 |
)
|
| 34 |
-
|
| 35 |
try:
|
| 36 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
clean_text = re.sub(r'\s+', ' ', description)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
# Интерфейс
|
| 56 |
with gr.Blocks() as app:
|
| 57 |
gr.Markdown("## Генератор метатегов (контроль длины)")
|
|
|
|
| 58 |
inp = gr.Textbox(label="Описание товара", lines=7)
|
| 59 |
btn = gr.Button("Сгенерировать")
|
| 60 |
-
with gr.Row():
|
| 61 |
-
out_title = gr.Textbox(label="Title (до 60 символов)", interactive=False)
|
| 62 |
-
out_desc = gr.Textbox(label="Description (до 160 символов)", lines=3, interactive=False)
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
app.launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
import re
|
|
|
|
| 5 |
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained("cointegrated/rut5-base-multitask", legacy=False)
|
| 7 |
model = T5ForConditionalGeneration.from_pretrained("cointegrated/rut5-base-multitask")
|
|
|
|
| 12 |
return text[:text[:max_len+1].rfind(' ')].strip()
|
| 13 |
|
| 14 |
def generate_meta(description):
|
| 15 |
+
# Пример промта для модели
|
| 16 |
prompt = f"""
|
| 17 |
Create a title and description for product page.
|
| 18 |
Product name: Fenix ARB-L18-4000U
|
| 19 |
+
Description: {description.strip()}
|
| 20 |
Output format:
|
| 21 |
+
{"title": "SEO заголовок до 60 символов", "description": "SEO описание до 160 символов"}
|
| 22 |
"""
|
| 23 |
+
|
| 24 |
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
| 25 |
+
|
| 26 |
with torch.no_grad():
|
| 27 |
outputs = model.generate(
|
| 28 |
**inputs,
|
|
|
|
| 31 |
early_stopping=True,
|
| 32 |
no_repeat_ngram_size=2
|
| 33 |
)
|
| 34 |
+
|
| 35 |
try:
|
| 36 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 37 |
+
json_match = re.search(r'\{.*\}', result, re.DOTALL)
|
| 38 |
+
|
| 39 |
+
if json_match:
|
| 40 |
+
json_data = json.loads(json_match.group())
|
| 41 |
+
title = smart_truncate(json_data.get("title", ""), 60)
|
| 42 |
+
desc = smart_truncate(json_data.get("description", ""), 160)
|
| 43 |
+
else:
|
| 44 |
+
clean_text = re.sub(r'\s+', ' ', description)
|
| 45 |
+
title = smart_truncate(f"Аккумулятор Fenix {clean_text}", 60)
|
| 46 |
+
desc = smart_truncate(clean_text, 160)
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
clean_text = re.sub(r'\s+', ' ', description)
|
| 50 |
+
title = smart_truncate(f"Аккумулятор Fenix {clean_text}", 60)
|
| 51 |
+
desc = smart_truncate(clean_text, 160)
|
| 52 |
+
|
| 53 |
+
return title, desc # ✅ Здесь важно — возвращать 2 отдельные строки
|
| 54 |
+
|
| 55 |
|
| 56 |
# Интерфейс
|
| 57 |
with gr.Blocks() as app:
|
| 58 |
gr.Markdown("## Генератор метатегов (контроль длины)")
|
| 59 |
+
|
| 60 |
inp = gr.Textbox(label="Описание товара", lines=7)
|
| 61 |
btn = gr.Button("Сгенерировать")
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
with gr.Row():
|
| 64 |
+
out_title = gr.Textbox(label="Title (до 60)", interactive=False)
|
| 65 |
+
out_desc = gr.Textbox(label="Description (до 160)", lines=3, interactive=False)
|
| 66 |
+
|
| 67 |
+
btn.click(fn=generate_meta, inputs=inp, outputs=[out_title, out_desc])
|
| 68 |
|
| 69 |
app.launch()
|