File size: 4,263 Bytes
0c4b135
5c2f8ce
 
80b26db
21d73b8
 
80b26db
21d73b8
 
 
 
6fc805b
5c2f8ce
98e1cb0
d133fb5
 
4dec23b
d133fb5
21d73b8
5c2f8ce
6fc805b
 
 
 
 
 
 
21d73b8
6fc805b
0c4b135
21d73b8
 
 
0c4b135
 
 
03113eb
0c4b135
 
21d73b8
 
 
 
 
07c7708
 
 
 
21d73b8
 
 
 
 
07c7708
 
 
 
 
 
220d18f
 
 
 
07c7708
 
 
 
 
 
 
220d18f
 
 
 
0c4b135
220d18f
 
 
 
 
 
 
7668d2f
8159951
 
 
 
220d18f
8159951
220d18f
 
 
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
from transformers import AutoModelForQuestionAnswering, AutoModelForSeq2SeqLM, AutoTokenizer, PegasusForConditionalGeneration, PegasusTokenizer, pipeline
import gradio as grad
import ast

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

# model_translate_name = 'danhsf/m2m100_418M-finetuned-kde4-en-to-pt_BR'
# model_translate = AutoModelForSeq2SeqLM.from_pretrained(model_translate_name)
# model_translate_token = AutoTokenizer.from_pretrained(model_translate_name)
# translate_pipeline = pipeline('translation', model=model_translate_name)

def answer_question(question,context):
    text= "{"+"'question': '"+question+"','context': '"+context+"'}"
    di=ast.literal_eval(text)
    response = my_pipeline(di)
    print('response', response)
    return response
#grad.Interface(answer_question, inputs=["text","text"], outputs="text").launch()


def translate(text):
    inputs = model_translate_token(text, return_tensor='pt')
    translate_output = model_translate.generate(**inputs)
    response = model_translate_token(translate_output[0], skip_special_tokens=True)
    #response = translate_pipeline(text)
    return response
# grad.Interface(translate, inputs=['text',], outputs='text').launch()


# mdl_name = "google/pegasus-xsum"
# pegasus_tkn = PegasusTokenizer.from_pretrained(mdl_name)
# mdl = PegasusForConditionalGeneration.from_pretrained(mdl_name)

def summarize(text):
    tokens = pegasus_tkn(text, truncation=True, padding="longest", return_tensors="pt")
    txt_summary = mdl.generate(**tokens, num_return_sequences=5, max_length=200, temperature=1.5,num_beams=10)
    response = pegasus_tkn.batch_decode(txt_summary, skip_special_tokens=True)
    return response
    
# txt=grad.Textbox(lines=10, label="English", placeholder="English Text here")
# out=grad.Textbox(lines=10, label="Summary")
# grad.Interface(summarize, inputs=txt, outputs=out).launch()

# ZeroShotClassification using pipeline
# from transformers import pipeline
# import gradio as grad
# zero_shot_classifier = pipeline("zero-shot-classification")
def classify(text,labels):
    classifer_labels = labels.split(",")
    #["software", "politics", "love", "movies", "emergency", "advertisment","sports"]
    response = zero_shot_classifier(text,classifer_labels)
    return response
# txt=grad.Textbox(lines=1, label="English", placeholder="text to be classified")
# labels=grad.Textbox(lines=1, label="Labels", placeholder="comma separated labels")
# out=grad.Textbox(lines=1, label="Classification")
# grad.Interface(classify, inputs=[txt,labels], outputs=out).launch()

# Text classification using BartForSequenceClassification
# from transformers import BartForSequenceClassification, BartTokenizer
# import gradio as grad
# bart_tkn = BartTokenizer.from_pretrained('facebook/bart-large-mnli')
# mdl = BartForSequenceClassification.from_pretrained('facebook/bart-large-mnli')
def classify(text,label):
    tkn_ids = bart_tkn.encode(text, label, return_tensors='pt')
    tkn_lgts = mdl(tkn_ids)[0]
    entail_contra_tkn_lgts = tkn_lgts[:,[0,2]]
    probab = entail_contra_tkn_lgts.softmax(dim=1)
    response =  probab[:,1].item() * 100
    return response
# txt=grad.Textbox(lines=1, label="English", placeholder="text to be classified")
# labels=grad.Textbox(lines=1, label="Label", placeholder="Input a Label")
# out=grad.Textbox(lines=1, label="Probablity of label being true is")
# grad.Interface(classify, inputs=[txt,labels], outputs=out).launch()

# GPT2
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, max_length=100, no_repeat_ngram_size=True)
    # response = gpt2_tensors
    response=""
    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()