Spaces:
Runtime error
Runtime error
!pip install --quiet transformers | |
!pip install --quiet gradio | |
# imports | |
import gradio as gr | |
import pandas as pd | |
import tempfile | |
import itertools | |
# import required packages | |
#import torch | |
import numpy as np | |
from numpy import dot | |
from numpy.linalg import norm | |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer | |
# compute dot product of inputs | |
# summary function - test for single gradio function interfrace | |
def gr_cosine_similarity(sentence1, sentence2): | |
# Create class for data preparation | |
class SimpleDataset: | |
def __init__(self, tokenized_texts): | |
self.tokenized_texts = tokenized_texts | |
def __len__(self): | |
return len(self.tokenized_texts["input_ids"]) | |
def __getitem__(self, idx): | |
return {k: v[idx] for k, v in self.tokenized_texts.items()} | |
# load tokenizer and model, create trainer | |
model_name = "j-hartmann/emotion-english-distilroberta-base" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForSequenceClassification.from_pretrained(model_name) | |
trainer = Trainer(model=model) | |
# sentences in list | |
lines_s = [sentence1, sentence2] | |
print(type(sentence1), type(sentence2)) | |
print(sentence1, sentence2) | |
print(lines_s) | |
# Tokenize texts and create prediction data set | |
tokenized_texts = tokenizer(lines_s, truncation=True, padding=True) | |
pred_dataset = SimpleDataset(tokenized_texts) | |
# Run predictions -> predict whole df | |
predictions = trainer.predict(pred_dataset) | |
# Transform predictions to labels | |
preds = predictions.predictions.argmax(-1) | |
labels = pd.Series(preds).map(model.config.id2label) | |
scores = (np.exp(predictions[0])/np.exp(predictions[0]).sum(-1,keepdims=True)).max(1) | |
# scores raw | |
temp = (np.exp(predictions[0])/np.exp(predictions[0]).sum(-1, keepdims=True)).tolist() | |
# work in progress | |
# container | |
anger = [] | |
disgust = [] | |
fear = [] | |
joy = [] | |
neutral = [] | |
sadness = [] | |
surprise = [] | |
print(temp) | |
# extract scores (as many entries as exist in pred_texts) | |
for i in range(len(lines_s)): | |
anger.append(temp[i][0]) | |
disgust.append(temp[i][1]) | |
fear.append(temp[i][2]) | |
joy.append(temp[i][3]) | |
neutral.append(temp[i][4]) | |
sadness.append(temp[i][5]) | |
surprise.append(temp[i][6]) | |
# define both vectors for the dot product | |
# each include all values for both predictions | |
v1 = temp[0] | |
v2 = temp[1] | |
print(type(v1), type(v2)) | |
# compute dot product of all | |
dot_product = dot(v1, v2) | |
# define df | |
df = pd.DataFrame(list(zip(lines_s,labels, anger, disgust, fear, joy, neutral, sadness, surprise)), columns=['text','label', 'anger', 'disgust', 'fear', 'joy', 'neutral', 'sadness', 'surprise']) | |
# compute cosine similarity | |
# is dot product of vectors n / norms 1*..*n vectors | |
cosine_similarity = dot_product / (norm(v1) * norm(v2)) | |
# return dataframe for space output | |
return df, cosine_similarity | |
gr.Interface(gr_cosine_similarity, | |
[ | |
gr.inputs.Textbox(lines=1, placeholder="This movie always makes me cry..", default="", label="Text 1"), | |
gr.inputs.Textbox(lines=1, placeholder="Her dog is sad.", default="", label="Text 2"), | |
#gr.outputs.Textbox(type="auto", label="Cosine similarity"), | |
], | |
["dataframe","text"] | |
).launch(debug=True) |