intent-analysis / app.py
youj2005's picture
Create app
2bca1d4
raw
history blame
2.56 kB
import gradio as gr
from transformers import BartForSequenceClassification, BartTokenizer
import torch.nn.functional as F
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration
from transformers import Pipeline
te_tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-mnli')
te_model = BartForSequenceClassification.from_pretrained('facebook/bart-large-mnli')
qa_tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
qa_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base", device_map="auto")
def predict(context, intent):
input_text = "In one word, what is the opposite of: " + intent + "?"
input_ids = qa_tokenizer(input_text, return_tensors="pt")
encoded_input = qa_tokenizer(input_ids, return_tensors="pt")
opposite_output = qa_tokenizer.decode(qa_model.generate(encoded_input)[0])
input_text = "In one word, what is the following describing: " + context
input_ids = qa_tokenizer(input_text, return_tensors="pt")
encoded_input = qa_tokenizer(input_ids, return_tensors="pt")
object_output = qa_tokenizer.decode(qa_model.generate(encoded_input)[0])
batch = ['I think the ' + object_output + ' are long.', 'I think the ' + object_output + ' are ' + opposite_output, 'I think the ' + object_output + ' are the perfect']
outputs = []
for i, hypothesis in enumerate(batch):
input_ids = te_tokenizer.encode(context, hypothesis, return_tensors='pt')
# -> [contradiction, neutral, entailment]
logits = te_model(input_ids)[0][0]
if (i == 2):
# -> [contradiction, entailment]
probs = logits[[0,2]].softmax(dim=0)
else:
probs = logits.softmax(dim=0)
outputs.append(probs)
# -> [entailment, contradiction]
outputs[2] = outputs[2].flip(dims=[0])
# -> [entailment, neutral, contradiction]
outputs[0] = outputs[0].flip(dims=[0])
pn_tensor = (outputs[0] + outputs[1]).softmax(dim=0)
pn_tensor[1] = pn_tensor[1] * outputs[2][0]
pn_tensor[2] = pn_tensor[2] * outputs[2][1]
pn_tensor[0] = pn_tensor[0] * outputs[2][1]
pn_tensor = F.normalize(pn_tensor, p=1, dim=0)
pn_tensor = pn_tensor.softmax(dim=0)
return {"entailment": pn_tensor[0].item(), "neutral": pn_tensor[1].item(), "contradiction": pn_tensor[2].item()}
gradio_app = gr.Interface(
predict,
inputs=gr.Text(label="Input sentence"),
outputs=[gr.Label(num_top_classes=3)],
title="Hot Dog? Or Not?",
)
if __name__ == "__main__":
gradio_app.launch()