Best-Sentiment / app.py
sashadd's picture
Update app.py
e96d6b1 verified
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()