David Kagramanyan commited on
Commit
ed13494
1 Parent(s): 2050a8d
Files changed (4) hide show
  1. app.py +54 -0
  2. classifier.ipynb +210 -0
  3. rfc_30_0.95.joblib +3 -0
  4. src.py +89 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers.utils import logging
3
+ import time
4
+ import joblib
5
+ import re
6
+ import numpy as np
7
+ from transformers.utils import logging
8
+
9
+ from src import get_lexical_desc,get_morphemic_desc, preprocess
10
+
11
+ logging.set_verbosity_info()
12
+ logger = logging.get_logger("transformers")
13
+
14
+
15
+ def classify(input_text_message: str):
16
+
17
+ logger.info(time.strftime("%Y.%m.%d, %H:%M:%S")+' '+'input text message: '+input_text_message)
18
+
19
+ words=preprocess(input_text_message)
20
+ desc_lexical=get_lexical_desc(words).reshape((1,-1))
21
+ desc_morphemic=get_morphemic_desc(words).reshape((1,-1))
22
+
23
+ data=np.concatenate([desc_morphemic,desc_lexical],axis=1)
24
+ prediction=loaded_rf.predict_proba(data)[0]
25
+
26
+ confidences = {labels[i]: float(prediction[i]) for i in range(3)}
27
+
28
+ return confidences
29
+
30
+ labels = ['eastern armenian', 'western armenian', 'grabar (classic) armenian']
31
+
32
+ loaded_rf = joblib.load("rfc_30_0.95.joblib")
33
+
34
+ text='western - Աստուած ըսաւ. «Մեր պատկերով, մեր նմանութեան պէս մարդ ընենք, որ տիրապետէն ծովու ձուկերուն, երկինքի թռչուններուն եւ ընտանի անասուններուն, ամբողջ երկրին, ու երկրի վրայ ըսողացող բոլոր սողուններուն».* 27 Աստուած իր պատկերով ստեղծեց մարդը'
35
+
36
+ examples=[text]
37
+
38
+ with gr.Blocks() as demo:
39
+ gr.Markdown("Check your text for compliance with the NVC rules")
40
+
41
+
42
+ with gr.Tab("Text analysis"):
43
+ text_input = gr.Textbox(lines=2, placeholder="Enter your text here")
44
+ text_button = gr.Button("Define dialect group")
45
+ examples_block = gr.Examples(examples=examples,
46
+ inputs=[text_input], )
47
+
48
+ rec_output = gr.Label(label='Labels', num_top_classes=3)
49
+
50
+ text_button.click(classify, inputs=text_input,
51
+ outputs=[rec_output])
52
+
53
+
54
+ demo.launch(share=False, debug=True)
classifier.ipynb ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "id": "initial_id",
7
+ "metadata": {
8
+ "ExecuteTime": {
9
+ "end_time": "2023-08-06T22:05:00.259026Z",
10
+ "start_time": "2023-08-06T22:05:00.224444Z"
11
+ },
12
+ "collapsed": true
13
+ },
14
+ "outputs": [],
15
+ "source": [
16
+ "import gradio as gr\n",
17
+ "from transformers.utils import logging\n",
18
+ "import time\n",
19
+ "import joblib\n",
20
+ "import re\n",
21
+ "import numpy as np\n",
22
+ "\n",
23
+ "from src import get_lexical_desc,get_morphemic_desc, preprocess\n",
24
+ "\n",
25
+ "logging.set_verbosity_info()\n",
26
+ "logger = logging.get_logger(\"transformers\")"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": 3,
32
+ "id": "5ac71e99efcd2995",
33
+ "metadata": {
34
+ "ExecuteTime": {
35
+ "end_time": "2023-08-06T22:05:01.601612Z",
36
+ "start_time": "2023-08-06T22:05:01.084800Z"
37
+ },
38
+ "collapsed": false
39
+ },
40
+ "outputs": [
41
+ {
42
+ "name": "stderr",
43
+ "output_type": "stream",
44
+ "text": [
45
+ "[Parallel(n_jobs=1)]: Done 49 tasks | elapsed: 0.0s\n"
46
+ ]
47
+ },
48
+ {
49
+ "data": {
50
+ "text/plain": [
51
+ "array([0.07140597, 0.85206542, 0.07652861])"
52
+ ]
53
+ },
54
+ "execution_count": 3,
55
+ "metadata": {},
56
+ "output_type": "execute_result"
57
+ }
58
+ ],
59
+ "source": [
60
+ "loaded_rf = joblib.load(\"rfc_30_0.95.joblib\")\n",
61
+ "folders = ['eastern', 'western', 'grabar']\n",
62
+ "\n",
63
+ "text='western - Աստուած ըսաւ. «Մեր պատկերով, մեր նմանութեան պէս մարդ ընենք, որ տիրապետէն ծովու ձուկերուն, երկինքի թռչուններուն եւ ընտանի անասուններուն, ամբողջ երկրին, ու երկրի վրայ ըսողացող բոլոր սողուններուն».* 27 Աստուած իր պատկերով ստեղծեց մարդը'\n",
64
+ "\n",
65
+ "\n",
66
+ "words=preprocess(text)\n",
67
+ "desc_lexical=get_lexical_desc(words).reshape((1,-1))\n",
68
+ "desc_morphemic=get_morphemic_desc(words).reshape((1,-1))\n",
69
+ "\n",
70
+ "\n",
71
+ "data=np.concatenate([desc_morphemic,desc_lexical],axis=1)\n",
72
+ "\n",
73
+ "loaded_rf.predict_proba(data)[0]"
74
+ ]
75
+ },
76
+ {
77
+ "cell_type": "code",
78
+ "execution_count": 5,
79
+ "id": "6db9ac824255b9f3",
80
+ "metadata": {
81
+ "ExecuteTime": {
82
+ "end_time": "2023-08-06T22:07:50.931031Z",
83
+ "start_time": "2023-08-06T22:07:11.927454Z"
84
+ },
85
+ "collapsed": false
86
+ },
87
+ "outputs": [
88
+ {
89
+ "name": "stdout",
90
+ "output_type": "stream",
91
+ "text": [
92
+ "Running on local URL: http://127.0.0.1:7860\n",
93
+ "Running on public URL: https://f0de872179e31656f7.gradio.live\n",
94
+ "\n",
95
+ "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"
96
+ ]
97
+ },
98
+ {
99
+ "data": {
100
+ "text/html": [
101
+ "<div><iframe src=\"https://f0de872179e31656f7.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
102
+ ],
103
+ "text/plain": [
104
+ "<IPython.core.display.HTML object>"
105
+ ]
106
+ },
107
+ "metadata": {},
108
+ "output_type": "display_data"
109
+ },
110
+ {
111
+ "name": "stderr",
112
+ "output_type": "stream",
113
+ "text": [
114
+ "2023.08.07, 01:06:33 input text message: western - Աստուած ըսաւ. «Մեր պատկերով, մեր նմանութեան պէս մարդ ընենք, որ տիրապետէն ծովու ձուկերուն, երկինքի թռչուններուն եւ ընտանի անասուններուն, ամբողջ երկրին, ու երկրի վրայ ըսողացող բոլոր սողուններուն».* 27 Աստուած իր պատկերով ստեղծեց մարդը\n",
115
+ "[Parallel(n_jobs=1)]: Done 49 tasks | elapsed: 0.0s\n"
116
+ ]
117
+ },
118
+ {
119
+ "name": "stdout",
120
+ "output_type": "stream",
121
+ "text": [
122
+ "Keyboard interruption in main thread... closing server.\n",
123
+ "Killing tunnel 127.0.0.1:7860 <> https://f0de872179e31656f7.gradio.live\n"
124
+ ]
125
+ },
126
+ {
127
+ "data": {
128
+ "text/plain": []
129
+ },
130
+ "execution_count": 5,
131
+ "metadata": {},
132
+ "output_type": "execute_result"
133
+ }
134
+ ],
135
+ "source": [
136
+ "def classify(input_text_message: str):\n",
137
+ "\n",
138
+ " logger.info(time.strftime(\"%Y.%m.%d, %H:%M:%S\")+' '+'input text message: '+input_text_message)\n",
139
+ "\n",
140
+ " words=preprocess(input_text_message)\n",
141
+ " desc_lexical=get_lexical_desc(words).reshape((1,-1))\n",
142
+ " desc_morphemic=get_morphemic_desc(words).reshape((1,-1))\n",
143
+ " \n",
144
+ " data=np.concatenate([desc_morphemic,desc_lexical],axis=1)\n",
145
+ " prediction=loaded_rf.predict_proba(data)[0]\n",
146
+ "\n",
147
+ " confidences = {labels[i]: float(prediction[i]) for i in range(3)}\n",
148
+ " \n",
149
+ " return confidences\n",
150
+ "\n",
151
+ "labels = ['eastern armenian', 'western armenian', 'grabar (classic) armenian']\n",
152
+ "\n",
153
+ "loaded_rf = joblib.load(\"rfc_30_0.95.joblib\")\n",
154
+ "\n",
155
+ "text='western - Աստուած ըսաւ. «Մեր պատկերով, մեր նմանութեան պէս մարդ ընենք, որ տիրապետէն ծովու ձուկերուն, երկինքի թռչուններուն եւ ընտանի անասուններուն, ամբողջ երկրին, ու երկրի վրայ ըսողացող բոլոր սողուններուն».* 27 Աստուած իր պատկերով ստեղծեց մարդը'\n",
156
+ "\n",
157
+ "examples=[text]\n",
158
+ "\n",
159
+ "with gr.Blocks() as demo:\n",
160
+ " gr.Markdown(\"Check your text for compliance with the NVC rules\")\n",
161
+ "\n",
162
+ "\n",
163
+ " with gr.Tab(\"Single message analysis\"):\n",
164
+ " text_input = gr.Textbox(lines=2, placeholder=\"Enter your text here\")\n",
165
+ " text_button = gr.Button(\"Define dialect group\")\n",
166
+ " examples_block = gr.Examples(examples=examples,\n",
167
+ " inputs=[text_input], )\n",
168
+ "\n",
169
+ " rec_output = gr.Label(label='Labels', num_top_classes=3)\n",
170
+ "\n",
171
+ " text_button.click(classify, inputs=text_input,\n",
172
+ " outputs=[rec_output])\n",
173
+ "\n",
174
+ "\n",
175
+ "demo.launch(share=True, debug=True)"
176
+ ]
177
+ },
178
+ {
179
+ "cell_type": "code",
180
+ "execution_count": null,
181
+ "id": "675f27ee2cd8dcc",
182
+ "metadata": {
183
+ "collapsed": false
184
+ },
185
+ "outputs": [],
186
+ "source": []
187
+ }
188
+ ],
189
+ "metadata": {
190
+ "kernelspec": {
191
+ "display_name": "torch",
192
+ "language": "python",
193
+ "name": "torch"
194
+ },
195
+ "language_info": {
196
+ "codemirror_mode": {
197
+ "name": "ipython",
198
+ "version": 2
199
+ },
200
+ "file_extension": ".py",
201
+ "mimetype": "text/x-python",
202
+ "name": "python",
203
+ "nbconvert_exporter": "python",
204
+ "pygments_lexer": "ipython2",
205
+ "version": "2.7.6"
206
+ }
207
+ },
208
+ "nbformat": 4,
209
+ "nbformat_minor": 5
210
+ }
rfc_30_0.95.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e5ce28a74fa4b30546216dde10089eff055e8083830372d3228da751242abba5
3
+ size 78544897
src.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers.utils import logging
3
+ import time
4
+ import joblib
5
+ import re
6
+ import numpy as np
7
+
8
+ logging.set_verbosity_info()
9
+ logger = logging.get_logger("transformers")
10
+
11
+ western_stop = ['ենք', 'էի', 'թ', 'ին', 'մենք', 'որոնք', 'պիտի', 'և', 'որպեսզի', 'վրայ', 'կ՚', 'կը', 'մը', 'մըն',
12
+ 'անոր', 'ալ', 'ան', 'քեզ', 'եթէ', 'թէ', 'որպէս']
13
+
14
+ grabar_stop = ['դու', 'եք', 'ըստ', 'նա', 'պիտի', 'վրայ', 'զի', 'ընդ', 'քո', 'քեզ', 'եթէ', 'թէ', 'որպէս']
15
+
16
+ eastern_stop = ['դու', 'ենք', 'եք', 'էի', 'ըստ', 'ին', 'հետո', 'մենք', 'մեջ', 'նա', 'նաև', 'նրա', 'նրանք', 'որը',
17
+ 'որոնք', 'որպես', 'ում', 'վրա', 'և', 'որպեսզի']
18
+
19
+ western_stop = set(western_stop)
20
+ grabar_stop = set(grabar_stop)
21
+ eastern_stop = set(eastern_stop)
22
+
23
+ def get_lexical_desc(words):
24
+ intersect_western = len(set(words) & western_stop) / len(western_stop)
25
+ intersect_grabar = len(set(words) & grabar_stop) / len(grabar_stop)
26
+ intersect_eastern = len(set(words) & eastern_stop) // len(eastern_stop)
27
+
28
+ return np.array([intersect_western, intersect_grabar, intersect_eastern])
29
+
30
+ grabar_suffixes = ['աւք', 'եալ', 'եան', 'իւք', 'ոյց', 'ովք', 'ուց', 'ուցան']
31
+
32
+ grabar_prefixes = ['ապա', 'արտ', 'բաղ', 'բաղա', 'դեր', 'ենթ', 'ենթա', 'ընթա', ' համ', 'համա', 'հան', 'հոմ', 'հոմա',
33
+ 'տար', 'տարա']
34
+
35
+ eastern_suffixes = ['աբար', 'ագին', 'ագույն', 'ածո', 'ածու', 'ական', 'ակերտ', 'ային', 'անակ', 'անի', 'անոց', 'անք',
36
+ 'ապան', 'ապանակ', 'ապատ', 'ապես', 'աստան', 'ավետ', 'ավուն', 'արան', 'արար', 'արեն', 'արք', 'ացի',
37
+ 'ացն-', 'ացու', 'բան', 'բար', 'գին', 'գույն', 'եղեն', 'ենի', 'երեն', 'երորդ', 'եցն-', 'լիկ', 'կերտ',
38
+ 'կոտ', 'մունք ', 'յալ', 'յակ', 'յան', 'յանց', 'յուն նախա-', 'ներ', 'նոց', 'ոնք', 'ովին', 'որդ',
39
+ 'որեն', 'ոցի', 'ուք', 'պան', 'պանակ', 'ստան', 'ված', 'վածք', 'ավոր', 'վոր', 'ություն', 'ուլ', 'ուկ',
40
+ 'ուհի', 'ում', 'ույթ', 'ույր', 'ուն', 'ուտ', 'ուրդ', 'ուց']
41
+
42
+ eastern_prefixes = ['ամենա', 'այսր', 'անդր', 'ապա', 'ավտո', 'արտ', 'արտա', 'բենզա', ', գեր', 'գերա', 'դեր', 'ենթա',
43
+ 'եվրա', ' էլեկտրա', 'թեր', 'թերա', 'կենս', 'կինո', 'հակ', 'հակա', 'համ', 'համա', 'հար', 'հարա',
44
+ 'հեռա', 'հեռուստա', 'հոմա', 'մակ', 'մակրո', 'միկրո', 'միջ', 'նախ', 'ներ', 'ստոր', 'վեր', 'վերա',
45
+ 'տար', 'տարա', 'փոխ', 'քառ', 'քառա']
46
+
47
+ western_reform = ['իլ', 'իուն', 'եան', 'յ', 'օ', 'է', 'յ', 'վո', 'ոյ', 'եա', 'եօ', 'իւ', 'ու', 'ւ,' 'յե', 'եյ', 'զի',
48
+ 'եւ', 'ել', 'յուն', 'յան', 'ում', 'ո', 'ե', 'հ', 'ո', 'ույ', 'յա', 'յո', 'յու', 'վ', 'ե', ]
49
+
50
+ morphems=[]
51
+ morphems.extend(grabar_suffixes)
52
+ morphems.extend(grabar_prefixes)
53
+ morphems.extend(eastern_suffixes)
54
+ morphems.extend(eastern_prefixes)
55
+ morphems.extend(western_reform)
56
+
57
+ def get_morphemic_desc(words):
58
+ res=[]
59
+ for morphema in morphems:
60
+ positions = []
61
+ for word in words:
62
+ pos = word.find(morphema)
63
+ if pos != -1:
64
+ positions.append((pos+1)/len(word))
65
+ if len(positions)==0:
66
+
67
+ res.append(0)
68
+ else:
69
+ # std=np.std(positions)
70
+ res.append(np.mean(positions))
71
+
72
+ return np.array(res)
73
+
74
+
75
+ def preprocess(text):
76
+ punctuation=['.','-',',','!','?','(','—',')','՛','։','՝','՜','’','«','»','*','\n','=',':','[',']','/',';','․','`','\t','%','$','\xa0','\r','_','●','՜', ',', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', '-', '—', '։','՞']
77
+ text=text.lower()
78
+
79
+ for spaced in punctuation:
80
+ text = text.replace(spaced, '')
81
+
82
+ text = re.sub(" +", " ", text)
83
+ txt = text.split(' ')
84
+ words = [t for t in txt if t != '']
85
+ return words
86
+
87
+
88
+
89
+