Spaces:
Sleeping
Sleeping
| import time | |
| import gradio as gr | |
| from transformers import pipeline | |
| TASK = "sentiment-analysis" | |
| MODEL_NAME = 'cardiffnlp/twitter-roberta-base-sentiment-latest' | |
| pipe = pipeline(TASK, model = MODEL_NAME) | |
| 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] | |
| return | |
| 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} | |
| """) | |
| 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() |