File size: 3,132 Bytes
488d025
d55a6ac
 
cdd78db
 
 
 
 
 
d55a6ac
 
 
 
 
 
 
488d025
 
a663e78
488d025
 
135593c
488d025
 
 
 
 
 
a663e78
 
 
a0b921f
a663e78
 
 
 
 
 
 
 
 
 
 
 
 
 
a0b921f
 
 
9fa1f1e
a0b921f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fa1f1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
'''from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
import gradio as grad
import ast

#mdl_name = "deepset/roberta-base-squad2"
#my_pipeline = pipeline('question-answering', model=mdl_name, tokenizer=mdl_name)

mdl_name = "distilbert-base-cased-distilled-squad"
my_pipeline = pipeline('question-answering', model=mdl_name,tokenizer=mdl_name)

def answer_question(question,context):
    text= "{"+"'question': '"+question+"','context': '"+context+"'}"
    
    di=ast.literal_eval(text)
    response = my_pipeline(di)
    return response
grad.Interface(answer_question, inputs=["text","text"], outputs="text").launch()
'''
''' 
from transformers import pipeline
import gradio as grad
mdl_name = "VietAI/envit5-translation"
opus_translator = pipeline("translation", model=mdl_name)

def translate(text):
    
    response = opus_translator(text)
    return response
grad.Interface(translate, inputs=["text",], outputs="text").launch()
'''

'''5.11

from transformers import GPT2LMHeadModel,GPT2Tokenizer
import gradio as grad

mdl = GPT2LMHeadModel.from_pretrained('gpt2')
gpt2_tkn=GPT2Tokenizer.from_pretrained('gpt2')

def generate(starting_text):
    tkn_ids = gpt2_tkn.encode(starting_text, return_tensors = 'pt')
    gpt2_tensors = mdl.generate(tkn_ids)
    response = gpt2_tensors
    return response
txt=grad.Textbox(lines=1, label="English", placeholder="English Text here")
out=grad.Textbox(lines=1, label="Generated Tensors")
grad.Interface(generate, inputs=txt, outputs=out).launch()
'''

'''5.12
from transformers import GPT2LMHeadModel,GPT2Tokenizer
import gradio as grad

mdl = GPT2LMHeadModel.from_pretrained('gpt2')
gpt2_tkn=GPT2Tokenizer.from_pretrained('gpt2')


def generate(starting_text):
    tkn_ids = gpt2_tkn.encode(starting_text, return_tensors = 'pt')
    gpt2_tensors = mdl.generate(tkn_ids)
    response=""
    #response = gpt2_tensors
    for i, x in enumerate(gpt2_tensors):
       response=response+f"{i}: {gpt2_tkn.decode(x, skip_special_tokens=True)}"
    return response

txt=grad.Textbox(lines=1, label="English", placeholder="English Text here")
out=grad.Textbox(lines=1, label="Generated Tensors")
grad.Interface(generate, inputs=txt, outputs=out).launch()
'''

#5.20
from transformers import AutoModelWithLMHead, AutoTokenizer
import gradio as grad

text2text_tkn = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap")
mdl = AutoModelWithLMHead.from_pretrained("mrm8488/t5-base-finetuned-question-generation-ap")

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

context=grad.Textbox(lines=10, label="English", placeholder="Context")
ans=grad.Textbox(lines=1, label="Answer")
out=grad.Textbox(lines=1, label="Genereated Question")
grad.Interface(text2text, inputs=[context,ans], outputs=out).launch()