Initial space
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import MBartForConditionalGeneration, AutoModelForSeq2SeqLM
|
3 |
+
from transformers import AlbertTokenizer, AutoTokenizer
|
4 |
+
|
5 |
+
tokenizer = AlbertTokenizer.from_pretrained("ai4bharat/MultiIndicWikiBioSS", do_lower_case=False, use_fast=False, keep_accents=True)
|
6 |
+
|
7 |
+
# Or use tokenizer = AlbertTokenizer.from_pretrained("ai4bharat/IndicBART-XLSum", do_lower_case=False, use_fast=False, keep_accents=True)
|
8 |
+
|
9 |
+
# xlsummodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/IndicBART-XLSum")
|
10 |
+
qgmodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/MultiIndicQuestionGenerationSS")
|
11 |
+
hgmodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/MultiIndicHeadlineGenerationSS")
|
12 |
+
ssmodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/MultiIndicSentenceSummarizationSS")
|
13 |
+
ppmodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/MultiIndicParaphraseGenerationSS")
|
14 |
+
wbmodel = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/MultiIndicWikiBioSS")
|
15 |
+
|
16 |
+
# Some initial mapping
|
17 |
+
bos_id = tokenizer._convert_token_to_id_with_added_voc("<s>")
|
18 |
+
eos_id = tokenizer._convert_token_to_id_with_added_voc("</s>")
|
19 |
+
pad_id = tokenizer._convert_token_to_id_with_added_voc("<pad>")
|
20 |
+
# To get lang_id use any of ['<2bn>', '<2gu>', '<2hi>', '<2mr>', '<2pa>', '<2ta>', '<2te>']
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
def greet(choice, lang, input):
|
25 |
+
if choice == "IndicWikiBio":
|
26 |
+
model = wbmodel
|
27 |
+
elif choice == "IndicHeadlineGeneration":
|
28 |
+
model = hgmodel
|
29 |
+
elif choice == "IndicParaprasing":
|
30 |
+
model = ppmodel
|
31 |
+
elif choice == "IndicSentenceSummarization":
|
32 |
+
model = ssmodel
|
33 |
+
elif choice == "IndicQuestionGeneration":
|
34 |
+
model = qgmodel
|
35 |
+
|
36 |
+
|
37 |
+
inp = tokenizer(input.strip() + " </s> <2" + lang + ">", add_special_tokens=False, return_tensors="pt", padding=True).input_ids
|
38 |
+
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+">"))
|
39 |
+
|
40 |
+
|
41 |
+
# Decode to get output strings
|
42 |
+
|
43 |
+
decoded_output=tokenizer.decode(model_output[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
44 |
+
|
45 |
+
return decoded_output
|
46 |
+
|
47 |
+
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")
|
48 |
+
iface.launch()
|