David Kagramanyan commited on
Commit
b063ad5
·
1 Parent(s): a1b76f2

fixed labels

Browse files
Files changed (2) hide show
  1. app.py +36 -9
  2. ner_v0_model.ipynb +126 -9
app.py CHANGED
@@ -1,9 +1,12 @@
1
  import gradio as gr
 
 
2
  import requests
3
 
 
4
 
5
- def compute_ner(input_text_message):
6
 
 
7
  endpoint_url = 'https://on1m82uknekghqeh.us-east-1.aws.endpoints.huggingface.cloud'
8
 
9
  headers = {
@@ -17,14 +20,38 @@ def compute_ner(input_text_message):
17
 
18
  response = requests.post(endpoint_url, headers=headers, json=json_data)
19
 
20
- result = response.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- return {"text": input_text_message, "entities": result}
23
 
 
 
24
 
25
- examples = ['You are dick', 'My dad is an asshole and took his anger out on my mom by verbally abusing her and when she left he eventually moved on to my brother']
26
- iface = gr.Interface(fn=compute_ner,
27
- inputs=gr.Textbox(placeholder="Enter sentence here"),
28
- outputs=gr.HighlightedText(),
29
- examples=examples)
30
- iface.launch()
 
1
  import gradio as gr
2
+ import os
3
+
4
  import requests
5
 
6
+ from spacy import displacy
7
 
 
8
 
9
+ def compute_ner(input_text_message):
10
  endpoint_url = 'https://on1m82uknekghqeh.us-east-1.aws.endpoints.huggingface.cloud'
11
 
12
  headers = {
 
20
 
21
  response = requests.post(endpoint_url, headers=headers, json=json_data)
22
 
23
+ tokens = response.json()
24
+
25
+ entities = []
26
+
27
+ for token in tokens:
28
+ label = token["entity"]
29
+
30
+ if label == "I-Observation" or label == "B-Observation":
31
+ label = "Observation"
32
+ token["label"] = label
33
+ entities.append(token)
34
+
35
+ if label == "I-Evaluation" or label == "B-Evaluation":
36
+ label = "Evaluation"
37
+ token["label"] = label
38
+ entities.append(token)
39
+
40
+ params = [{"text": input_text_message,
41
+ "ents": entities,
42
+ "title": None}]
43
+
44
+ return displacy.render(params, style="ent", manual=True, options={
45
+ "colors": {
46
+ "Observation": "#9bddff",
47
+ "Evaluation": "#f08080",
48
+ },
49
+ })
50
 
 
51
 
52
+ examples = ['You are dick',
53
+ 'My dad is an asshole and took his anger out on my mom by verbally abusing her and when she left he eventually moved on to my brother']
54
 
55
+ iface = gr.Interface(fn=compute_ner, inputs=gr.inputs.Textbox(lines=5, placeholder="Enter your text here"),
56
+ outputs="html", examples=examples)
57
+ iface.launch()
 
 
 
ner_v0_model.ipynb CHANGED
@@ -12,7 +12,12 @@
12
  "outputs": [],
13
  "source": [
14
  "import gradio as gr\n",
15
- "import requests"
 
 
 
 
 
16
  ]
17
  },
18
  {
@@ -22,7 +27,6 @@
22
  "source": [
23
  "\n",
24
  "def compute_ner(input_text_message):\n",
25
- "\n",
26
  " endpoint_url = 'https://on1m82uknekghqeh.us-east-1.aws.endpoints.huggingface.cloud'\n",
27
  "\n",
28
  " headers = {\n",
@@ -36,16 +40,40 @@
36
  "\n",
37
  " response = requests.post(endpoint_url, headers=headers, json=json_data)\n",
38
  "\n",
39
- " result = response.json()\n",
 
 
 
 
 
40
  "\n",
41
- " return {\"text\": input_text_message, \"entities\": result}\n",
 
 
 
42
  "\n",
 
 
 
 
43
  "\n",
44
- "examples = ['You are dick', 'My dad is an asshole and took his anger out on my mom by verbally abusing her and when she left he eventually moved on to my brother']\n",
45
- "iface = gr.Interface(fn=compute_ner,\n",
46
- " inputs=gr.Textbox(placeholder=\"Enter sentence here\"),\n",
47
- " outputs=gr.HighlightedText(),\n",
48
- " examples=examples)\n",
 
 
 
 
 
 
 
 
 
 
 
 
49
  "iface.launch()"
50
  ],
51
  "metadata": {
@@ -54,6 +82,95 @@
54
  "name": "#%%\n"
55
  }
56
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  }
58
  ],
59
  "metadata": {
 
12
  "outputs": [],
13
  "source": [
14
  "import gradio as gr\n",
15
+ "import os\n",
16
+ "\n",
17
+ "os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'\n",
18
+ "import requests\n",
19
+ "\n",
20
+ "from spacy import displacy"
21
  ]
22
  },
