Spaces:
Runtime error
Runtime error
ashutosh
commited on
Commit
β’
2ba745b
1
Parent(s):
200d0e9
Name Entity Recognition
Browse files- __pycache__/app.cpython-311.pyc +0 -0
- app.py +40 -0
- env/conda-meta/history +3 -0
- requirements.txt +3 -0
__pycache__/app.cpython-311.pyc
ADDED
Binary file (2.26 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
|
5 |
+
# get_completion = pipeline("summarization")
|
6 |
+
get_completion = pipeline("ner", model="dslim/bert-base-NER")
|
7 |
+
|
8 |
+
|
9 |
+
def merge_tokens(tokens):
|
10 |
+
merged_tokens = []
|
11 |
+
for token in tokens:
|
12 |
+
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
|
13 |
+
# If current token continues the entity of the last one, merge them
|
14 |
+
last_token = merged_tokens[-1]
|
15 |
+
last_token['word'] += token['word'].replace('##', '')
|
16 |
+
last_token['end'] = token['end']
|
17 |
+
last_token['score'] = (last_token['score'] + token['score']) / 2
|
18 |
+
else:
|
19 |
+
# Otherwise, add the token to the list
|
20 |
+
merged_tokens.append(token)
|
21 |
+
|
22 |
+
return merged_tokens
|
23 |
+
|
24 |
+
def ner(input):
|
25 |
+
output = get_completion(input)
|
26 |
+
merged_tokens = merge_tokens(output)
|
27 |
+
return {"text": input, "entities": merged_tokens}
|
28 |
+
|
29 |
+
gr.close_all()
|
30 |
+
demo = gr.Interface(fn=ner,
|
31 |
+
inputs=[gr.Textbox(label="Text to find entities", lines=2)],
|
32 |
+
outputs=[gr.HighlightedText(label="Text with entities")],
|
33 |
+
title="NER with dslim/bert-base-NER",
|
34 |
+
description="Find entities using the `dslim/bert-base-NER` model under the hood!",
|
35 |
+
allow_flagging="never",
|
36 |
+
examples=["My name is Ashutosh. I'm from India and I like building Generative AI applications π§ π€ππ."])
|
37 |
+
|
38 |
+
demo.launch()
|
39 |
+
|
40 |
+
gr.close_all()
|
env/conda-meta/history
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
==> 2023-08-24 08:44:04 <==
|
2 |
+
# cmd: /home/ashutosh/anaconda3/bin/conda create --prefix ./env
|
3 |
+
# conda version: 23.7.2
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformer
|
2 |
+
torch
|
3 |
+
gradio
|