hh1199 commited on
Commit
0a1dfe8
·
verified ·
1 Parent(s): 3ee972d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -12
app.py CHANGED
@@ -1,23 +1,54 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- models = {
5
- "ruBert-tiny2": "cointegrated/rubert-tiny2",
6
  "ruRoberta-large": "sberbank-ai/ruRoberta-large",
 
7
  "multilingual-e5": "intfloat/multilingual-e5-base"
8
  }
9
 
10
- def classify(model, text, labels):
11
- classifier = pipeline("zero-shot-classification", model=models[model])
12
- result = classifier(text, [l.strip() for l in labels.split(",")])
13
- return result['labels'][0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- gr.Interface(
16
  fn=classify,
17
  inputs=[
18
- gr.Dropdown(list(models.keys())),
19
- gr.Textbox(),
20
- gr.Textbox(value="Овощи, Инструменты, Техника")
21
  ],
22
- outputs=gr.Textbox()
23
- ).launch()
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import re
4
 
5
+ MODELS = {
 
6
  "ruRoberta-large": "sberbank-ai/ruRoberta-large",
7
+ "rubert-tiny2": "cointegrated/rubert-tiny2",
8
  "multilingual-e5": "intfloat/multilingual-e5-base"
9
  }
10
 
11
+ def classify(model_name: str, item: str, categories: str) -> str:
12
+ # Нормализация текста
13
+ item = re.sub(r"[^а-яА-ЯёЁ]", " ", item).lower().strip()
14
+
15
+ classifier = pipeline(
16
+ "zero-shot-classification",
17
+ model=MODELS[model_name],
18
+ device=-1
19
+ )
20
+
21
+ hypothesis_template = (
22
+ "Примеры категорий:\n"
23
+ "- молоток → инструменты\n"
24
+ "- картофель → овощи\n"
25
+ "Категория для '{}' → "
26
+ )
27
+
28
+ result = classifier(
29
+ item,
30
+ candidate_labels=[c.strip().lower() for c in categories.split(",")],
31
+ hypothesis_template=hypothesis_template,
32
+ multi_label=False
33
+ )
34
+
35
+ if result['scores'][0] < 0.3:
36
+ return "Категория не определена"
37
+
38
+ return f"{result['labels'][0].capitalize()} ({result['scores'][0]:.2f})"
39
 
40
+ iface = gr.Interface(
41
  fn=classify,
42
  inputs=[
43
+ gr.Dropdown(list(MODELS.keys()), label="Модель"),
44
+ gr.Textbox(label="Товар"),
45
+ gr.Textbox(label="Категории", value="Инструменты, Овощи, Техника")
46
  ],
47
+ outputs=gr.Textbox(label="Результат"),
48
+ examples=[
49
+ ["ruRoberta-large", "Аккумуляторная дрель", "Инструменты, Техника"],
50
+ ["rubert-tiny2", "Свёкла кормовая", "Овощи, Фураж"]
51
+ ]
52
+ )
53
+
54
+ iface.launch()