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("") eos_id = tokenizer._convert_token_to_id_with_added_voc("") pad_id = tokenizer._convert_token_to_id_with_added_voc("") # 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() + " <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()