PRAli22 commited on
Commit
375f639
·
1 Parent(s): f2e4143

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoConfig, AutoModelForTokenClassification, AutoTokenizer
2
+ import numpy as np
3
+ import torch
4
+ import gradio as gr
5
+ import requests
6
+
7
+ test_model = AutoModelForTokenClassification.from_pretrained("PRAli22/arabert_arabic_ner")
8
+ TOKENIZER = AutoTokenizer.from_pretrained("PRAli22/arabert_arabic_ner")
9
+
10
+ label_map = {'B-LOC': 0, 'O': 1, 'B-PERS': 2, 'I-PERS': 3, 'B-ORG': 4, 'I-LOC': 5, 'I-ORG': 6, 'B-MISC': 7, 'I-MISC': 8}
11
+ inv_label_map = {0: 'B-LOC', 1: 'O', 2: 'B-PERS', 3: 'I-PERS', 4: 'B-ORG', 5: 'I-LOC', 6: 'I-ORG', 7: 'B-MISC', 8: 'I-MISC'}
12
+
13
+
14
+ def predict_sent(sentences):
15
+ input_ids = TOKENIZER.encode(sentences, return_tensors='pt')
16
+
17
+ with torch.no_grad():
18
+ test_model.to('cpu')
19
+ output = test_model(input_ids)
20
+ label_indices = np.argmax(output[0].to('cpu').numpy(), axis=2)
21
+
22
+ tokens = TOKENIZER.convert_ids_to_tokens(input_ids.to('cpu').numpy()[0])
23
+
24
+ new_tokens, new_labels = [], []
25
+ for token, label_idx in zip(tokens, label_indices[0]):
26
+ if token.startswith("##"):
27
+ new_tokens[-1] = new_tokens[-1] + token[2:]
28
+ else:
29
+ new_labels.append(inv_label_map[label_idx])
30
+ new_tokens.append(token)
31
+
32
+ output_string = "\n".join(["{}\t{}".format(label, token) for token, label in zip(new_tokens, new_labels)])
33
+
34
+ return output_string
35
+
36
+ css_code='body{background-image:url("https://media.istockphoto.com/id/1256252051/vector/people-using-online-translation-app.jpg?s=612x612&w=0&k=20&c=aa6ykHXnSwqKu31fFR6r6Y1bYMS5FMAU9yHqwwylA94=");}'
37
+
38
+ demo = gr.Interface(
39
+ fn=predict_sent,
40
+ inputs=
41
+ gr.Textbox(label="sentence", placeholder=" Enter the sentence "),
42
+
43
+
44
+ outputs=[gr.Textbox(label="labels")],
45
+ title="Arabic Named Entity Recognition",
46
+ description= "This is Arabic Named Entity Recognition System, it takes a sentence in Arabic as inputs and returns it's labels",
47
+ css = css_code
48
+ )
49
+ demo.launch()