Spaces:
Runtime error
Runtime error
File size: 2,830 Bytes
7e6d4d1 f957ae9 c2dbaac 7e6d4d1 70b4cc0 7f79884 f957ae9 c2dbaac 0754326 c2dbaac 7e6d4d1 70b4cc0 4c740cc 7e6d4d1 c2dbaac 7e6d4d1 70b4cc0 7e6d4d1 70b4cc0 c2dbaac f957ae9 c2dbaac 7e6d4d1 70b4cc0 c2dbaac f957ae9 8a39aa7 |
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 |
# from transformers import AutoModelWithLMHead, AutoTokenizer
# Translate
from transformers import T5ForConditionalGeneration, T5Tokenizer
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')
# translate, sentiment
text2text_tkn = T5Tokenizer.from_pretrained('t5-small')
mdl = T5ForConditionalGeneration.from_pretrained('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.decode(tkn_ids[0], skip_special_tokens = True)
return response
def text2text_translate(text):
inp = "translate English to German:: " + text
enc = text2text_tkn(inp, return_tensors = 'pt')
tokens = mdl.generate(**enc)
response = text2text_tkn.batch_decode(tokens)
return response
def text2text_sentiment(text):
inp = "sst2 sentence: " + text
enc = text2text_tkn(inp, return_tensors = 'pt')
tokens = mdl.generate(**enc)
response = text2text_tkn.batch_decode(tokens)
return response
# question
# context = grad.Textbox(lines = 10, label = 'English', placeholder = 'Context')
# ans = grad.Textbox(lines = 1, label = 'Answer')
# out = grad.Textbox(lines = 1, label = 'Generated Question')
# summary
# para = grad.Textbox(lines = 10, label = 'Paragraph', placeholder = 'Copy paragraph')
# out = grad.Textbox(lines = 1, label = 'Summary')
# tranlate
# para = grad.Textbox(lines = 1, label = 'English Text', placeholder = 'Text in English')
# out = grad.Textbox(lines = 1, label = 'German Translation')
# sentiment
para = grad.Textbox(lines = 1, label = 'English Text', placeholder = 'Text in English')
out = grad.Textbox(lines = 1, label = 'Sentiment')
grad.Interface(
# text2text,
# inputs = [context, ans],
# text2text_summary,
# text2text_translate,
text2text_sentiment,
inputs = para,
outputs = out
).launch() |