quincyqiang christophalt commited on
Commit
3b667c7
0 Parent(s):

Duplicate from pie/Joint-NER-and-Relation-Extraction

Browse files

Co-authored-by: Christoph Alt <[email protected]>

Files changed (4) hide show
  1. .gitattributes +27 -0
  2. README.md +14 -0
  3. app.py +61 -0
  4. requirements.txt +2 -0
.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.onnx filter=lfs diff=lfs merge=lfs -text
13
+ *.ot filter=lfs diff=lfs merge=lfs -text
14
+ *.parquet filter=lfs diff=lfs merge=lfs -text
15
+ *.pb filter=lfs diff=lfs merge=lfs -text
16
+ *.pt filter=lfs diff=lfs merge=lfs -text
17
+ *.pth filter=lfs diff=lfs merge=lfs -text
18
+ *.rar filter=lfs diff=lfs merge=lfs -text
19
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
20
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
21
+ *.tflite filter=lfs diff=lfs merge=lfs -text
22
+ *.tgz filter=lfs diff=lfs merge=lfs -text
23
+ *.wasm filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Joint NER And Relation Extraction
3
+ emoji: 🐠
4
+ colorFrom: yellow
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 2.9.4
8
+ app_file: app.py
9
+ pinned: false
10
+ python_version: 3.9.12
11
+ duplicated_from: pie/Joint-NER-and-Relation-Extraction
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import gradio as gr
3
+ from dataclasses import dataclass
4
+ from prettytable import PrettyTable
5
+
6
+ from pytorch_ie.annotations import LabeledSpan, BinaryRelation
7
+ from pytorch_ie.auto import AutoPipeline
8
+ from pytorch_ie.core import AnnotationList, annotation_field
9
+ from pytorch_ie.documents import TextDocument
10
+
11
+ from typing import List
12
+
13
+
14
+ @dataclass
15
+ class ExampleDocument(TextDocument):
16
+ entities: AnnotationList[LabeledSpan] = annotation_field(target="text")
17
+ relations: AnnotationList[BinaryRelation] = annotation_field(target="entities")
18
+
19
+
20
+ ner_model_name_or_path = "pie/example-ner-spanclf-conll03"
21
+ re_model_name_or_path = "pie/example-re-textclf-tacred"
22
+
23
+ ner_pipeline = AutoPipeline.from_pretrained(ner_model_name_or_path, device=-1, num_workers=0)
24
+ re_pipeline = AutoPipeline.from_pretrained(re_model_name_or_path, device=-1, num_workers=0)
25
+
26
+
27
+ def predict(text):
28
+ document = ExampleDocument(text)
29
+
30
+ ner_pipeline(document)
31
+
32
+ while len(document.entities.predictions) > 0:
33
+ document.entities.append(document.entities.predictions.pop(0))
34
+
35
+ re_pipeline(document)
36
+
37
+ t = PrettyTable()
38
+ t.field_names = ["head", "tail", "relation"]
39
+ t.align = "l"
40
+ for relation in document.relations.predictions:
41
+ t.add_row([str(relation.head), str(relation.tail), relation.label])
42
+
43
+ html = t.get_html_string(format=True)
44
+ html = (
45
+ "<div style='max-width:100%; max-height:360px; overflow:auto'>"
46
+ + html
47
+ + "</div>"
48
+ )
49
+
50
+ return html
51
+
52
+
53
+ iface = gr.Interface(
54
+ fn=predict,
55
+ inputs=gr.inputs.Textbox(
56
+ lines=5,
57
+ default="There is still some uncertainty that Musk - also chief executive of electric car maker Tesla and rocket company SpaceX - will pull off his planned buyout.",
58
+ ),
59
+ outputs="html",
60
+ )
61
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ pytorch-ie==0.15.0
2
+ prettytable