Spaces:
Runtime error
Runtime error
| from transformers import AutoModelWithLMHead, AutoTokenizer | |
| import gradio as grad | |
| # make a question | |
| # text2text_tkn = AutoTokenizer.from_pretrained('mrm8488/t5-base-finetuned-question-generation-ap') | |
| # mdl = AutoModelWithLMHead.from_pretrained('mrm8488/t5-base-finetuned-question-generation-ap') | |
| # summarize | |
| text2text_tkn = AutoTokenizer.from_pretrained('deep-learning-analytics/wikihow-t5-small') | |
| mdl = AutoModelWithLMHead.from_pretrained('deep-learning-analytics/wikihow-t5-small') | |
| def text2text(context, answer): | |
| input_text = "answer: %s context: %s </s>" % (answer, context) | |
| features = text2text_tkn([input_text], return_tensors = 'pt') | |
| output = mdl.generate( | |
| input_ids = features['input_ids'], | |
| attention_mask = features['attention_mask'], | |
| max_length = 64 | |
| ) | |
| response = text2text_tkn.decode(output[0]) | |
| return response | |
| def text2text_summary(para): | |
| initial_txt = para.strip().replace("\n", "") | |
| tkn_text = text2text_tkn.encode(initial_txt, return_tensors = 'pt') | |
| tkn_ids = mdl.generate( | |
| tkn_text, | |
| max_length = 250, | |
| num_beams = 5, | |
| repetition_penalty = 2.5, | |
| early_stopping = True | |
| ) | |
| response = text2text_tkn.encode(tkn_ids[0], skip_special_tokens = True) | |
| return response | |
| # context = grad.Textbox(lines = 10, label = 'English', placeholder = 'Context') | |
| # ans = grad.Textbox(lines = 1, label = 'Answer') | |
| # out = grad.Textbox(lines = 1, label = 'Generated Question') | |
| para = grad.Textbox(lines = 10, label = 'Paragraph', placeholder = 'Copy paragraph') | |
| out = grad.Textbox(lines = 1, label = 'Summary') | |
| grad.Interface( | |
| # text2text, | |
| # inputs = [context, ans], | |
| text2text_summary, | |
| inputs = para, | |
| outputs = out | |
| ).launch() |