Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,69 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
rom transformers import AutoModelForTokenClassification, AutoTokenizer, pipeline
|
2 |
import gradio as gr
|
3 |
|
4 |
+
def ner_tagging(text):
|
5 |
+
model_name = "browndw/docusco-bert"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, add_prefix_space=True)
|
7 |
+
|
8 |
+
model = AutoModelForTokenClassification.from_pretrained(model_name)
|
9 |
+
nlp = pipeline("ner", model=model, tokenizer=tokenizer)
|
10 |
+
|
11 |
+
output = []
|
12 |
+
|
13 |
+
text_2 = text.split(" ")
|
14 |
+
|
15 |
+
for i in range(len(text_2)):
|
16 |
+
ent = ner_results[i]["entity"]
|
17 |
+
if ent != "O":
|
18 |
+
output.extend([(text_2[i], ent), (" ", None)])
|
19 |
+
else:
|
20 |
+
output.extend([(text_2[i], None), (" ", None)])
|
21 |
+
|
22 |
+
return output
|
23 |
+
|
24 |
+
def get_entities(example):
|
25 |
+
model_name = "browndw/docusco-bert"
|
26 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, add_prefix_space=True)
|
27 |
+
|
28 |
+
model = AutoModelForTokenClassification.from_pretrained(model_name)
|
29 |
+
token_classifier = pipeline("token-classification", aggregation_strategy="simple", model=model, tokenizer=tokenizer)
|
30 |
+
|
31 |
+
output = []
|
32 |
+
|
33 |
+
i=0
|
34 |
+
prev_item = None
|
35 |
+
next_item = None
|
36 |
+
while i < (len(results)):
|
37 |
+
item = results[i]
|
38 |
+
p=i-1
|
39 |
+
n=i+1
|
40 |
+
|
41 |
+
if p > 0:
|
42 |
+
prev_item = results[p]
|
43 |
+
|
44 |
+
|
45 |
+
if n<(len(results)):
|
46 |
+
next_item = results[n]
|
47 |
+
|
48 |
+
|
49 |
+
if (i==0):
|
50 |
+
if item["start"]>0:
|
51 |
+
output.extend([(example[0:item["start"]], None)])
|
52 |
+
output.extend([(example[item["start"]:item["end"]], item["entity_group"])])
|
53 |
+
if (next_item!=None):
|
54 |
+
##verificar el tramo entre actual y siguiente
|
55 |
+
if(item["end"]!=next_item["start"]):
|
56 |
+
output.extend([(example[item["end"]:next_item["start"]], None)])
|
57 |
+
i=i+1
|
58 |
+
|
59 |
+
if item["end"] < len(example):
|
60 |
+
output.extend([(example[item["end"]:len(example)], None)])
|
61 |
+
|
62 |
+
return output
|
63 |
+
|
64 |
+
def greet(name):
|
65 |
+
return "Hello " + name + "!!"
|
66 |
+
|
67 |
+
iface = gr.Interface(fn=get_entities, inputs="text", outputs=['highlight'], examples=[['Jaws is a splendidly shrewd cinematic equation which not only gives you one or two very nasty turns when you least expect them but, possibly more important, knows when to make you think another is coming without actually providing it.'],
|
68 |
+
['Jaws is a splendidly shrewd cinematic equation which not only gives you one or two very nasty turns when you least expect them but, possibly more important, knows when to make you think another is coming without actually providing it.']], title="Test of docusco-bert ",)
|
69 |
+
iface.launch()
|