Spaces:
Runtime error
Runtime error
Logeswaransr
commited on
Commit
•
c44137f
1
Parent(s):
7014262
App File Added
Browse files
app.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from ner import ner
|
3 |
+
|
4 |
+
demo = gr.Interface(fn=ner,
|
5 |
+
inputs=[gr.Textbox(label="Input text", lines=3)],
|
6 |
+
outputs=[gr.HighlightedText(label='Entities')],
|
7 |
+
title='Named Entity Recognition with `dslim/bert-base-ner` ',
|
8 |
+
description='Find entities using the dslim/bert-base-ner model under the hood',
|
9 |
+
allow_flagging='never',
|
10 |
+
examples=["My name is Logeswaran, the creator of this HuggingFace Space. I live in India.","Hello There! I am Andrew Ng, founder of DeepLearning.AI"]
|
11 |
+
)
|
12 |
+
|
13 |
+
|
14 |
+
demo.launch()
|
ner.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
|
5 |
+
def get_completion(inputs, parameters=None):
|
6 |
+
headers = {
|
7 |
+
"Authorization": f"Bearer {model_api}",
|
8 |
+
"Content-Type": "application/json"
|
9 |
+
}
|
10 |
+
data = { "inputs": inputs }
|
11 |
+
if parameters is not None:
|
12 |
+
data.update({"parameters": parameters})
|
13 |
+
response = requests.request("POST",
|
14 |
+
model_url, headers=headers,
|
15 |
+
data=json.dumps(data)
|
16 |
+
)
|
17 |
+
return json.loads(response.content.decode("utf-8"))
|
18 |
+
|
19 |
+
def merge_tokens(tokens):
|
20 |
+
merged_tokens=[]
|
21 |
+
for token in tokens:
|
22 |
+
if merged_tokens and merged_tokens[-1]['entity_group'] == token['entity_group']:
|
23 |
+
last_token = merged_tokens[-1]
|
24 |
+
last_token['word'] += token['word'].replace('##', '')
|
25 |
+
last_token['end'] = token['end']
|
26 |
+
last_token['score'] = (last_token['score'] + token['score']) / 2
|
27 |
+
else:
|
28 |
+
merged_tokens.append(token)
|
29 |
+
|
30 |
+
return merged_tokens
|
31 |
+
|
32 |
+
def ner(input):
|
33 |
+
output = get_completion(input)
|
34 |
+
output = merge_tokens(output)
|
35 |
+
return {"text": input, "entities": output}
|
36 |
+
|
37 |
+
content = json.load(open('hf_api.json'))
|
38 |
+
ner_api = content['named_entity_recognition']
|
39 |
+
model_url = ner_api['API_URL']
|
40 |
+
model_api = ner_api['API_Key']
|