# app.py import time import gradio as gr from transformers import pipeline # Пример: sentiment-analysis TASK = "text-classification" MODEL_NAME_1 = "s-nlp/russian_toxicity_classifier" pipe = pipeline(TASK, model=MODEL_NAME_1) 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 "ОК.", 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_1} """) 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=[ ["Я тебя люблю."], ["Я тебя ненавижу."], ["Все в порядке."] ], inputs=inp ) demo.launch()