Spaces:
Runtime error
Runtime error
File size: 1,866 Bytes
d4b5af9 |
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 50 51 52 |
import transformers
import sentencepiece
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) |