| 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_CHARSE=2000 | |
| def runk(text): | |
| if text is None or not text.strip(): | |
| return 'Error', None,None | |
| text=text.strip() | |
| if len(text)>MAX_CHARSE: | |
| text=text[:MAX_CHARSE] | |
| t0=time.time() | |
| try: | |
| result=sentiment_model(text) | |
| latency=round((time.time()-t0)*1000,1) | |
| return 'okay',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(runk,inputs=inp,outputs=[status,out,latency]) | |
| gr.Examples( | |
| examples=[['Я люблю этот продукт. Он великолепен'], | |
| ["Это наихудший опыт"], | |
| ['Никакой специфики']], | |
| inputs=inp | |
| ) | |
| demo.launch() | |