Shirobokov's picture
Update app.py
12b33bc verified
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()