Spaces:
Runtime error
Runtime error
File size: 3,589 Bytes
d4b5af9 5f9120d d4b5af9 64a1aa6 d4b5af9 2f7d4d8 d4b5af9 64a1aa6 d4b5af9 9da0b35 d4b5af9 7827790 64a1aa6 d4b5af9 0fab62c 64a1aa6 0fab62c d4b5af9 64a1aa6 d4b5af9 64a1aa6 0fab62c 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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(prompt,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(Text,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) |