Spaces:
Sleeping
Sleeping
File size: 3,561 Bytes
6f4a27c bf035c8 84710d4 bf035c8 6f4a27c ac436d4 bf035c8 6f4a27c 8024d68 6f4a27c 1dee0bd 6f4a27c 8024d68 6f4a27c bf035c8 |
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
"""
import os
import gradio as gr
from transformers import T5ForConditionalGeneration, T5Tokenizer
#question_model = T5ForConditionalGeneration.from_pretrained('ramsrigouthamg/t5_squad_v1')
#question_tokenizer = T5Tokenizer.from_pretrained('t5-base')
question_model = T5ForConditionalGeneration.from_pretrained('ramsrigouthamg/t5_squad_v1')
question_tokenizer = T5Tokenizer.from_pretrained('t5-small')
def get_question(context, answer):
text = "context: {} answer: {}".format(context, answer)
#max_len = 512
#encoding = question_tokenizer.encode_plus(text, max_length=max_len, padding='max_length', truncation=True, return_tensors="pt")
encoding = question_tokenizer.encode_plus(text, return_tensors="pt")
input_ids, attention_mask = encoding["input_ids"], encoding["attention_mask"]
outs = question_model.generate(input_ids=input_ids,
attention_mask=attention_mask,
early_stopping=True,
num_beams=3, # Use fewer beams to generate fewer but higher-quality questions
num_return_sequences=3,
no_repeat_ngram_size=3, # Allow some repetition to avoid generating nonsensical questions
max_length=256) # Use a shorter max length to focus on generating more relevant questions
dec = [question_tokenizer.decode(ids) for ids in outs]
questions = ""
for i, question in enumerate(dec):
question = question.replace("question:", "").replace("<pad>", "").replace("</s>", "")
question = question.strip()
questions += question
if i != len(dec)-1:
questions += "§"
return questions
input_context = gr.Textbox()
input_answer = gr.Textbox()
output_question = gr.Textbox()
interface = gr.Interface(
fn=get_question,
inputs=[input_context, input_answer],
outputs=output_question
)
interface.launch()
"""
import gradio as gr
from transformers import T5ForConditionalGeneration,T5Tokenizer
question_model = T5ForConditionalGeneration.from_pretrained('ramsrigouthamg/t5_squad_v1')
question_tokenizer = T5Tokenizer.from_pretrained('t5-base')
def get_question(sentence,answer):
text = "context: {} answer: {} </s>".format(sentence,answer)
print (text)
max_len = 256
encoding = question_tokenizer.encode_plus(text,max_length=max_len, pad_to_max_length=True, return_tensors="pt")
input_ids, attention_mask = encoding["input_ids"], encoding["attention_mask"]
outs = question_model.generate(input_ids=input_ids,
attention_mask=attention_mask,
early_stopping=True,
num_beams=3,
num_return_sequences=3,
no_repeat_ngram_size=2,
max_length=200)
dec = [question_tokenizer.decode(ids) for ids in outs]
questions = ""
for i, question in enumerate(dec):
question = question.replace("question:", "").replace("<pad>", "").replace("</s>", "")
question = question.strip()
questions += question
if i != len(dec)-1:
questions += "§"
return questions
input_context = gr.Textbox()
input_answer = gr.Textbox()
output_question = gr.Textbox()
interface = gr.Interface(
fn=get_question,
inputs=[input_context, input_answer],
outputs=output_question
)
interface.launch() |