{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "initial_id", "metadata": { "ExecuteTime": { "end_time": "2023-08-06T22:05:00.259026Z", "start_time": "2023-08-06T22:05:00.224444Z" }, "collapsed": true }, "outputs": [], "source": [ "import gradio as gr\n", "from transformers.utils import logging\n", "import time\n", "import joblib\n", "import re\n", "import numpy as np\n", "\n", "from src import get_lexical_desc,get_morphemic_desc, preprocess\n", "\n", "logging.set_verbosity_info()\n", "logger = logging.get_logger(\"transformers\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "5ac71e99efcd2995", "metadata": { "ExecuteTime": { "end_time": "2023-08-06T22:05:01.601612Z", "start_time": "2023-08-06T22:05:01.084800Z" }, "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[Parallel(n_jobs=1)]: Done 49 tasks | elapsed: 0.0s\n" ] }, { "data": { "text/plain": [ "array([0.07140597, 0.85206542, 0.07652861])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loaded_rf = joblib.load(\"rfc_30_0.95.joblib\")\n", "folders = ['eastern', 'western', 'grabar']\n", "\n", "text='western - Աստուած ըսաւ. «Մեր պատկերով, մեր նմանութեան պէս մարդ ընենք, որ տիրապետէն ծովու ձուկերուն, երկինքի թռչուններուն եւ ընտանի անասուններուն, ամբողջ երկրին, ու երկրի վրայ ըսողացող բոլոր սողուններուն».* 27 Աստուած իր պատկերով ստեղծեց մարդը'\n", "\n", "\n", "words=preprocess(text)\n", "desc_lexical=get_lexical_desc(words).reshape((1,-1))\n", "desc_morphemic=get_morphemic_desc(words).reshape((1,-1))\n", "\n", "\n", "data=np.concatenate([desc_morphemic,desc_lexical],axis=1)\n", "\n", "loaded_rf.predict_proba(data)[0]" ] }, { "cell_type": "code", "execution_count": 5, "id": "6db9ac824255b9f3", "metadata": { "ExecuteTime": { "end_time": "2023-08-06T22:07:50.931031Z", "start_time": "2023-08-06T22:07:11.927454Z" }, "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "Running on public URL: https://f0de872179e31656f7.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "2023.08.07, 01:06:33 input text message: western - Աստուած ըսաւ. «Մեր պատկերով, մեր նմանութեան պէս մարդ ընենք, որ տիրապետէն ծովու ձուկերուն, երկինքի թռչուններուն եւ ընտանի անասուններուն, ամբողջ երկրին, ու երկրի վրայ ըսողացող բոլոր սողուններուն».* 27 Աստուած իր պատկերով ստեղծեց մարդը\n", "[Parallel(n_jobs=1)]: Done 49 tasks | elapsed: 0.0s\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Keyboard interruption in main thread... closing server.\n", "Killing tunnel 127.0.0.1:7860 <> https://f0de872179e31656f7.gradio.live\n" ] }, { "data": { "text/plain": [] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def classify(input_text_message: str):\n", "\n", " logger.info(time.strftime(\"%Y.%m.%d, %H:%M:%S\")+' '+'input text message: '+input_text_message)\n", "\n", " words=preprocess(input_text_message)\n", " desc_lexical=get_lexical_desc(words).reshape((1,-1))\n", " desc_morphemic=get_morphemic_desc(words).reshape((1,-1))\n", " \n", " data=np.concatenate([desc_morphemic,desc_lexical],axis=1)\n", " prediction=loaded_rf.predict_proba(data)[0]\n", "\n", " confidences = {labels[i]: float(prediction[i]) for i in range(3)}\n", " \n", " return confidences\n", "\n", "labels = ['eastern armenian', 'western armenian', 'grabar (classic) armenian']\n", "\n", "loaded_rf = joblib.load(\"rfc_30_0.95.joblib\")\n", "\n", "text='western - Աստուած ըսաւ. «Մեր պատկերով, մեր նմանութեան պէս մարդ ընենք, որ տիրապետէն ծովու ձուկերուն, երկինքի թռչուններուն եւ ընտանի անասուններուն, ամբողջ երկրին, ու երկրի վրայ ըսողացող բոլոր սողուններուն».* 27 Աստուած իր պատկերով ստեղծեց մարդը'\n", "\n", "examples=[text]\n", "\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"Check your text for compliance with the NVC rules\")\n", "\n", "\n", " with gr.Tab(\"Single message analysis\"):\n", " text_input = gr.Textbox(lines=2, placeholder=\"Enter your text here\")\n", " text_button = gr.Button(\"Define dialect group\")\n", " examples_block = gr.Examples(examples=examples,\n", " inputs=[text_input], )\n", "\n", " rec_output = gr.Label(label='Labels', num_top_classes=3)\n", "\n", " text_button.click(classify, inputs=text_input,\n", " outputs=[rec_output])\n", "\n", "\n", "demo.launch(share=True, debug=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "675f27ee2cd8dcc", "metadata": { "collapsed": false }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "torch", "language": "python", "name": "torch" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }