first_space / app.py
flichote's picture
Update app.py
5dc8d86
raw
history blame
5.1 kB
# from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
# import gradio as grad
# import ast
# # mdl_name = "deepset/roberta-base-squad2"
# 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 = "Helsinki-NLP/opus-mt-en-zh"
# opus_translator = pipeline("translation", model=mdl_name)
# def translate(text):
# response = opus_translator(text)
# return response
# grad.Interface(translate, inputs=["text",], outputs="text").launch()
# from transformers import pipeline
# import gradio as grad
# mdl_name = "Helsinki-NLP/opus-mt-en-zh"
# opus_translator = pipeline("translation", model=mdl_name)
# def translate(text):
# response = opus_translator(text)
# return response
# txt=grad.Textbox(lines=1, label="English", placeholder="English Text here")
# out=grad.Textbox(lines=1, label="Chinese")
# grad.Interface(translate, inputs=txt, outputs=out).launch()
################################5-6
# from transformers import AutoModel,AutoTokenizer,AutoModelForSeq2SeqLM
# import gradio as grad
# mdl_name = "Helsinki-NLP/opus-mt-en-fr"
# mdl = AutoModelForSeq2SeqLM.from_pretrained(mdl_name)
# my_tkn = AutoTokenizer.from_pretrained(mdl_name)
# #opus_translator = pipeline("translation", model=mdl_name)
# def translate(text):
# inputs = my_tkn(text, return_tensors="pt")
# trans_output = mdl.generate(**inputs)
# response = my_tkn.decode(trans_output[0], skip_special_tokens=True)
# #response = opus_translator(text)
# return response
# txt=grad.Textbox(lines=1, label="English", placeholder="English Text here")
# out=grad.Textbox(lines=1, label="French")
# grad.Interface(translate, inputs=txt, outputs=out).launch()
# from transformers import PegasusForConditionalGeneration, PegasusTokenizer
# import gradio as grad
# 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)
# 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()
# from transformers import PegasusForConditionalGeneration, PegasusTokenizer
# import gradio as grad
# 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")
# translated_txt = mdl.generate(**tokens,num_return_sequences=5,max_length=200,temperature=1.5,num_beams=10)
# response = pegasus_tkn.batch_decode(translated_txt, 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()
# 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()
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()