File size: 1,521 Bytes
c0b90a4
 
 
 
 
 
793c39f
 
c0b90a4
 
 
 
793c39f
c0b90a4
 
793c39f
 
 
 
c0b90a4
 
 
793c39f
c0b90a4
793c39f
c0b90a4
 
 
 
793c39f
 
 
 
 
12b33bc
793c39f
 
 
 
c0b90a4
793c39f
 
 
 
 
 
 
12b33bc
793c39f
 
c0b90a4
 
793c39f
 
 
 
 
12b33bc
c0b90a4
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
import time
from transformers import pipeline

TASK = 'text-classification'
MODEL_NAME = 'Aniemore/rubert-tiny2-russian-emotion-detection'
sentiment_model = pipeline(TASK, model=MODEL_NAME)
MAX_CHARS = 2000

def runk(text):
    if text is None or not text.strip():
        return "Error", None, None

    text = text.strip()

    if len(text) > MAX_CHARS:
        text = text[:MAX_CHARS]

    t0 = time.time()

    try:
        result = sentiment_model(text)
        latency = round((time.time() - t0) * 1000, 1)
        return "Ok", result, f"{latency} ms"

    except Exception as e:
        return f"Error: {type(e).__name__}: {e}", None, None

with gr.Blocks() as demo:
    gr.Markdown(f"""
    **Задача:** {TASK}  
    **Модель:** {MODEL_NAME}
    """)

    inp = gr.Textbox(
        label="Введите текст",
        lines=6,
        placeholder="Скопируйте сюда текст"
    )

    btn = gr.Button("Обработать")
    status = gr.Textbox(label="Статус")
    out = gr.JSON(label="Результат модели")
    latency = gr.Textbox(label="Время ответа")

    btn.click(
        fn=runk,
        inputs=inp,
        outputs=[status, out, latency]
    )

    gr.Examples(
        examples=[
            ["Я люблю этот продукт, он великолепен"],
            ["Это наихудший опыт"],
            ["Никакой специфики"]
        ],
        inputs=inp
    )

demo.launch()