mwitiderrick commited on
Commit
edeb660
·
1 Parent(s): f7b3a5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -51
app.py CHANGED
@@ -4,14 +4,25 @@ import gradio as gr
4
 
5
  markdownn = '''
6
  # Named Entity Recognition Pipeline with DeepSparse
 
 
 
 
7
  DeepSparse is sparsity-aware inference runtime offering GPU-class performance on CPUs and APIs to integrate ML into your application. DeepSparse provides sparsified pipelines for computer vision and NLP.
8
- The pipelines are similar to Hugging Face pipelines but are faster because they have been pruned and quantized. SparseML Named Entity Recognition Pipelines integrate with Hugging Face’s Transformers library to enable the sparsification of a large set of transformers models. Here is sample code for a token classification pipeline:
 
 
9
  ```python
10
  from deepsparse import Pipeline
11
  pipeline = Pipeline.create(task="ner", model_path="zoo:nlp/token_classification/distilbert-none/pytorch/huggingface/conll2003/pruned80_quant-none-vnni")
12
  inference = pipeline(text)
13
  print(inference)
14
  ```
 
 
 
 
 
15
  '''
16
  task = "ner"
17
  dense_qa_pipeline = Pipeline.create(
@@ -23,36 +34,39 @@ sparse_qa_pipeline = Pipeline.create(
23
  task=task,
24
  model_path="zoo:nlp/token_classification/bert-base/pytorch/huggingface/conll2003/12layer_pruned80_quant-none-vnni",
25
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def run_pipeline(text):
27
  dense_start = time.perf_counter()
28
- dense_entities = []
29
- sparse_entities = []
30
 
31
  dense_output = dense_qa_pipeline(text)
32
- for item in dict(dense_output)['predictions'][0]:
33
- dictionary = dict(item)
34
- entity = dictionary['entity']
35
-
36
- if entity == "LABEL_0":
37
- value = "O"
38
- elif entity == "LABEL_1":
39
- value = "B-PER"
40
- elif entity == "LABEL_2":
41
- value = "I-PER"
42
- elif entity == "LABEL_3":
43
- value = "-ORG"
44
- elif entity == "LABEL_4":
45
- value = "I-ORG"
46
- elif entity == "LABEL_5":
47
- value = "B-LOC"
48
- elif entity == "LABEL_6":
49
- value = "I-LOC"
50
- elif entity == "LABEL_7":
51
- value = "B-MISC"
52
- else:
53
- value = "I-MISC"
54
- dictionary['entity'] = value
55
- dense_entities.append(dictionary)
56
 
57
  dense_output = {"text": text, "entities": dense_entities}
58
 
@@ -62,30 +76,7 @@ def run_pipeline(text):
62
  sparse_start = time.perf_counter()
63
 
64
  sparse_output = sparse_qa_pipeline(text)
65
- for item in dict(sparse_output)['predictions'][0]:
66
- sparse_dictionary = dict(item)
67
- entity = sparse_dictionary['entity']
68
-
69
- if entity == "LABEL_0":
70
- value = "O"
71
- elif entity == "LABEL_1":
72
- value = "B-PER"
73
- elif entity == "LABEL_2":
74
- value = "I-PER"
75
- elif entity == "LABEL_3":
76
- value = "-ORG"
77
- elif entity == "LABEL_4":
78
- value = "I-ORG"
79
- elif entity == "LABEL_5":
80
- value = "B-LOC"
81
- elif entity == "LABEL_6":
82
- value = "I-LOC"
83
- elif entity == "LABEL_7":
84
- value = "B-MISC"
85
- else:
86
- value = "I-MISC"
87
- sparse_dictionary['entity'] = value
88
- sparse_entities.append(sparse_dictionary)
89
 
90
  sparse_output = {"text": text, "entities": sparse_entities}
91
 
 
4
 
5
  markdownn = '''
6
  # Named Entity Recognition Pipeline with DeepSparse
7
+ Named Entity Recognition is the task of extracting and locating named entities in a sentence. The entities include, people's names, location, organizations, etc.
8
+ ![Named Entity Recognition Pipeline with DeepSparse](https://huggingface.co/spaces/neuralmagic/nlp-ner/resolve/main/ner.png)
9
+
10
+ ## What is DeepSparse?
11
  DeepSparse is sparsity-aware inference runtime offering GPU-class performance on CPUs and APIs to integrate ML into your application. DeepSparse provides sparsified pipelines for computer vision and NLP.
12
+ The pipelines are similar to Hugging Face pipelines but are faster because they have been pruned and quantized. SparseML Named Entity Recognition Pipelines integrate with Hugging Face’s Transformers library to enable the sparsification of a large set of transformers models.
13
+ ### Inference
14
+ Here is sample code for a token classification pipeline:
15
  ```python
16
  from deepsparse import Pipeline
17
  pipeline = Pipeline.create(task="ner", model_path="zoo:nlp/token_classification/distilbert-none/pytorch/huggingface/conll2003/pruned80_quant-none-vnni")
18
  inference = pipeline(text)
19
  print(inference)
20
  ```
21
+ ## Use case example
22
+ The Named Entity Recognition Pipeline can process text before storing the information in a database.
23
+
24
+ For example, you may want to process texta and store the entities in different columns depending on the entity type.
25
+
26
  '''
27
  task = "ner"
28
  dense_qa_pipeline = Pipeline.create(
 
34
  task=task,
35
  model_path="zoo:nlp/token_classification/bert-base/pytorch/huggingface/conll2003/12layer_pruned80_quant-none-vnni",
36
  )
37
+
38
+ def map_ner(inference):
39
+ entities = []
40
+ for item in dict(inference)['predictions'][0]:
41
+ dictionary = dict(item)
42
+ entity = dictionary['entity']
43
+ if entity == "LABEL_0":
44
+ value = "O"
45
+ elif entity == "LABEL_1":
46
+ value = "B-PER"
47
+ elif entity == "LABEL_2":
48
+ value = "I-PER"
49
+ elif entity == "LABEL_3":
50
+ value = "-ORG"
51
+ elif entity == "LABEL_4":
52
+ value = "I-ORG"
53
+ elif entity == "LABEL_5":
54
+ value = "B-LOC"
55
+ elif entity == "LABEL_6":
56
+ value = "I-LOC"
57
+ elif entity == "LABEL_7":
58
+ value = "B-MISC"
59
+ else:
60
+ value = "I-MISC"
61
+ dictionary['entity'] = value
62
+ entities.append(dictionary)
63
+ return entities
64
+
65
  def run_pipeline(text):
66
  dense_start = time.perf_counter()
 
 
67
 
68
  dense_output = dense_qa_pipeline(text)
69
+ dense_entities = map_ner(dense_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  dense_output = {"text": text, "entities": dense_entities}
72
 
 
76
  sparse_start = time.perf_counter()
77
 
78
  sparse_output = sparse_qa_pipeline(text)
79
+ sparse_entities = map_ner(sparse_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  sparse_output = {"text": text, "entities": sparse_entities}
82