File size: 7,009 Bytes
ed13494
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{
 "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": [
       "<div><iframe src=\"https://f0de872179e31656f7.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "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
}