leonhardhennig commited on
Commit
83ad59f
·
1 Parent(s): fddd175

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from dataclasses import dataclass
3
+
4
+ from pytorch_ie.annotations import LabeledSpan
5
+ from pytorch_ie.auto import AutoPipeline
6
+ from pytorch_ie.core import AnnotationList, annotation_field
7
+ from pytorch_ie.documents import TextDocument
8
+
9
+ from spacy import displacy
10
+
11
+
12
+ @dataclass
13
+ class ExampleDocument(TextDocument):
14
+ entities: AnnotationList[LabeledSpan] = annotation_field(target="text")
15
+
16
+
17
+ model_name_or_path = "leonhardhennig/copious-ner"
18
+
19
+ ner_pipeline = AutoPipeline.from_pretrained(model_name_or_path, device=-1, num_workers=0)
20
+
21
+
22
+ def predict(text):
23
+ document = ExampleDocument(text)
24
+
25
+ ner_pipeline(document)
26
+
27
+ doc = {
28
+ "text": document.text,
29
+ "ents": [{
30
+ "start": entity.start,
31
+ "end": entity.end,
32
+ "label": entity.label
33
+ } for entity in sorted(document.entities.predictions, key=lambda e: e.start)],
34
+ "title": None
35
+ }
36
+
37
+ html = displacy.render(doc, style="ent", page=True, manual=True, minify=True)
38
+ html = (
39
+ "<div style='max-width:100%; max-height:360px; overflow:auto'>"
40
+ + html
41
+ + "</div>"
42
+ )
43
+
44
+ return html
45
+
46
+
47
+ iface = gr.Interface(
48
+ fn=predict,
49
+ inputs=gr.inputs.Textbox(
50
+ lines=5,
51
+ default="The most valuable mother-of-pearl is obtained from four species; Pinctada maxima P. margaritifera Trochus niloticus and Turbo marmoratus.",
52
+ ),
53
+ outputs="html",
54
+ )
55
+ iface.launch()