File size: 1,599 Bytes
a3c6681
 
 
 
492daba
a3c6681
e3469d3
a3c6681
e3469d3
a3c6681
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e96d6b1
a3c6681
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
import gradio as gr
from transformers import pipeline

TASK = "sentiment-analysis"
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment-latest"
MODEL_NAME_2 = "MonoHime/rubert-base-cased-sentiment-new"

pipe = pipeline(TASK, model = MODEL_NAME_2)

MAX_CHARS = 2000

def run (text:str):
    if (text is None or not text.strip()):
        return "Ошибка: введено пустое значение!", None, None

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

    t0 = time.time()

    try:
        result = pipe(text)
        latency = round((time.time() - t0) * 1000, 1)
        return "OK", result, f"{latency} ms"
    except Exception as e:
        return f"Ошибка: {type(e).name}: {e}", None, None

with gr.Blocks() as demo:
    gr.Markdown(f"""
    # NLP-приложение (Hugging Face Spaces + Gradio)
    Задача: {TASK}  
    Модель: {MODEL_NAME_2}
    """)

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

    btn.click(run, inputs = inp, outputs = [status, out, latency])
    gr.Examples(
        examples=[
            ["I love this product! It works great."],
            ["This is the worst experience ever."],
            ["It's okay, nothing special."]
        ],
        inputs=inp
    )
    
demo.launch()