Spaces:
Runtime error
Runtime error
import gradio as gr | |
import torch | |
from transformers import AutoTokenizer, AutoModelForTokenClassification | |
tokenizer = AutoTokenizer.from_pretrained("ai4bharat/IndicNER") | |
model = AutoModelForTokenClassification.from_pretrained("ai4bharat/IndicNER") | |
def get_ner(sentence): | |
tok_sentence = tokenizer(sentence, return_tensors='pt') | |
with torch.no_grad(): | |
logits = model(**tok_sentence).logits.argmax(-1) | |
predicted_tokens_classes = [ | |
model.config.id2label[t.item()] for t in logits[0]] | |
predicted_labels = [] | |
previous_token_id = 0 | |
word_ids = tok_sentence.word_ids() | |
for word_index in range(len(word_ids)): | |
if word_ids[word_index] == None: | |
previous_token_id = word_ids[word_index] | |
elif word_ids[word_index] == previous_token_id: | |
previous_token_id = word_ids[word_index] | |
else: | |
predicted_labels.append(predicted_tokens_classes[word_index]) | |
previous_token_id = word_ids[word_index] | |
ner_output = [] | |
for index in range(len(sentence.split(' '))): | |
ner_output.append( | |
(sentence.split(' ')[index], predicted_labels[index])) | |
return ner_output | |
iface = gr.Interface(get_ner, | |
gr.Textbox(placeholder="Enter sentence here..."), | |
["highlight"], description='NER Specialized for Tamil Language.', | |
examples=["முதல்வர் ஸ்டாலின் பட்டமளிப்பு விழாவிற்காக சிதம்பரத்திலுள்ள அண்ணாமலைப் பல்கலைகழகத்திற்கு வருகை தந்தார்.","வல்லவராயன் வந்தியதேவனும் ஆதித்திய கரிகாலனும் கடம்பூருக்குச் சென்றனர். "], title='TaNER', | |
article='TaNER is a model developed for NER in Tamil Language' | |
) | |
iface.launch(enable_queue=True) | |