File size: 2,577 Bytes
8a9003b
2923a84
8a9003b
 
 
 
 
 
 
 
5f0507e
 
 
 
 
8a9003b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import gradio as gr
import transformers
from transformers import MBartForConditionalGeneration, AutoModelForSeq2SeqLM
from transformers import AlbertTokenizer, AutoTokenizer

tokenizer = AlbertTokenizer.from_pretrained("ai4bharat/MultiIndicWikiBioSS", do_lower_case=False, use_fast=False, keep_accents=True)

# Or use tokenizer = AlbertTokenizer.from_pretrained("ai4bharat/IndicBART-XLSum", do_lower_case=False, use_fast=False, keep_accents=True)

# xlsummodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/IndicBART-XLSum")
qgmodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/MultiIndicQuestionGenerationSS").eval()
hgmodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/MultiIndicHeadlineGenerationSS").eval()
ssmodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/MultiIndicSentenceSummarizationSS").eval()
ppmodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/MultiIndicParaphraseGenerationSS").eval()
wbmodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/MultiIndicWikiBioSS").eval()

# Some initial mapping
bos_id = tokenizer._convert_token_to_id_with_added_voc("<s>")
eos_id = tokenizer._convert_token_to_id_with_added_voc("</s>")
pad_id = tokenizer._convert_token_to_id_with_added_voc("<pad>")
# To get lang_id use any of ['<2bn>', '<2gu>', '<2hi>', '<2mr>', '<2pa>', '<2ta>', '<2te>']



def greet(choice, lang, input):
    if choice == "IndicWikiBio":
        model = wbmodel
    elif choice == "IndicHeadlineGeneration":
        model = hgmodel
    elif choice == "IndicParaprasing": 
        model = ppmodel
    elif choice == "IndicSentenceSummarization":
        model = ssmodel
    elif choice ==  "IndicQuestionGeneration":
        model = qgmodel


    inp = tokenizer(input.strip() + " </s> <2" + lang + ">", add_special_tokens=False, return_tensors="pt", padding=True).input_ids
    model_output=model.generate(inp, use_cache=True, num_beams=1, max_length=100, min_length=1, early_stopping=True, pad_token_id=pad_id, bos_token_id=bos_id, eos_token_id=eos_id, decoder_start_token_id=tokenizer._convert_token_to_id_with_added_voc("<2"+lang+">"))


    # Decode to get output strings

    decoded_output=tokenizer.decode(model_output[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)

    return decoded_output

iface = gr.Interface(fn=greet, inputs=[gr.inputs.Dropdown("IndicWikiBio", "IndicHeadlineGeneration", "IndicParaprasing", "IndicSentenceSummarization", "IndicQuestionGeneration"), gr.inputs.Dropdown("as","bn", "gu", "hi", "kn", "ml", "mr", "or", "pa", "ta", "te"), "text"], outputs="text")
iface.launch()