Spaces:
Runtime error
Runtime error
Weixin-Liang
commited on
Commit
·
f4f893f
1
Parent(s):
88734c0
update inference APP
Browse files
app.py
CHANGED
|
@@ -14,7 +14,15 @@ label_dict_relations={ i : l for i, l in enumerate(label_list) }
|
|
| 14 |
PATH = "./saved-models/my_model"
|
| 15 |
model_metaphor_detection = AutoModelForTokenClassification.from_pretrained(PATH, id2label=label_dict_relations)
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
examples = [
|
| 20 |
"It would change the trajectory of your legal career.",
|
|
@@ -22,16 +30,115 @@ examples = [
|
|
| 22 |
"Those statements are deeply concerning.",
|
| 23 |
]
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def ner(text):
|
| 26 |
output = pipeline_metaphors(text)
|
| 27 |
-
# change name
|
| 28 |
for x in output:
|
| 29 |
-
|
|
|
|
| 30 |
return {"text": text, "entities": output}
|
| 31 |
|
|
|
|
|
|
|
| 32 |
demo = gr.Interface(ner,
|
| 33 |
gr.Textbox(placeholder="Enter sentence here..."),
|
| 34 |
gr.HighlightedText(),
|
| 35 |
examples=examples)
|
| 36 |
|
| 37 |
-
demo.launch(
|
|
|
|
| 14 |
PATH = "./saved-models/my_model"
|
| 15 |
model_metaphor_detection = AutoModelForTokenClassification.from_pretrained(PATH, id2label=label_dict_relations)
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 17 |
+
|
| 18 |
+
pipeline_metaphors=pipeline(
|
| 19 |
+
"ner",
|
| 20 |
+
model=model_metaphor_detection,
|
| 21 |
+
tokenizer=tokenizer,
|
| 22 |
+
aggregation_strategy="none",
|
| 23 |
+
# aggregation_strategy="simple",
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
|
| 27 |
examples = [
|
| 28 |
"It would change the trajectory of your legal career.",
|
|
|
|
| 30 |
"Those statements are deeply concerning.",
|
| 31 |
]
|
| 32 |
|
| 33 |
+
# Demo usage
|
| 34 |
+
import pprint
|
| 35 |
+
detection_results = pipeline_metaphors("It would change the trajectory of your legal career.")
|
| 36 |
+
pp = pprint.PrettyPrinter(indent=4)
|
| 37 |
+
pp.pprint(detection_results)
|
| 38 |
+
|
| 39 |
+
"""Example Output; aggregation_strategy="none"
|
| 40 |
+
[ { 'end': 2,
|
| 41 |
+
'entity': 'literal',
|
| 42 |
+
'index': 1,
|
| 43 |
+
'score': 0.99981445,
|
| 44 |
+
'start': 0,
|
| 45 |
+
'word': '▁It'},
|
| 46 |
+
{ 'end': 8,
|
| 47 |
+
'entity': 'literal',
|
| 48 |
+
'index': 2,
|
| 49 |
+
'score': 0.9999882,
|
| 50 |
+
'start': 3,
|
| 51 |
+
'word': '▁would'},
|
| 52 |
+
{ 'end': 15,
|
| 53 |
+
'entity': 'literal',
|
| 54 |
+
'index': 3,
|
| 55 |
+
'score': 0.6243065,
|
| 56 |
+
'start': 9,
|
| 57 |
+
'word': '▁change'},
|
| 58 |
+
{ 'end': 19,
|
| 59 |
+
'entity': 'literal',
|
| 60 |
+
'index': 4,
|
| 61 |
+
'score': 0.9999826,
|
| 62 |
+
'start': 16,
|
| 63 |
+
'word': '▁the'},
|
| 64 |
+
{ 'end': 27,
|
| 65 |
+
'entity': 'metaphoric',
|
| 66 |
+
'index': 5,
|
| 67 |
+
'score': 0.99631363,
|
| 68 |
+
'start': 20,
|
| 69 |
+
'word': '▁traject'},
|
| 70 |
+
{ 'end': 30,
|
| 71 |
+
'entity': 'metaphoric',
|
| 72 |
+
'index': 6,
|
| 73 |
+
'score': 0.9979997,
|
| 74 |
+
'start': 27,
|
| 75 |
+
'word': 'ory'},
|
| 76 |
+
{ 'end': 33,
|
| 77 |
+
'entity': 'literal',
|
| 78 |
+
'index': 7,
|
| 79 |
+
'score': 0.9996278,
|
| 80 |
+
'start': 31,
|
| 81 |
+
'word': '▁of'},
|
| 82 |
+
{ 'end': 38,
|
| 83 |
+
'entity': 'literal',
|
| 84 |
+
'index': 8,
|
| 85 |
+
'score': 0.99985147,
|
| 86 |
+
'start': 34,
|
| 87 |
+
'word': '▁your'},
|
| 88 |
+
{ 'end': 44,
|
| 89 |
+
'entity': 'literal',
|
| 90 |
+
'index': 9,
|
| 91 |
+
'score': 0.99984956,
|
| 92 |
+
'start': 39,
|
| 93 |
+
'word': '▁legal'},
|
| 94 |
+
{ 'end': 51,
|
| 95 |
+
'entity': 'literal',
|
| 96 |
+
'index': 10,
|
| 97 |
+
'score': 0.998919,
|
| 98 |
+
'start': 45,
|
| 99 |
+
'word': '▁career'},
|
| 100 |
+
{ 'end': 52,
|
| 101 |
+
'entity': 'literal',
|
| 102 |
+
'index': 11,
|
| 103 |
+
'score': 0.99775606,
|
| 104 |
+
'start': 51,
|
| 105 |
+
'word': '.'}]
|
| 106 |
+
"""
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
"""Example Output; aggregation_strategy="simple"
|
| 110 |
+
[ { 'end': 19,
|
| 111 |
+
'entity_group': 'literal',
|
| 112 |
+
'score': 0.9060229,
|
| 113 |
+
'start': 0,
|
| 114 |
+
'word': 'It would change the'},
|
| 115 |
+
{ 'end': 30,
|
| 116 |
+
'entity_group': 'metaphoric',
|
| 117 |
+
'score': 0.9971567,
|
| 118 |
+
'start': 20,
|
| 119 |
+
'word': 'trajectory'},
|
| 120 |
+
{ 'end': 52,
|
| 121 |
+
'entity_group': 'literal',
|
| 122 |
+
'score': 0.9992008,
|
| 123 |
+
'start': 31,
|
| 124 |
+
'word': 'of your legal career.'}]
|
| 125 |
+
|
| 126 |
+
"""
|
| 127 |
+
# exit(0)
|
| 128 |
+
|
| 129 |
def ner(text):
|
| 130 |
output = pipeline_metaphors(text)
|
| 131 |
+
# # change name
|
| 132 |
for x in output:
|
| 133 |
+
if 'entity_group' in x:
|
| 134 |
+
x['entity'] = x['entity_group']
|
| 135 |
return {"text": text, "entities": output}
|
| 136 |
|
| 137 |
+
|
| 138 |
+
|
| 139 |
demo = gr.Interface(ner,
|
| 140 |
gr.Textbox(placeholder="Enter sentence here..."),
|
| 141 |
gr.HighlightedText(),
|
| 142 |
examples=examples)
|
| 143 |
|
| 144 |
+
demo.launch()
|