livinNector commited on
Commit
1e516ec
·
1 Parent(s): 56e9c00

updated app.py with multiple models

Browse files
Files changed (1) hide show
  1. app.py +56 -32
app.py CHANGED
@@ -1,45 +1,69 @@
1
  import gradio as gr
2
  import torch
3
- from transformers import AutoTokenizer, AutoModelForTokenClassification
4
-
5
- tokenizer = AutoTokenizer.from_pretrained("ai4bharat/IndicNER")
6
-
7
- model = AutoModelForTokenClassification.from_pretrained("ai4bharat/IndicNER")
8
-
9
-
10
- def get_ner(sentence):
11
- tok_sentence = tokenizer(sentence, return_tensors='pt')
12
 
 
 
13
  with torch.no_grad():
14
- logits = model(**tok_sentence).logits.argmax(-1)
15
  predicted_tokens_classes = [
16
- model.config.id2label[t.item()] for t in logits[0]]
 
17
 
18
  predicted_labels = []
19
-
20
  previous_token_id = 0
21
- word_ids = tok_sentence.word_ids()
22
  for word_index in range(len(word_ids)):
23
- if word_ids[word_index] == None:
24
- previous_token_id = word_ids[word_index]
25
- elif word_ids[word_index] == previous_token_id:
26
- previous_token_id = word_ids[word_index]
27
- else:
28
- predicted_labels.append(predicted_tokens_classes[word_index])
29
- previous_token_id = word_ids[word_index]
30
-
31
- ner_output = []
32
- for index in range(len(sentence.split(' '))):
33
- ner_output.append(
34
- (sentence.split(' ')[index], predicted_labels[index]))
35
  return ner_output
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- iface = gr.Interface(get_ner,
39
- gr.Textbox(placeholder="Enter sentence here..."),
40
- ["highlight"], description='NER Specialized for Tamil Language.',
41
- examples=["முதல்வர் ஸ்டாலின் பட்டமளிப்பு விழாவிற்காக சிதம்பரத்திலுள்ள அண்ணாமலைப் பல்கலைகழகத்திற்கு வருகை தந்தார்.","வல்லவராயன் வந்தியதேவனும் ஆதித்திய கரிகாலனும் கடம்பூருக்குச் சென்றனர். "], title='TaNER',
42
- article='TaNER is a model developed for NER in Tamil Language'
43
- )
 
 
 
 
 
 
44
 
45
- iface.launch(enable_queue=True)
 
1
  import gradio as gr
2
  import torch
3
+ from transformers import AutoTokenizer, AutoModelForTokenClassification,pipeline
 
 
 
 
 
 
 
 
4
 
5
+ def get_ner_bio(pipe,text):
6
+ tok_text = pipe.tokenizer(text, return_tensors='pt')
7
  with torch.no_grad():
8
+ logits = pipe.model(**tok_text).logits.argmax(-1)
9
  predicted_tokens_classes = [
10
+ pipe.model.config.id2label[t.item()] for t in logits[0]
11
+ ]
12
 
13
  predicted_labels = []
 
14
  previous_token_id = 0
15
+ word_ids = tok_text.word_ids()
16
  for word_index in range(len(word_ids)):
17
+ if not (word_ids[word_index] == None or word_ids[word_index] == previous_token_id):
18
+ predicted_labels.append(predicted_tokens_classes[word_index])
19
+ previous_token_id = word_ids[word_index]
20
+
21
+ ner_output = [
22
+ (word, label if label!="O" else None)
23
+ for word, label in zip(text.split(" "),predicted_labels)
24
+ ]
 
 
 
 
25
  return ner_output
26
 
27
+ def get_ner(pipe,text,aggregation_strategy="first"):
28
+
29
+ if aggregation_strategy == "bio_first":
30
+ return get_ner_bio(pipe,text)
31
+ else:
32
+ results = pipe(text,aggregation_strategy=aggregation_strategy)
33
+ for result in results:
34
+ result["entity"] = result["entity_group"]
35
+ return {"text": text, "entities": results}
36
+
37
+ ner_models = [
38
+ "livinNector/TryNER-500",
39
+ "livinNector/TryNER-1k",
40
+ "livinNector/IndicBERTNER",
41
+ "livinNector/IndicNER",
42
+ "ai4bharat/IndicNER",
43
+ "livinNector/distilbert-multilingual-base-ner"
44
+ ]
45
+ ner_pipes = [pipeline("token-classification",model) for model in ner_models]
46
+
47
+ def get_ner_outputs(text,aggregation_strategy):
48
+ return [get_ner(pipe,text,aggregation_strategy) for pipe in ner_pipes]
49
+ examples = [
50
+ ["ஆனந்த் மற்றும் லிவின் நெக்டர் ஆகியொர் அண்ணாமலை பல்கலைக்கழகத்தில் படித்து வருகின்றனர்.","first"],
51
+ ["இந்தியன் இன்ஸ்டிட்யூட் ஆஃப் டெக்னாலஜி மெட்ராஸ் கிண்டியில் அமைந்துள்ளது.","average"],
52
+ ["சச்சின் டெண்டுல்கர் மும்பை மாநகரத்தைச் சேர்ந்த ஒரு நடுத்தரக் குடும்பத்தில் நான்காவது குழந்தையாகப் பிறந்தார். பல துடுப்பாட்ட வீரர்களை உருவாக்கிய சாரதாஷ்ரம் வித்யாமந்திர் பள்ளியில் சேர்ந்தார்.","bio_first"]
53
+
54
+ ]
55
 
56
+ iface = gr.Interface(
57
+ get_ner_outputs,
58
+ [
59
+ gr.Textbox(value=examples[0][0]),
60
+ gr.Dropdown(["bio_first","first","max","average"],value=examples[0][1])
61
+ ],
62
+ [gr.Highlight(label=model) for model in ner_models],
63
+ description='Named Entity Recongnition Interface Comparing Various Transformer Based NER models for Tamil Language.',
64
+ examples=examples,
65
+ title='TaNER',
66
+
67
+ )
68
 
69
+ iface.launch(enable_queue=True)