Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
-
import re
|
4 |
|
|
|
5 |
classifier = pipeline(
|
6 |
"zero-shot-classification",
|
7 |
-
model="cointegrated/rubert-tiny2
|
8 |
device=-1
|
9 |
)
|
10 |
|
11 |
-
def preprocess(text: str) -> str:
|
12 |
-
"""Нормализация текста для улучшения качества классификации"""
|
13 |
-
text = re.sub(r"[^а-яА-ЯёЁa-zA-Z0-9]", " ", text) # Удаляем спецсимволы
|
14 |
-
text = re.sub(r"\s+", " ", text).strip().lower() # Нормализуем пробелы и регистр
|
15 |
-
return text
|
16 |
-
|
17 |
def classify(item: str, categories: str) -> str:
|
18 |
-
|
19 |
-
item = preprocess(item)
|
20 |
-
categories_list = [preprocess(c) for c in categories.split(",")]
|
21 |
-
|
22 |
-
# Формируем контекстные примеры для улучшения понимания
|
23 |
-
hypothesis_template = (
|
24 |
-
"Примеры категоризации:\n"
|
25 |
-
"- 'молоток' → инструменты\n"
|
26 |
-
"- 'морковь' → овощи\n"
|
27 |
-
"Теперь определи категорию для: '{}' → "
|
28 |
-
)
|
29 |
-
|
30 |
result = classifier(
|
31 |
item,
|
32 |
candidate_labels=categories_list,
|
33 |
-
hypothesis_template=hypothesis_template,
|
34 |
multi_label=False
|
35 |
)
|
36 |
-
|
37 |
-
# Фильтр низкой уверенности
|
38 |
-
if result['scores'][0] < 0.5:
|
39 |
-
return "Не удалось определить категорию"
|
40 |
-
|
41 |
-
return f"{result['labels'][0].capitalize()} (точность: {result['scores'][0]:.2f})"
|
42 |
|
43 |
iface = gr.Interface(
|
44 |
fn=classify,
|
45 |
inputs=[
|
46 |
-
gr.Textbox(label="
|
47 |
-
|
48 |
-
gr.Textbox(label="Категории через запятую",
|
49 |
-
value="Инструменты, Овощи, Техника, Упаковка")
|
50 |
],
|
51 |
outputs=gr.Textbox(label="Результат"),
|
52 |
examples=[
|
53 |
-
["
|
54 |
-
["
|
55 |
-
|
56 |
-
],
|
57 |
-
title="Умный классификатор товаров",
|
58 |
-
description="🚀 Версия с улучшенной точностью за счет:\n"
|
59 |
-
"- Специальной модели классификации\n"
|
60 |
-
"- Предобработки текста\n"
|
61 |
-
"- Контекстных примеров\n"
|
62 |
-
"- Фильтра низкой уверенности"
|
63 |
)
|
64 |
|
65 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
+
# Используем проверенную модель
|
5 |
classifier = pipeline(
|
6 |
"zero-shot-classification",
|
7 |
+
model="cointegrated/rubert-tiny2",
|
8 |
device=-1
|
9 |
)
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def classify(item: str, categories: str) -> str:
|
12 |
+
categories_list = [c.strip() for c in categories.split(",")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
result = classifier(
|
14 |
item,
|
15 |
candidate_labels=categories_list,
|
|
|
16 |
multi_label=False
|
17 |
)
|
18 |
+
return f"{result['labels'][0]} (score: {result['scores'][0]:.2f})"
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
iface = gr.Interface(
|
21 |
fn=classify,
|
22 |
inputs=[
|
23 |
+
gr.Textbox(label="Товар"),
|
24 |
+
gr.Textbox(label="Категории", value="Инструменты, Овощи, Техника")
|
|
|
|
|
25 |
],
|
26 |
outputs=gr.Textbox(label="Результат"),
|
27 |
examples=[
|
28 |
+
["Молоток", "Инструменты, Овощи"],
|
29 |
+
["Морковь", "Овощи, Фрукты"]
|
30 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
)
|
32 |
|
33 |
iface.launch()
|