Wootang01's picture
Update app.py
64a1aa6
raw
history blame
3.59 kB
import transformers
import sentencepiece
import torch
import numpy as np
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):
prompt = "context: {} answer: {}".format(sentence,answer)
print (prompt)
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
Text = "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
title = "Question Generator Three"
description = "Paste or write a text. You may also paste or write a short answer, preferably a noun or noun phrase. Submit and the machine will attempt to generate a coherent question."
Text = 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")
examples = [
["""Fears of a new Covid-19 cluster linked to a hotpot restaurant have surfaced amid Hong Kong’s Omicron-fuelled fifth wave, while infections tied to an investment bank continued to expand, triggering the evacuation of residents in a building after vertical transmission of the virus was detected.
On Wednesday, hundreds thronged Covid-19 testing stations in Tuen Mun, with some residents complaining of long waiting times and chaotic arrangements. Authorities have deemed the district a high-risk area because of a higher number of infections.
Health officials said sewage testing would be conducted in Tuen Mun to monitor the spread of the coronavirus, but a string of preliminary-positive cases detected across the city suggested a wider, more worrying situation.
""", "a higher number of infections"],
["""Squid Game made history on Wednesday as the first non-English-language television series and the first Korean series to score a nomination for a Screen Actors Guild Award.
The hit Netflix show, created by Hwang Dong-hyuk, is nominated for ensemble in a drama series alongside The Handmaid’s Tale, The Morning Show, Succession and Yellowstone.
Squid Game stars Lee Jung-jae and Jung Ho-yeon also landed individual nominations for actor and actress in a drama series, respectively.
""", "Yellowstone"]
]
def generate_question(Text,Answer):
return get_question(Text,Answer,question_model,question_tokenizer)
iface = gr.Interface(
fn=generate_question,
inputs=[Text,Answer],
outputs=question, title=title, description=description, examples=examples)
iface.launch(debug=False)