Wootang01's picture
Create app.py
d4b5af9
raw
history blame
1.89 kB
import transformers
import sentencepiece
import ipython-autotime
from transformers import T5ForConditionalGeneration,T5Tokenizer
question_model = T5ForConditionalGeneration.from_pretrained('ramsrigouthamg/t5_squad_v1')
question_tokenizer = T5Tokenizer.from_pretrained('ramsrigouthamg/t5_squad_v1')
def get_question(sentence,answer,mdl,tknizer):
text = "context: {} answer: {}".format(sentence,answer)
print (text)
max_len = 256
encoding = tknizer.encode_plus(text,max_length=max_len, pad_to_max_length=False,truncation=True, return_tensors="pt")
input_ids, attention_mask = encoding["input_ids"], encoding["attention_mask"]
outs = mdl.generate(input_ids=input_ids,
attention_mask=attention_mask,
early_stopping=True,
num_beams=5,
num_return_sequences=1,
no_repeat_ngram_size=2,
max_length=300)
dec = [tknizer.decode(ids,skip_special_tokens=True) for ids in outs]
Question = dec[0].replace("question:","")
Question= Question.strip()
return Question
context = "Elon Musk said that Tesla will not accept payments in Bitcoin because of environmental concerns."
answer = "Elon Musk"
ques = get_question(context,answer,question_model,question_tokenizer)
print ("question: ",ques)
import gradio as gr
context = gr.inputs.Textbox(lines=5, placeholder="Enter paragraph/context here...")
answer = gr.inputs.Textbox(lines=3, placeholder="Enter answer/keyword here...")
question = gr.outputs.Textbox( type="auto", label="Question")
def generate_question(context,answer):
return get_question(context,answer,question_model,question_tokenizer)
iface = gr.Interface(
fn=generate_question,
inputs=[context,answer],
outputs=question)
iface.launch(debug=False)