Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,39 @@
|
|
1 |
-
from transformers import AutoTokenizer, T5ForConditionalGeneration
|
2 |
-
import torch
|
3 |
import gradio as gr
|
4 |
-
import
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
def
|
10 |
-
|
11 |
-
return text
|
12 |
-
return text[:text[:max_len+1].rfind(' ')].strip()
|
13 |
-
|
14 |
-
def generate_meta(description):
|
15 |
-
# Очищаем входное описание
|
16 |
-
description = description.strip()
|
17 |
-
|
18 |
-
# Теперь безопасный f-string без .strip() внутри
|
19 |
-
prompt = """
|
20 |
-
Create a title and description for product page.
|
21 |
-
Product name: Fenix ARB-L18-4000U
|
22 |
-
Description: {description}
|
23 |
-
|
24 |
-
Output format:
|
25 |
-
{{"title": "SEO заголовок до 60 символов", "description": "SEO описание до 160 символов"}}
|
26 |
-
""".format(description=description)
|
27 |
|
28 |
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
if json_match:
|
44 |
-
json_data = json.loads(json_match.group())
|
45 |
-
title = smart_truncate(json_data.get("title", ""), 60)
|
46 |
-
desc = smart_truncate(json_data.get("description", ""), 160)
|
47 |
-
else:
|
48 |
-
clean_text = re.sub(r'\s+', ' ', description)
|
49 |
-
title = smart_truncate(f"Аккумулятор Fenix {clean_text}", 60)
|
50 |
-
desc = smart_truncate(clean_text, 160)
|
51 |
-
|
52 |
-
except Exception as e:
|
53 |
-
clean_text = re.sub(r'\s+', ' ', description)
|
54 |
-
title = smart_truncate(f"Аккумулятор Fenix {clean_text}", 60)
|
55 |
-
desc = smart_truncate(clean_text, 160)
|
56 |
-
|
57 |
-
return title, desc
|
58 |
-
|
59 |
-
|
60 |
-
# Интерфейс
|
61 |
-
with gr.Blocks() as app:
|
62 |
-
gr.Markdown("## Генератор метатегов (контроль длины)")
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
|
4 |
+
# Загружаем модель и токенайзер
|
5 |
+
model_name = "cointegrated/rut5-base-summarization"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
8 |
|
9 |
+
def generate_meta_description(product_description):
|
10 |
+
prompt = f"Сгенерируй meta description (до 160 символов) по следующему описанию товара: {product_description}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
13 |
+
summary_ids = model.generate(
|
14 |
+
inputs["input_ids"],
|
15 |
+
max_length=60, # приблизительно ~160 символов на русском
|
16 |
+
num_beams=4,
|
17 |
+
no_repeat_ngram_size=2,
|
18 |
+
early_stopping=True
|
19 |
+
)
|
20 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
21 |
+
|
22 |
+
# обрезаем аккуратно, чтобы не обрывать слова
|
23 |
+
if len(summary) > 160:
|
24 |
+
truncated = summary[:160]
|
25 |
+
last_space = truncated.rfind(' ')
|
26 |
+
summary = truncated[:last_space]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
return summary.strip()
|
29 |
+
|
30 |
+
iface = gr.Interface(
|
31 |
+
fn=generate_meta_description,
|
32 |
+
inputs=gr.Textbox(label="Описание товара", lines=5, placeholder="Например: Красивое мужское пальто из шерсти..."),
|
33 |
+
outputs=gr.Textbox(label="Meta Description (до 160 символов)"),
|
34 |
+
title="Meta Description генератор (русский)",
|
35 |
+
description="Генерирует логичный и краткий meta description по описанию товара (до 160 символов, без обрезания слов)."
|
36 |
+
)
|
37 |
+
|
38 |
+
if __name__ == "__main__":
|
39 |
+
iface.launch()
|