Pranav0gp commited on
Commit
39dfaa9
1 Parent(s): 00a11ed

A names entity Recognition using gradio.

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ API_URL = "https://api-inference.huggingface.co/models/dslim/bert-base-NER"
2
+
3
+ # Helper function
4
+ import requests, json
5
+
6
+ #Summarization endpoint
7
+ def get_completion(inputs,ENDPOINT_URL, parameters=None):
8
+ hf_api_key = "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"
9
+ headers = {
10
+ "Content-Type": "application/json"
11
+ }
12
+ data = { "inputs": inputs }
13
+ if parameters is not None:
14
+ data.update({"parameters": parameters})
15
+ response = requests.request("POST",
16
+ ENDPOINT_URL, headers=headers,
17
+ data=json.dumps(data)
18
+ )
19
+ return json.loads(response.content.decode("utf-8"))
20
+
21
+
22
+ import gradio as gr
23
+ def merge_tokens(tokens):
24
+ merged_tokens = []
25
+ for token in tokens:
26
+ if merged_tokens and token['entity_group'].startswith('I-') and merged_tokens[-1]['entity_group'].endswith(token['entity'][2:]):
27
+ # If current token continues the entity of the last one, merge them
28
+ last_token = merged_tokens[-1]
29
+ last_token['word'] += token['word'].replace('##', '')
30
+ last_token['end'] = token['end']
31
+ last_token['score'] = (last_token['score'] + token['score']) / 2
32
+ else:
33
+ # Otherwise, add the token to the list
34
+ merged_tokens.append(token)
35
+
36
+ return merged_tokens
37
+
38
+ def ner(input):
39
+ output = get_completion(input, parameters=None, ENDPOINT_URL=API_URL)
40
+ merged_tokens = merge_tokens(output)
41
+ return {"text": input, "entities": merged_tokens}
42
+
43
+ gr.close_all()
44
+ demo = gr.Interface(fn=ner,
45
+ inputs=[gr.Textbox(label="Text to find entities", lines=2)],
46
+ outputs=[gr.HighlightedText(label="Text with entities")],
47
+ title="NER with dslim/bert-base-NER",
48
+ description="Find entities using the `dslim/bert-base-NER` model under the hood!",
49
+ allow_flagging="never",
50
+ examples=["My name is Andrew, I'm building DeeplearningAI and I live in California", "My name is Poli, I live in Vienna and work at HuggingFace"])
51
+
52
+ demo.launch(inline= False)