23
  {
 
27
  "source": [
28
  "\n",
29
  "def compute_ner(input_text_message):\n",
 
30
  " endpoint_url = 'https://on1m82uknekghqeh.us-east-1.aws.endpoints.huggingface.cloud'\n",
31
  "\n",
32
  " headers = {\n",
 
40
  "\n",
41
  " response = requests.post(endpoint_url, headers=headers, json=json_data)\n",
42
  "\n",
43
+ " tokens = response.json()\n",
44
+ "\n",
45
+ " entities = []\n",
46
+ "\n",
47
+ " for token in tokens:\n",
48
+ " label = token[\"entity\"]\n",
49
  "\n",
50
+ " if label == \"I-Observation\" or label == \"B-Observation\":\n",
51
+ " label = \"Observation\"\n",
52
+ " token[\"label\"] = label\n",
53
+ " entities.append(token)\n",
54
  "\n",
55
+ " if label == \"I-Evaluation\" or label == \"B-Evaluation\":\n",
56
+ " label = \"Evaluation\"\n",
57
+ " token[\"label\"] = label\n",
58
+ " entities.append(token)\n",
59
  "\n",
60
+ " params = [{\"text\": input_text_message,\n",
61
+ " \"ents\": entities,\n",
62
+ " \"title\": None}]\n",
63
+ "\n",
64
+ " return displacy.render(params, style=\"ent\", manual=True, options={\n",
65
+ " \"colors\": {\n",
66
+ " \"Observation\": \"#9bddff\",\n",
67
+ " \"Evaluation\": \"#f08080\",\n",
68
+ " },\n",
69
+ " })\n",
70
+ "\n",
71
+ "\n",
72
+ "examples = ['You are dick',\n",
73
+ " 'My dad is an asshole and took his anger out on my mom by verbally abusing her and when she left he eventually moved on to my brother']\n",
74
+ "\n",
75
+ "iface = gr.Interface(fn=compute_ner, inputs=gr.inputs.Textbox(lines=5, placeholder=\"Enter your text here\"),\n",
76
+ " outputs=\"html\", examples=examples)\n",
77
  "iface.launch()"
78
  ],
79
  "metadata": {
 
82
  "name": "#%%\n"
83
  }
84
  }
85
+ },
86
+ {
87
+ "cell_type": "code",
88
+ "execution_count": null,
89
+ "outputs": [],
90
+ "source": [
91
+ "endpoint_url = 'https://on1m82uknekghqeh.us-east-1.aws.endpoints.huggingface.cloud'\n",
92
+ "\n",
93
+ "headers = {\n",
94
+ " 'Authorization': 'Bearer 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',\n",
95
+ " 'Content-Type': 'application/json',\n",
96
+ "}\n",
97
+ "\n",
98
+ "json_data = {\n",
99
+ " 'inputs': 'you are dick',\n",
100
+ "}\n",
101
+ "\n",
102
+ "response = requests.post(endpoint_url, headers=headers, json=json_data)\n",
103
+ "\n",
104
+ "tokens = response.json()"
105
+ ],
106
+ "metadata": {
107
+ "collapsed": false,
108
+ "pycharm": {
109
+ "name": "#%%\n"
110
+ }
111
+ }
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "execution_count": null,
116
+ "outputs": [],
117
+ "source": [
118
+ "tokens"
119
+ ],
120
+ "metadata": {
121
+ "collapsed": false,
122
+ "pycharm": {
123
+ "name": "#%%\n"
124
+ }
125
+ }
126
+ },
127
+ {
128
+ "cell_type": "code",
129
+ "execution_count": null,
130
+ "outputs": [],
131
+ "source": [
132
+ "import spacy\n",
133
+ "from spacy import displacy"
134
+ ],
135
+ "metadata": {
136
+ "collapsed": false,
137
+ "pycharm": {
138
+ "name": "#%%\n"
139
+ }
140
+ }
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "execution_count": null,
145
+ "outputs": [],
146
+ "source": [
147
+ "!python -m spacy download en_core_web_sm"
148
+ ],
149
+ "metadata": {
150
+ "collapsed": false,
151
+ "pycharm": {
152
+ "name": "#%%\n"
153
+ }
154
+ }
155
+ },
156
+ {
157
+ "cell_type": "code",
158
+ "execution_count": null,
159
+ "outputs": [],
160
+ "source": [
161
+ "\n",
162
+ "text = \"When Sebastian Thrun started working on self-driving cars at Google in 2007, few people outside of the company took him seriously.\"\n",
163
+ "\n",
164
+ "nlp = spacy.load(\"en_core_web_sm\")\n",
165
+ "doc = nlp(text)\n",
166
+ "displacy.serve(doc, style=\"ent\")"
167
+ ],
168
+ "metadata": {
169
+ "collapsed": false,
170
+ "pycharm": {
171
+ "name": "#%%\n"
172
+ }
173
+ }
174
  }
175
  ],
176
  "metadata": {