Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,37 +4,36 @@ import gradio as gr
|
|
4 |
import re
|
5 |
import json
|
6 |
|
7 |
-
# Инициализация модели
|
8 |
def load_model():
|
9 |
tokenizer = AutoTokenizer.from_pretrained(
|
10 |
"cointegrated/rut5-base-multitask",
|
11 |
-
legacy=False
|
12 |
)
|
13 |
model = T5ForConditionalGeneration.from_pretrained(
|
14 |
"cointegrated/rut5-base-multitask",
|
15 |
-
torch_dtype=torch.float16
|
16 |
)
|
17 |
return tokenizer, model
|
18 |
|
19 |
tokenizer, model = load_model()
|
20 |
|
21 |
-
#
|
22 |
-
def
|
23 |
try:
|
24 |
-
#
|
25 |
prompt = f"""
|
26 |
-
Сгенерируй SEO-метатеги
|
27 |
Требования:
|
28 |
-
- Title: до 60 символов,
|
29 |
-
- Description: до 160 символов,
|
30 |
|
31 |
Описание товара:
|
32 |
{description.strip()}
|
33 |
|
34 |
-
Выведи только JSON
|
35 |
{{"title": "...", "description": "..."}}
|
36 |
"""
|
37 |
-
# Токенизация и генерация
|
38 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
39 |
|
40 |
with torch.no_grad():
|
@@ -47,21 +46,22 @@ def generate_seo_tags(description):
|
|
47 |
early_stopping=True
|
48 |
)
|
49 |
|
50 |
-
#
|
51 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
52 |
json_match = re.search(r'\{.*\}', result, re.DOTALL)
|
53 |
|
54 |
if json_match:
|
55 |
-
|
56 |
-
#
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
61 |
except Exception as e:
|
62 |
-
print(f"
|
63 |
|
64 |
-
#
|
65 |
clean_text = re.sub(r'[^\w\s]', '', description)
|
66 |
return {
|
67 |
"title": clean_text[:60],
|
@@ -70,32 +70,25 @@ def generate_seo_tags(description):
|
|
70 |
|
71 |
# Интерфейс Gradio
|
72 |
with gr.Blocks(title="SEO Генератор") as app:
|
73 |
-
gr.Markdown("## Генератор
|
74 |
|
75 |
with gr.Row():
|
76 |
with gr.Column():
|
77 |
-
|
78 |
-
label="
|
79 |
-
placeholder="
|
80 |
lines=5
|
81 |
)
|
82 |
-
|
83 |
|
84 |
with gr.Column():
|
85 |
-
|
86 |
-
label="SEO-метатеги",
|
87 |
-
interactive=False
|
88 |
-
)
|
89 |
|
90 |
-
|
91 |
-
fn=
|
92 |
-
inputs=
|
93 |
-
outputs=
|
94 |
)
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
-
app.launch(
|
98 |
-
server_name="0.0.0.0",
|
99 |
-
server_port=7860,
|
100 |
-
show_error=True
|
101 |
-
)
|
|
|
4 |
import re
|
5 |
import json
|
6 |
|
7 |
+
# Инициализация модели
|
8 |
def load_model():
|
9 |
tokenizer = AutoTokenizer.from_pretrained(
|
10 |
"cointegrated/rut5-base-multitask",
|
11 |
+
legacy=False
|
12 |
)
|
13 |
model = T5ForConditionalGeneration.from_pretrained(
|
14 |
"cointegrated/rut5-base-multitask",
|
15 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
16 |
)
|
17 |
return tokenizer, model
|
18 |
|
19 |
tokenizer, model = load_model()
|
20 |
|
21 |
+
# Функция генерации мета-тегов
|
22 |
+
def generate_meta_tags(description):
|
23 |
try:
|
24 |
+
# Формируем четкий промт
|
25 |
prompt = f"""
|
26 |
+
Сгенерируй SEO-метатеги для товара на основе описания.
|
27 |
Требования:
|
28 |
+
- Title: до 60 символов, ключевые слова
|
29 |
+
- Description: до 160 символов, преимущества
|
30 |
|
31 |
Описание товара:
|
32 |
{description.strip()}
|
33 |
|
34 |
+
Выведи только JSON-объект:
|
35 |
{{"title": "...", "description": "..."}}
|
36 |
"""
|
|
|
37 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
38 |
|
39 |
with torch.no_grad():
|
|
|
46 |
early_stopping=True
|
47 |
)
|
48 |
|
49 |
+
# Обработка результата
|
50 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
51 |
json_match = re.search(r'\{.*\}', result, re.DOTALL)
|
52 |
|
53 |
if json_match:
|
54 |
+
tags = json.loads(json_match.group())
|
55 |
+
# Проверяем и обрезаем длину
|
56 |
+
return {
|
57 |
+
"title": tags.get("title", "")[:60],
|
58 |
+
"description": tags.get("description", "")[:160]
|
59 |
+
}
|
60 |
+
|
61 |
except Exception as e:
|
62 |
+
print(f"Error: {e}")
|
63 |
|
64 |
+
# Fallback вариант
|
65 |
clean_text = re.sub(r'[^\w\s]', '', description)
|
66 |
return {
|
67 |
"title": clean_text[:60],
|
|
|
70 |
|
71 |
# Интерфейс Gradio
|
72 |
with gr.Blocks(title="SEO Генератор") as app:
|
73 |
+
gr.Markdown("## Генератор мета-тегов для товаров")
|
74 |
|
75 |
with gr.Row():
|
76 |
with gr.Column():
|
77 |
+
input_desc = gr.Textbox(
|
78 |
+
label="Введите описание товара",
|
79 |
+
placeholder="Например: Смартфон Samsung Galaxy S23...",
|
80 |
lines=5
|
81 |
)
|
82 |
+
btn = gr.Button("Сгенерировать", variant="primary")
|
83 |
|
84 |
with gr.Column():
|
85 |
+
output_tags = gr.JSON(label="Результат")
|
|
|
|
|
|
|
86 |
|
87 |
+
btn.click(
|
88 |
+
fn=generate_meta_tags,
|
89 |
+
inputs=input_desc,
|
90 |
+
outputs=output_tags
|
91 |
)
|
92 |
|
93 |
if __name__ == "__main__":
|
94 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|