Spaces:
Runtime error
Runtime error
initial commit
Browse files- app.py +75 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
model_checkpoint = 'zinoubm/bert-finetuned-ner'
|
5 |
+
model = pipeline(
|
6 |
+
"token-classification", model=model_checkpoint,
|
7 |
+
)
|
8 |
+
|
9 |
+
|
10 |
+
def concat_prediction(prediction):
|
11 |
+
entity = prediction[0]['entity'][2:]
|
12 |
+
start = prediction[0]['start']
|
13 |
+
end = prediction[-1]['end']
|
14 |
+
return {
|
15 |
+
'entity': entity,
|
16 |
+
'start': start,
|
17 |
+
'end': end}
|
18 |
+
|
19 |
+
|
20 |
+
def concat_predictions(predictions):
|
21 |
+
concatenated_predictions = []
|
22 |
+
for_concat = []
|
23 |
+
for i in range(len(predictions)):
|
24 |
+
if predictions[i]['entity'].startswith('B'):
|
25 |
+
for_concat.append(predictions[i])
|
26 |
+
j = i+1
|
27 |
+
while j < len(predictions) and predictions[j]['entity'].startswith('I'):
|
28 |
+
for_concat.append(predictions[j])
|
29 |
+
j += 1
|
30 |
+
|
31 |
+
concatenated_predictions.append(concat_prediction(for_concat))
|
32 |
+
for_concat = []
|
33 |
+
|
34 |
+
return concatenated_predictions
|
35 |
+
|
36 |
+
|
37 |
+
mport gradio as gr
|
38 |
+
|
39 |
+
title = 'Extended Name Entity Recognition'
|
40 |
+
|
41 |
+
examples = [
|
42 |
+
"Does Chicago have any stores and does Joe live here?",
|
43 |
+
"My name is Sylvain and I work at Hugging Face in Brooklyn."
|
44 |
+
]
|
45 |
+
|
46 |
+
article = '''
|
47 |
+
# How to use this interface
|
48 |
+
Using the interface is very easy, just type some text that and the model will give the names of entities in one of these categories:
|
49 |
+
|
50 |
+
- **org** : organization
|
51 |
+
- **per** : person
|
52 |
+
- **geo** : location
|
53 |
+
- **tim** : dates and times
|
54 |
+
- **gpe** : Geopolitical Entity
|
55 |
+
- **art**
|
56 |
+
- **nat**
|
57 |
+
- **eve**
|
58 |
+
|
59 |
+
just hit **Submit** to see the results.You can also try some of the provided examples.
|
60 |
+
'''
|
61 |
+
|
62 |
+
|
63 |
+
def predict(text):
|
64 |
+
output = model(text)
|
65 |
+
return {"text": text, "entities": concat_predictions(output)}
|
66 |
+
|
67 |
+
|
68 |
+
demo = gr.Interface(predict,
|
69 |
+
gr.Textbox(placeholder="Enter sentence here..."),
|
70 |
+
gr.HighlightedText(),
|
71 |
+
title=title,
|
72 |
+
examples=examples,
|
73 |
+
article=article)
|
74 |
+
|
75 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|