File size: 1,317 Bytes
7ab3fbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import gradio as gr
from transformers import pipeline
import time

# Загружаем модель ПЕРВОЙ
try:
    classifier = pipeline(
        "text-classification", 
        model="cointegrated/rubert-tiny2",
        truncation=True
    )
except Exception as e:
    print(f"Ошибка загрузки модели: {e}")
    raise

# Функция классификации
def classify(item: str, categories: str) -> str:
    try:
        prompt = f"Отнеси '{item}' к категории: {categories}. Ответь только категорией."
        result = classifier(prompt)
        return result[0]['label']
    except Exception as e:
        return f"Ошибка: {str(e)}"

# Создаем интерфейс ПОСЛЕ загрузки модели
iface = gr.Interface(
    fn=classify,
    inputs=[
        gr.Textbox(label="Товар"), 
        gr.Textbox(label="Категории (через запятую)", value="Овощи, Инструменты")
    ],
    outputs="text",
    title="Классификатор"
)

# Явно запускаем сервер
iface.launch(
    server_name="0.0.0.0",
    server_port=7860,
    show_error=True
)

# Бесконечный цикл, чтобы сервер не завершался
while True:
    time.sleep(60)